-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathauth.ts
More file actions
114 lines (109 loc) · 3.67 KB
/
Copy pathauth.ts
File metadata and controls
114 lines (109 loc) · 3.67 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
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
import NextAuth from "next-auth";
import { PrismaAdapter } from "@auth/prisma-adapter";
import Google from "next-auth/providers/google";
import Credentials from "next-auth/providers/credentials";
import Email from "next-auth/providers/email";
import bcrypt from "bcryptjs";
import { prisma } from "@/lib/prisma";
import { sendSwimEmail } from "@/lib/email/swim";
import { SUBDOMAIN_APEX } from "@/lib/subdomains";
const authCookieDomain =
process.env.NODE_ENV === "production" ? `.${SUBDOMAIN_APEX}` : undefined;
const sharedAuthCookies = authCookieDomain
? {
sessionToken: { options: { domain: authCookieDomain } },
callbackUrl: { options: { domain: authCookieDomain } },
// __Host-authjs.csrf-token must not set Domain (browser rejects it).
pkceCodeVerifier: { options: { domain: authCookieDomain } },
state: { options: { domain: authCookieDomain } },
}
: undefined;
export const { handlers, auth, signIn, signOut } = NextAuth({
trustHost: true,
adapter: PrismaAdapter(prisma),
...(sharedAuthCookies ? { cookies: sharedAuthCookies } : {}),
providers: [
Google({
clientId: process.env.GOOGLE_CLIENT_ID ?? "",
clientSecret: process.env.GOOGLE_CLIENT_SECRET ?? "",
}),
Email({
server: {
host: "localhost",
port: 25,
auth: { user: "unused", pass: "unused" },
},
from: process.env.SWIM_EMAIL_FROM ?? "meets@utilio.solutions",
sendVerificationRequest: async ({ identifier, url }) => {
await sendSwimEmail({
to: identifier,
subject: "Sign in to Utilio Swim",
text: `Sign in to Utilio Swim: ${url}`,
html: `<p><a href="${url}">Sign in to Utilio Swim</a></p>`,
});
},
}),
Credentials({
credentials: {
email: { label: "Email", type: "email" },
password: { label: "Password", type: "password" },
},
async authorize(credentials) {
const email = credentials?.email as string | undefined;
const password = credentials?.password as string | undefined;
if (!email || !password) return null;
const user = await prisma.user.findUnique({ where: { email } });
if (!user?.password) return null;
const valid = await bcrypt.compare(password, user.password);
if (!valid) return null;
return { id: user.id, email: user.email, name: user.name, image: user.image };
},
}),
],
session: { strategy: "jwt" },
callbacks: {
async redirect({ url, baseUrl }) {
if (url.startsWith("/")) return `${baseUrl}${url}`;
try {
const target = new URL(url);
const base = new URL(baseUrl);
if (
target.hostname === base.hostname ||
target.hostname.endsWith(`.${SUBDOMAIN_APEX}`)
) {
return url;
}
} catch {
// fall through
}
return baseUrl;
},
async jwt({ token, user }) {
if (user?.id) {
token.id = user.id;
}
if (token.id) {
const dbUser = await prisma.user.findUnique({
where: { id: token.id as string },
select: { plan: true },
});
token.plan = dbUser?.plan ?? "free";
}
return token;
},
async session({ session, token }) {
if (session.user && token.id) {
const dbUser = await prisma.user.findUnique({
where: { id: token.id as string },
select: { plan: true },
});
(session.user as { id?: string }).id = token.id as string;
(session.user as { plan?: string }).plan = dbUser?.plan ?? "free";
}
return session;
},
},
pages: {
signIn: "/", // We use a modal, not a dedicated sign-in page
},
});