-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathauth.ts
More file actions
53 lines (49 loc) · 1.42 KB
/
Copy pathauth.ts
File metadata and controls
53 lines (49 loc) · 1.42 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
import NextAuth from "next-auth";
import { FirestoreAdapter } from "@auth/firebase-adapter";
import authConfig from "./auth.config";
import { getUserById, updateUserById } from "@/db/firebase/user";
import { updateUser } from "@/db/firebase/user";
import Resend from "next-auth/providers/resend";
import { cert } from "firebase-admin/app";
export const { handlers, signIn, signOut, auth } = NextAuth({
events: {
async linkAccount({ user }) {
if (!user.id) return;
await updateUserById(user.id, { emailVerified: new Date() });
},
},
callbacks: {
async session({ token, session }) {
if (token.sub && session.user) {
session.user.id = token.sub;
}
return session;
},
async jwt({ token }) {
if (!token.sub) return token;
const existingUser = await getUserById(token.sub);
if (!existingUser) {
return null;
}
token.name = existingUser.name;
return token;
},
},
adapter: FirestoreAdapter({
credential: cert({
projectId: process.env.AUTH_FIREBASE_PROJECT_ID,
clientEmail: process.env.AUTH_FIREBASE_CLIENT_EMAIL,
privateKey: process.env.AUTH_FIREBASE_PRIVATE_KEY,
}),
}),
session: { strategy: "jwt" },
providers: [
Resend({ from: "no-reply@mynotepal.ai" }),
...authConfig.providers,
],
secret: process.env.AUTH_SECRET,
pages: {
signIn: "/login",
verifyRequest: "/verify-email",
},
});