fix: move new user creation code to isgnIn callback
This commit is contained in:
+17
-10
@@ -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,
|
||||
|
||||
Reference in New Issue
Block a user