Skip to content

Commit

Permalink
feat: add PR change request
Browse files Browse the repository at this point in the history
  • Loading branch information
BibiSebi committed Jul 9, 2024
1 parent 2138fca commit f19211f
Show file tree
Hide file tree
Showing 3 changed files with 24 additions and 18 deletions.
13 changes: 7 additions & 6 deletions src/session-adapters/cookieAdapter.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,8 +23,9 @@ const createScopedKey = ({

const sessionKey = 'sb.auth'
//NOTE: possibly cookieAdapter can become createCookieAdapter(key: string)

export const cookieAdapter: Adapter = {
getItem: ({ req, spaceId, userId }) => {
getSession: ({ req, spaceId, userId }) => {
const cookie = getCookie(
req,
createScopedKey({ spaceId, userId, key: sessionKey }),
Expand All @@ -43,11 +44,11 @@ export const cookieAdapter: Adapter = {
return verifiedData
},

setItem: ({ res, spaceId, userId, value }) => {
setSession: ({ res, spaceId, userId, session }) => {
const expires = new Date()
expires.setDate(expires.getDate() + 7)

const signedData = jwt.sign({ data: value }, clientSecret)
const signedData = jwt.sign({ data: session }, clientSecret)
setCookie(
res,
createScopedKey({ spaceId, userId, key: sessionKey }),
Expand All @@ -57,10 +58,10 @@ export const cookieAdapter: Adapter = {
return true
},

hasItem: async (params) =>
(await cookieAdapter.getItem(params)) !== undefined,
hasSession: async (params) =>
(await cookieAdapter.getSession(params)) !== undefined,

removeItem: ({ res, spaceId, userId }) => {
removeSession: ({ res, spaceId, userId }) => {
expireCookie(res, createScopedKey({ spaceId, userId, key: sessionKey }))
return true
},
Expand Down
19 changes: 12 additions & 7 deletions src/session-adapters/internalAdapter.ts
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,7 @@ export const createInternalAdapter: CreateInternalAdapter = ({
return {
getSession: async ({ spaceId, userId }) => {
try {
const session = await adapter.getItem({
const session = await adapter.getSession({
req,
res,
clientId: params.clientId,
Expand All @@ -87,45 +87,50 @@ export const createInternalAdapter: CreateInternalAdapter = ({

setSession: async ({ spaceId, userId, session }) => {
try {
await adapter.setItem({
const isSessionSet = await adapter.setSession({
req,
res,
clientId: params.clientId,
spaceId,
userId,
value: session,
session,
})

return isSessionSet
} catch (e) {
console.log('Setting the session failed: ', e)
return false
}
},

hasSession: ({ spaceId, userId }) => {
hasSession: async ({ spaceId, userId }) => {
try {
adapter.hasItem({
const hasSession = await adapter.hasSession({
req,
res,
clientId: params.clientId,
spaceId,
userId,
})

return hasSession
} catch (e) {
console.log('Session could not be found: ', e)

return false
}
},

removeSession: async ({ spaceId, userId }) => {
try {
await adapter.removeItem({
const sessionRemoved = await adapter.removeSession({
req,
res,
clientId: params.clientId,
spaceId,
userId,
})

return sessionRemoved
} catch (e) {
console.log('Removing the session failed: ', e)
return false
Expand Down
10 changes: 5 additions & 5 deletions src/session-adapters/publicAdapter.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,32 +4,32 @@ import { AppSession } from '../session'
export type MaybePromise<T> = T | Promise<T>

export type Adapter = {
getItem: (params: {
getSession: (params: {
req: IncomingMessage
res: ServerResponse
clientId: string
spaceId: string
userId: string
}) => MaybePromise<AppSession | undefined>

setItem: (params: {
setSession: (params: {
req: IncomingMessage
res: ServerResponse
clientId: string
spaceId: string
userId: string
value: AppSession
session: AppSession
}) => MaybePromise<boolean>

removeItem: (params: {
removeSession: (params: {
req: IncomingMessage
res: ServerResponse
clientId: string
spaceId: string
userId: string
}) => MaybePromise<boolean>

hasItem: (params: {
hasSession: (params: {
req: IncomingMessage
res: ServerResponse
clientId: string
Expand Down

0 comments on commit f19211f

Please sign in to comment.