- Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathauth.ts
More file actions
Latest commit
119 lines (98 loc) · 3.33 KB
/
Copy pathauth.ts
File metadata and controls
119 lines (98 loc) · 3.33 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
115
116
117
118
119
importNextAuthfrom"next-auth"
import{PrismaAdapter}from"@auth/prisma-adapter"
importauthConfigfrom"./auth.config"
import{db}from"./lib/db";
import{getAccountByUserId,getUserById}from"@/features/auth/actions";
exportconst{ auth, handlers, signIn, signOut }=NextAuth({
callbacks: {
/**
* Handle user creation and account linking after a successful sign-in
*/
asyncsignIn({ user, account }){
if(!user||!account)returnfalse;
// Check if the user already exists
constexistingUser=awaitdb.user.findUnique({
where: {email: user.email!},
});
// If user does not exist, create a new one
if(!existingUser){
constnewUser=awaitdb.user.create({
data: {
email: user.email!,
name: user.name,
image: user.image,
accounts: {
create: {
type: account.type,
provider: account.provider,
providerAccountId: account.providerAccountId,
refreshToken: account.refresh_token,
accessToken: account.access_token,
expiresAt: account.expires_at,
tokenType: account.token_type,
scope: account.scope,
idToken: account.id_token,
sessionState: account.session_state,
},
},
},
});
if(!newUser)returnfalse;// Return false if user creation fails
}else{
// Link the account if user exists
constexistingAccount=awaitdb.account.findUnique({
where: {
provider_providerAccountId: {
provider: account.provider,
providerAccountId: account.providerAccountId,
},
},
});
// If the account does not exist, create it
if(!existingAccount){
awaitdb.account.create({
data: {
userId: existingUser.id,
type: account.type,
provider: account.provider,
providerAccountId: account.providerAccountId,
refreshToken: account.refresh_token,
accessToken: account.access_token,
expiresAt: account.expires_at,
tokenType: account.token_type,
scope: account.scope,
idToken: account.id_token,
// @ts-ignore
sessionState: account.session_state,
},
});
}
}
returntrue;
},
asyncjwt({ token, user, account }){
if(!token.sub)returntoken;
constexistingUser=awaitgetUserById(token.sub)
if(!existingUser)returntoken;
constexisitingAccount=awaitgetAccountByUserId(existingUser.id);
token.name=existingUser.name;
token.email=existingUser.email;
token.role=existingUser.role;
returntoken;
},
asyncsession({ session, token }){
// Attach the user ID from the token to the session
if(token.sub&&session.user){
session.user.id=token.sub
}
if(token.sub&&session.user){
session.user.role=token.role
}
returnsession;
},
},
secret: process.env.AUTH_SECRET,
adapter: PrismaAdapter(db),
session: {strategy: "jwt"},
...authConfig,
})