-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathauth.ts
168 lines (128 loc) · 5.22 KB
/
auth.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
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
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
import NextAuth from "next-auth";
import { PrismaAdapter } from "@auth/prisma-adapter";
import authConfig from "@/auth.config";
import { db } from "./lib/db";
import { getUserById } from "./data/user";
import { DEFAULT_ERROR_ADRESS, DEFAULT_LOGIN_ADRESS } from "./routes";
import { getTwoFactorConfirmationByUserId } from "./data/two-factor-confirmation";
import { getAccountByUserId } from "./data/account";
export const {
handlers,
auth,
signIn,
signOut,
unstable_update
} = NextAuth({
// *
pages: {
signIn: DEFAULT_LOGIN_ADRESS,
error: DEFAULT_ERROR_ADRESS,
//signOut: "/auth/logout"
},
// * Bu kısımdaki event, google / github ile giriş yapanlar için çalışacak.
// * Oauth ile giriş yapanların "email-verified" otomatik onaylı olmasını sağlayacak.
events: {
async linkAccount({ user }) {
await db.user.update({
where: { id: user.id },
data: { emailVerified: new Date() }
})
}
},
callbacks: {
// 0 - Önce burada token ile başlıyoruz //* (bu kısımda "user, profile" girdileri undefined dönüyor??)
async jwt({ token }) {
// 0.5 istenmeyen elemanları kaldırma:
if (token) {
//gn* Bu kısımda token ve session içerisinde taşınmasını istemedğimiz kısımları kaldırıyoruz.
const { name, picture, ...restToken } = token;
token = restToken;
}
// 1 - token içerisinde "sub" yok ise direk tokeni dönüyoruz.
if (!token.sub) return token;
// 2 - token içerisinde "sub" var ise sub içerisindeki id ile kullanıcı buluyoruz
const existingUser = await getUserById(token.sub);
// 3 - kullanıcı yok ise direk tokeni dönüyoruz.
if (!existingUser) return token;
// 4 - kullanıcı var ise token içerisine username ekliyoruz.
token.username = existingUser.username;
// 4.5 - Ek Özellik: İki adımlı doğrulama durumunu token içerisine ekleme:
token.isTwoFactorEnabled = existingUser.twoFactorEnabled;
// 4.7 -
token.name = existingUser.name;
token.email = existingUser.email;
// 4.8 - Ek özellik: Kullanıcıya ait hesap bilgilerini token içerisine ekleme:
const existingAccount = await getAccountByUserId(existingUser.id);
token.isOAuth = !!existingAccount;
/*
if (existingAccount) {
token.provider = existingAccount.provider;
token.type = existingAccount.type;
}
*/
// 5 - özelleştirilmiş tokeni dönüyoruz.
return token;
},
// 0 - token ve session ile başlıyoruz.
async session({ token, session }) {
// 0.5 istenmeyen elemanları kaldırma:
if (session.user) {
//gn* Bu kısımda token ve session içerisinde taşınmasını istemedğimiz kısımları kaldırıyoruz.
const { name, image, ...rest } = session.user;
session.user = rest;
}
// 1 - token içerisinde "sub" (id) var ise ve session user var ise session user id'sini token içerisindeki ile set ediyoruz.
if (token.sub && session.user) {
session.user.id = token.sub;
}
// 2 - token içerisinde "username" var ise ve session user var ise session user username'ini token içerisindeki ile set ediyoruz.
if (token.username && session.user) {
session.user.username = token.username as string;
}
// 2.5 - Ek Özellik: İki adımlı doğrulama durumunu tokenden alıp session içerisine ekleme:
if (session.user) {
session.user.twoFactorEnabled = token.isTwoFactorEnabled as boolean;
}
// 2.7 -
if (session.user) {
session.user.name = token.name as string;
session.user.email = token.email as string;
}
// 2.8 -
if (session.user) session.user.isOAuth = token.isOAuth as boolean;
return session;
},
// custom callback:
async signIn({ user, account }) {
// Bu kısımda "user.id" sonuna gelen ünlem zorunluluğu gözden geçirilecek. (NOT)
// * Eposta doğrulamasını diğer providerların hiçbirine eklemiyoruz.
// Allow OAuth without email verification.
if (account?.provider !== "credentials") return true;
const existingUser = await getUserById(user.id!);
// Prevent sign-in without email verification.
if (!existingUser || !existingUser.emailVerified) return false;
if (existingUser.twoFactorEnabled) {
const twoFactorConfirmation = await getTwoFactorConfirmationByUserId(existingUser.id);
if (!twoFactorConfirmation) return false;
// * Delete two factor confirmation for next sign-in. (NOT)
// * Kullanıcının her girişinde 2FA onayını silerek bir sonraki girişinde tekrar isteyecek şekilde çalışıyor.
// * Bunu değiştirerek (TwoFactorConfirmation Schema) içerisinde expires ekleyerek belli bir sürede silinmesini
// * sağlayabiliriz. (Örneğin 1 gün sonra silinmesi gibi)
await db.twoFactorConfirmation.delete({
where: { id: twoFactorConfirmation.id }
});
}
return true;
}
},
adapter: PrismaAdapter(db),
session: { strategy: "jwt" },
...authConfig,
});
/*
if (user.id) {
getUserById(user.id)
} else {
// Handle the case where id is undefined
}
*/