-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathauth.ts
More file actions
55 lines (53 loc) · 1.52 KB
/
auth.ts
File metadata and controls
55 lines (53 loc) · 1.52 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
54
55
import NextAuth from "next-auth";
import Google from "next-auth/providers/google";
import { isOwnerEmail } from "@/lib/owners";
import { prisma } from "@/lib/prisma";
const domain = process.env.ALLOWED_EMAIL_DOMAIN?.trim().toLowerCase();
export const { handlers, auth, signIn, signOut } = NextAuth({
session: { strategy: "jwt" },
trustHost: true,
pages: {
signIn: "/login",
},
providers: [
Google({
clientId: process.env.AUTH_GOOGLE_ID ?? process.env.GOOGLE_CLIENT_ID,
clientSecret:
process.env.AUTH_GOOGLE_SECRET ?? process.env.GOOGLE_CLIENT_SECRET,
}),
],
callbacks: {
async signIn({ user }) {
const email = user.email?.toLowerCase();
if (!domain || !email) return false;
return email.endsWith(`@${domain}`);
},
async jwt({ token, user }) {
if (user?.email) {
const email = user.email.trim().toLowerCase();
const dbUser = await prisma.user.upsert({
where: { email },
create: {
email,
name: user.name,
},
update: {
name: user.name ?? undefined,
},
});
token.sub = dbUser.id;
}
return token;
},
async session({ session, token }) {
if (session.user) {
if (token.sub) session.user.id = token.sub;
const email =
session.user.email ??
(typeof token.email === "string" ? token.email : undefined);
session.user.isOwner = isOwnerEmail(email);
}
return session;
},
},
});