fix: move new user creation code to `isgnIn` callback

master
Avraham Sakal 3 weeks ago
parent 0207e4fc47
commit 6311966f92

@ -72,13 +72,25 @@ const authjsConfig = {
async signIn({ user, account, profile }) { async signIn({ user, account, profile }) {
if (typeof user?.email !== "string") return false; if (typeof user?.email !== "string") return false;
const dbClient = await getDbClient(POSTGRES_CONNECTION_STRING); const dbClient = await getDbClient(POSTGRES_CONNECTION_STRING);
const userFromDb = await dbClient let userFromDb = await dbClient
.selectFrom("users") .selectFrom("users")
.selectAll() .selectAll()
.where("email", "=", user.email) .where("email", "=", user.email)
.executeTakeFirst(); .executeTakeFirst();
if (!userFromDb) { 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); console.log("signIn", user, account, profile);
return true; return true;
@ -91,6 +103,9 @@ const authjsConfig = {
.selectAll() .selectAll()
.where("email", "=", token.email || "") .where("email", "=", token.email || "")
.executeTakeFirst(); .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) { if (!userFromDb) {
userFromDb = ( userFromDb = (
await dbClient await dbClient
@ -105,14 +120,6 @@ const authjsConfig = {
.returningAll() .returningAll()
.execute() .execute()
)[0]; )[0];
// await db.users.create({
// // id: token.id,
// email: token.email,
// username: token.email,
// password: null,
// createdAt: null,
// lastLogin: null,
// });
} }
return { return {
...token, ...token,

Loading…
Cancel
Save