Skip to content

Commit

Permalink
refactor trpc session
Browse files Browse the repository at this point in the history
  • Loading branch information
arily committed Apr 7, 2024
1 parent 5fc2e35 commit c7c22d6
Showing 1 changed file with 23 additions and 31 deletions.
54 changes: 23 additions & 31 deletions src/server/trpc/middleware/session.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,25 @@ import { type Session } from '$base/server/session'
const config = {
httpOnly: true,
}

class SessionBinding {
persist?: boolean
constructor(readonly id: string, opts: { persist?: boolean }) {
this.persist = opts.persist
}

async getBinding() {
if (!this.id) {
return undefined
}
return (await sessions.get(this.id)) as Awaited<ReturnType<typeof sessions['get']>>
}

update(data: Partial<Session>) {
return sessions.update(this.id, data)
}
}

export const sessionProcedure = publicProcedure
.use(async ({ ctx, next }) => {
const opt = {
Expand All @@ -19,21 +38,15 @@ export const sessionProcedure = publicProcedure
setCookie(ctx.h3Event, Constant.SessionLabel, sessionId, opt)
return await next({
ctx: Object.assign(ctx, {
session: {
id: sessionId,
persist: ctx.session.persist,
},
session: new SessionBinding(sessionId, { persist: ctx.session.persist }),
}),
})
}

if (haveSession(ctx.h3Event)) {
return await next({
ctx: Object.assign(ctx, {
session: {
id: ctx.session.id,
persist: ctx.session.persist,
},
session: new SessionBinding(ctx.session.id, { persist: ctx.session.persist }),
}),
})
}
Expand All @@ -44,10 +57,7 @@ export const sessionProcedure = publicProcedure
setCookie(ctx.h3Event, Constant.SessionLabel, sessionId, opt)
return await next({
ctx: Object.assign(ctx, {
session: {
id: sessionId,
persist: ctx.session.persist,
},
session: new SessionBinding(sessionId, { persist: ctx.session.persist }),
}),
})
}
Expand All @@ -66,27 +76,9 @@ export const sessionProcedure = publicProcedure

return await next({
ctx: Object.assign(ctx, {
session: {
id: refreshed,
persist,
},
session: new SessionBinding(refreshed, { persist }),
}),
})
}
}
})
.use(async ({ ctx, next }) => {
return await next({
ctx: Object.assign(ctx, {
session: Object.assign(ctx.session, {
async getBinding() {
if (!ctx.session.id) {
return undefined
}
return (await sessions.get(ctx.session.id)) as Awaited<ReturnType<typeof sessions['get']>>
},
update: (data: Partial<Session>) => sessions.update(ctx.session.id, data),
}),
}),
})
})

0 comments on commit c7c22d6

Please sign in to comment.