-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix: move and refactor to reduce duplicate code
- Loading branch information
Showing
5 changed files
with
46 additions
and
98 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,38 @@ | ||
import { expireCookie, getCookie, setCookie, verifyData } from '../utils' | ||
import jwt from 'jsonwebtoken' | ||
import { type Adapter } from '../storyblok-auth-api/AuthHandlerParams' | ||
|
||
const clientSecret = process.env['CLIENT_SECRET'] || '' | ||
|
||
export const cookieAdapter: Adapter = { | ||
getItem: ({ req, key }) => { | ||
const cookie = getCookie(req, key) | ||
|
||
if (!cookie) { | ||
return undefined | ||
} | ||
|
||
//TODO: fix typing | ||
return verifyData(clientSecret)(cookie) as object | ||
}, | ||
setItem: ({ req, res, key, value }) => { | ||
const currentSession = cookieAdapter.getItem({ req, res, key }) | ||
|
||
// TODO: Improve readability | ||
// TODO: improve checking as user might select their own key and not sb.auth | ||
const isSession = key === 'sb.auth' | ||
const data = isSession | ||
? { | ||
sessions: currentSession ? [currentSession, value] : [value], | ||
} | ||
: value | ||
|
||
const signedData = jwt.sign({ data }, clientSecret) | ||
setCookie(res, key, signedData) | ||
}, | ||
hasItem: ({ req, res, key }) => | ||
cookieAdapter.getItem({ req, res, key }) !== undefined, | ||
removeItem: ({ res, key }) => { | ||
expireCookie(res, key) | ||
}, | ||
} |
5 changes: 4 additions & 1 deletion
5
...session-adapters/createInternalAdapter.ts → ...session-adapters/createInternalAdapter.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.