From 6311966f9216dcaa6148bcf01f8ac7e9274a02c8 Mon Sep 17 00:00:00 2001 From: Avraham Sakal Date: Sun, 21 Sep 2025 19:20:10 -0400 Subject: [PATCH] fix: move new user creation code to `isgnIn` callback --- server/authjs-handler.ts | 27 +++++++++++++++++---------- 1 file changed, 17 insertions(+), 10 deletions(-) diff --git a/server/authjs-handler.ts b/server/authjs-handler.ts index e174995..9399c2f 100644 --- a/server/authjs-handler.ts +++ b/server/authjs-handler.ts @@ -72,13 +72,25 @@ const authjsConfig = { async signIn({ user, account, profile }) { if (typeof user?.email !== "string") return false; const dbClient = await getDbClient(POSTGRES_CONNECTION_STRING); - const userFromDb = await dbClient + let userFromDb = await dbClient .selectFrom("users") .selectAll() .where("email", "=", user.email) .executeTakeFirst(); if (!userFromDb) { - return false; + userFromDb = ( + await dbClient + .insertInto("users") + .values({ + email: user.email, + username: user.email, + password: null, + createdAt: null, + lastLogin: null, + }) + .returningAll() + .execute() + )[0]; } console.log("signIn", user, account, profile); return true; @@ -91,6 +103,9 @@ const authjsConfig = { .selectAll() .where("email", "=", token.email || "") .executeTakeFirst(); + /** TODO: the following should never happen, because the account in + * created in the `isgnIn` step; but I don't know what error to throw here + * if for some reason there is no such account.*/ if (!userFromDb) { userFromDb = ( await dbClient @@ -105,14 +120,6 @@ const authjsConfig = { .returningAll() .execute() )[0]; - // await db.users.create({ - // // id: token.id, - // email: token.email, - // username: token.email, - // password: null, - // createdAt: null, - // lastLogin: null, - // }); } return { ...token,