Skip to content

Commit

Permalink
Fix bugs for multiple sessions
Browse files Browse the repository at this point in the history
  • Loading branch information
tom-sherman committed Sep 22, 2024
1 parent fd8b3f2 commit 4ab3e48
Showing 1 changed file with 9 additions and 9 deletions.
18 changes: 9 additions & 9 deletions packages/frontpage/lib/auth.ts
Original file line number Diff line number Diff line change
Expand Up @@ -288,7 +288,7 @@ export const handlers = {
throw new Error("Missing expires");
}

await db.insert(schema.OauthSession).values({
const { lastInsertRowid } = await db.insert(schema.OauthSession).values({
did: row.did,
username: row.username,
iss: row.iss,
Expand All @@ -301,10 +301,15 @@ export const handlers = {
dpopPublicJwk: row.dpopPublicJwk,
});

if (!lastInsertRowid) {
throw new Error("Failed to insert session");
}

const userToken = await new SignJWT()
.setSubject(row.did)
.setProtectedHeader({ alg: USER_SESSION_JWT_ALG })
.setIssuedAt()
.setJti(lastInsertRowid.toString())
.sign(
// TODO: This probably ought to be a different key
await getPrivateJwk(),
Expand Down Expand Up @@ -355,7 +360,7 @@ export async function signOut() {

await db
.delete(schema.OauthSession)
.where(eq(schema.OauthSession.did, session.user.did));
.where(eq(schema.OauthSession.sessionId, session.user.sessionId));
}

export const getSession = cache(async () => {
Expand All @@ -372,19 +377,14 @@ export const getSession = cache(async () => {
return null;
}

if (!token.payload.sub) {
return null;
}

const did = parseDid(token.payload.sub);
if (!did) {
if (!token.payload.jti) {
return null;
}

const [session] = await db
.select()
.from(schema.OauthSession)
.where(eq(schema.OauthSession.did, did));
.where(eq(schema.OauthSession.sessionId, Number(token.payload.jti)));

if (!session) {
return null;
Expand Down

0 comments on commit 4ab3e48

Please sign in to comment.