Skip to content

Commit

Permalink
fix: move and refactor to reduce duplicate code
Browse files Browse the repository at this point in the history
  • Loading branch information
BibiSebi committed Jul 1, 2024
1 parent 4ae03c4 commit a3baf27
Show file tree
Hide file tree
Showing 5 changed files with 46 additions and 98 deletions.
38 changes: 38 additions & 0 deletions src/session-adapters/cookieAdapter.ts
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)
},
}
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
import http from 'http'
import { Adapter, InternalAdapter } from '../AuthHandlerParams'
import {
Adapter,
InternalAdapter,
} from '../storyblok-auth-api/AuthHandlerParams'

export const createInternalAdapter = ({
req,
Expand Down
4 changes: 2 additions & 2 deletions src/session/sessionCookieStore.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,8 @@ import { AppSessionCookieStoreFactory, AppSessionStore } from './types'
import { getAllSessions, getSession, putSession, removeSession } from './crud'
import { refreshStoredAppSession } from './refreshStoredAppSession'
import { GetCookie, SetCookie } from '../utils'
import { createInternalAdapter } from '../storyblok-auth-api/session-adapters/createInternalAdapter'
import { cookieAdapter } from '../storyblok-auth-api/session-adapters/cookieAdapter'
import { createInternalAdapter } from '../session-adapters/createInternalAdapter'
import { cookieAdapter } from '../session-adapters/cookieAdapter'

export const sessionCookieStore: AppSessionCookieStoreFactory =
(params) =>
Expand Down
4 changes: 2 additions & 2 deletions src/storyblok-auth-api/auth-handler.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,8 @@ import http from 'http'
import { AuthHandlerParams } from './AuthHandlerParams'
import { handleAnyRequest } from './handle-requests'
import { reconcileNodeResponse } from './reconcileNodeResponse'
import { createInternalAdapter } from './session-adapters/createInternalAdapter'
import { cookieAdapter } from './session-adapters/cookieAdapter'
import { createInternalAdapter } from '../session-adapters/createInternalAdapter'
import { cookieAdapter } from '../session-adapters/cookieAdapter'

/**
* Auth handler for Node.js
Expand Down
93 changes: 0 additions & 93 deletions src/storyblok-auth-api/session-adapters/cookieAdapter.ts

This file was deleted.

0 comments on commit a3baf27

Please sign in to comment.