-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathauth.config.ts
88 lines (80 loc) · 2.31 KB
/
auth.config.ts
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
import vercelPostgresAdapter from "@/app/lib/vercelPostgresAdapter";
import type { NextAuthConfig } from "next-auth";
import {} from "next-auth/jwt";
import { AdapterUser } from "next-auth/adapters";
import CredentialsProvider from "next-auth/providers/credentials";
import GoogleProvider from "next-auth/providers/google";
import { z } from "zod";
declare module "next-auth" {
interface User {
isProviderAccount?: boolean;
}
}
declare module "next-auth/jwt" {
interface JWT {
isProviderAccount?: boolean;
}
}
const dbAdapter = vercelPostgresAdapter();
export const authConfig = {
pages: { signIn: "/login" },
adapter: dbAdapter,
session: { strategy: "jwt" },
providers: [
GoogleProvider,
CredentialsProvider({
async authorize(credentials) {
const parsedCredentials = z
.object({ name: z.string().min(1) })
.safeParse(credentials);
if (parsedCredentials.success) {
let user: AdapterUser = {
id: crypto.randomUUID(),
name: parsedCredentials.data.name,
email: crypto.randomUUID(),
emailVerified: null,
};
if (dbAdapter.createUser) {
user = await dbAdapter.createUser(user);
}
return user;
} else {
return null;
}
},
}),
],
callbacks: {
jwt: ({ token, user, profile, trigger }) => {
if (user?.id && profile && trigger === "signIn" && dbAdapter.updateUser) {
const updateUser: Partial<AdapterUser> & Pick<AdapterUser, "id"> = {
id: user.id,
name: profile.name || (user.name as string),
email: profile.email || (user.email as string),
image: profile.picture || user.image,
};
dbAdapter.updateUser(updateUser);
return {
...token,
name: updateUser.name,
email: updateUser.email,
picture: updateUser.image,
isProviderAccount: true,
};
}
return token;
},
session: ({ session, token }) => ({
...session,
sub: token.sub,
user: {
...session.user,
id: token.sub,
name: token.name,
email: token.email,
image: token.picture,
isProviderAccount: token.isProviderAccount,
},
}),
},
} satisfies NextAuthConfig;