Skip to content

Commit

Permalink
optimize session handling
Browse files Browse the repository at this point in the history
  • Loading branch information
arily committed Mar 27, 2024
1 parent 09334f0 commit 92a3c9a
Showing 1 changed file with 7 additions and 2 deletions.
9 changes: 7 additions & 2 deletions src/server/trpc/routers/session.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ import { Logger } from '$base/logger'
const logger = Logger.child({ label: 'session', backend: 'transport', transport: 'trpc' })

export const router = _router({
login: pSession
login: publicProcedure
.input(
object({
handle: zodHandle,
Expand All @@ -31,11 +31,16 @@ export const router = _router({
if (!ok) {
throwGucchoError(GucchoError.PasswordMismatch)
}

const opt = {
httpOnly: true,
maxAge: persist ? Constant.PersistDuration as number : undefined,
}
const newSessionId = await sessions.update(ctx.session.id, { userId: UserProvider.idToString(user.id) })
const partialSession = { userId: UserProvider.idToString(user.id) }
const newSessionId = ctx.session.id
? await sessions.update(ctx.session.id, partialSession)
: await sessions.create(Object.assign(partialSession, detectDevice(ctx.h3Event)))

if (newSessionId && (newSessionId !== ctx.session.id || persist)) {
setCookie(ctx.h3Event, Constant.SessionLabel, newSessionId, opt)
}
Expand Down

0 comments on commit 92a3c9a

Please sign in to comment.