diff --git a/lib/plugins/auth/routes/mod.ts b/lib/plugins/auth/routes/mod.ts index 0d829131..ca2a1e69 100644 --- a/lib/plugins/auth/routes/mod.ts +++ b/lib/plugins/auth/routes/mod.ts @@ -54,17 +54,15 @@ export const getRoutesByProvider = ( deletedAt: userCurrent?.deletedAt, } as unknown as AuthUser; - // IMPORTANT: remove undefined values to prevent "Unsupported type of value" + // IMPORTANT: remove undefined values to prevent error "Unsupported type of value" // and let the database handle setting defaults (e.g. null or anything else) Object.keys(user).forEach((key) => { - console.log(key, user[key]) if (user[key] === undefined) delete user[key]; }); - console.log({ userCurrent, user }) - if (!userCurrent) { await ctx.state.auth.createUser(user); + await ctx.state.auth.createUserSession(user, sessionId); } else { await ctx.state.auth.updateUser(user); await ctx.state.auth.updateUserSession(user, sessionId); diff --git a/lib/plugins/auth/utils/adapters/database.ts b/lib/plugins/auth/utils/adapters/database.ts index 51a35cfc..ea4f25d6 100644 --- a/lib/plugins/auth/utils/adapters/database.ts +++ b/lib/plugins/auth/utils/adapters/database.ts @@ -14,27 +14,21 @@ export const createDatabaseAuth = (db: ReturnType): Auth => { return { createUser: async (user: AuthUser) => { user.id = id(); - await db.transaction(async (tx) => { - await tx.insert($users).values(user).returning(); - await tx.insert($sessions).values({ - id: user.sessionId, - $userId: user.id, - }); - }); + await db.insert($users).values(user); + }, + createUserSession: async (user: AuthUser, sessionId: string) => { + await db.insert($sessions).values({ id: sessionId, $userId: user.id }); }, updateUser: async (user: AuthUser) => { - user.updatedAt = new Date().toISOString(); await db.update($users).set(user).where(eq($users.id, user.id)); await db.query.$users.findFirst({ where: eq($users.id, user.id) }); }, updateUserSession: async (user: AuthUser, sessionId: string) => { - user.updatedAt = new Date().toISOString(); + // IMPORTANT: this invalidates the old session and creates a new one. + // If multiple sessions per user are allowed, this should be adjusted. await db.transaction(async (tx) => { await tx.delete($sessions).where(eq($sessions.$userId, user.id)); - await tx.insert($sessions).values({ - id: sessionId, - $userId: user.id, - }); + await tx.insert($sessions).values({ id: sessionId, $userId: user.id }); }); }, getUser: async (authId: string) => { diff --git a/lib/plugins/auth/utils/adapters/datastore.ts b/lib/plugins/auth/utils/adapters/datastore.ts index 66d782ca..f5f32989 100644 --- a/lib/plugins/auth/utils/adapters/datastore.ts +++ b/lib/plugins/auth/utils/adapters/datastore.ts @@ -11,19 +11,27 @@ export const createDatastoreAuth = (kv = KV): Auth => { user.updatedAt = user.createdAt; user.deletedAt = ""; const usersKey = ["users", user.authId]; - const usersBySessionKey = ["usersBySession", user.sessionId]; const atomicOp = kv.atomic() .check({ key: usersKey, versionstamp: null }) + .set(usersKey, user); + + const res = await atomicOp.commit(); + if (!res.ok) throw new Error("Failed to create user"); + }, + createUserSession: async (user: AuthUser, sessionId: string) => { + user.updatedAt = new Date().toISOString(); + const usersBySessionKey = ["usersBySession", sessionId]; + + const atomicOp = kv.atomic() .check({ key: usersBySessionKey, versionstamp: null }) - .set(usersKey, user) .set(usersBySessionKey, user); const res = await atomicOp.commit(); - if (!res.ok) throw new Error("Failed to create user"); + if (!res.ok) throw new Error("Failed to create user session"); }, updateUser: async (user: AuthUser) => { - user.updatedAt = new Date().toISOString(); + user.updatedAt ||= new Date().toISOString(); const usersKey = ["users", user.authId]; const usersBySessionKey = ["usersBySession", user.sessionId];