can sign in and out
This commit is contained in:
+51
-11
@@ -5,6 +5,7 @@ import {
|
||||
setEnvDefaults,
|
||||
} from "@auth/core";
|
||||
import CredentialsProvider from "@auth/core/providers/credentials";
|
||||
import GoogleProvider from "@auth/core/providers/google";
|
||||
import type { Session } from "@auth/core/types";
|
||||
// TODO: stop using universal-middleware and directly integrate server middlewares instead and/or use vike-server https://vike.dev/server. (Bati generates boilerplates that use universal-middleware https://github.com/magne4000/universal-middleware to make Bati's internal logic easier. This is temporary and will be removed soon.)
|
||||
import type {
|
||||
@@ -12,17 +13,11 @@ import type {
|
||||
UniversalHandler,
|
||||
UniversalMiddleware,
|
||||
} from "@universal-middleware/core";
|
||||
import { env } from "./env.js";
|
||||
import { getDb } from "../database/index.js";
|
||||
|
||||
const env: Record<string, string | undefined> =
|
||||
typeof process?.env !== "undefined"
|
||||
? process.env
|
||||
: import.meta && "env" in import.meta
|
||||
? (
|
||||
import.meta as ImportMeta & {
|
||||
env: Record<string, string | undefined>;
|
||||
}
|
||||
).env
|
||||
: {};
|
||||
const POSTGRES_CONNECTION_STRING =
|
||||
"postgres://neondb_owner:npg_sOVmj8vWq2zG@ep-withered-king-adiz9gpi-pooler.c-2.us-east-1.aws.neon.tech:5432/neondb?sslmode=require&channel_binding=true";
|
||||
|
||||
if (!globalThis.crypto) {
|
||||
/**
|
||||
@@ -43,7 +38,7 @@ const authjsConfig = {
|
||||
env.AUTH_TRUST_HOST ?? env.VERCEL ?? env.NODE_ENV !== "production"
|
||||
),
|
||||
// TODO: Replace secret {@see https://authjs.dev/reference/core#secret}
|
||||
secret: "MY_SECRET",
|
||||
secret: "buginoo",
|
||||
providers: [
|
||||
// TODO: Choose and implement providers
|
||||
CredentialsProvider({
|
||||
@@ -66,7 +61,52 @@ const authjsConfig = {
|
||||
return user ?? null;
|
||||
},
|
||||
}),
|
||||
GoogleProvider({
|
||||
clientId:
|
||||
"697711350664-t6237s5n3ttjd1npp1qif1aupptkr0va.apps.googleusercontent.com",
|
||||
clientSecret: "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",
|
||||
}),
|
||||
],
|
||||
callbacks: {
|
||||
async signIn({ user, account, profile }) {
|
||||
if (typeof user?.email !== "string") return false;
|
||||
const db = await getDb(POSTGRES_CONNECTION_STRING);
|
||||
const userFromDb = await db.users.findByEmailAddress(user.email);
|
||||
if (!userFromDb) {
|
||||
return false;
|
||||
}
|
||||
console.log("signIn", user, account, profile);
|
||||
return true;
|
||||
},
|
||||
jwt: async ({ token }) => {
|
||||
if (typeof token?.email !== "string") return token;
|
||||
const db = await getDb(POSTGRES_CONNECTION_STRING);
|
||||
let userFromDb = await db.users.findByEmailAddress(token.email || "");
|
||||
if (!userFromDb) {
|
||||
userFromDb = await db.users.create({
|
||||
// id: token.id,
|
||||
email: token.email,
|
||||
username: token.email,
|
||||
password: null,
|
||||
createdAt: null,
|
||||
lastLogin: null,
|
||||
});
|
||||
}
|
||||
return {
|
||||
...token,
|
||||
id: userFromDb?.id || "",
|
||||
};
|
||||
},
|
||||
session: ({ token, session }) => {
|
||||
return {
|
||||
...session,
|
||||
user: {
|
||||
...session.user,
|
||||
id: token.id as string,
|
||||
},
|
||||
};
|
||||
},
|
||||
},
|
||||
} satisfies Omit<AuthConfig, "raw">;
|
||||
|
||||
/**
|
||||
|
||||
Reference in New Issue
Block a user