-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathauth.js
128 lines (112 loc) Β· 3.38 KB
/
auth.js
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
import NextAuth from "next-auth";
import Credentials from "next-auth/providers/credentials";
import { decrypt } from "@/src/libs/session";
import { cookies } from "next/headers";
import { BASE_URL } from "./src/constants/url";
export const { handlers, signIn, signOut, auth } = NextAuth({
pages: {
error: "/error",
},
providers: [
Credentials({
authorize: async (credentials) => {
const cookieStore = await cookies();
if (credentials.token) {
const session = await decrypt(credentials.token);
const user = {
id: session?.memberId,
role: session?.role,
Authorization: credentials.token,
};
return user;
}
cookieStore.delete("email");
cookieStore.delete("name");
cookieStore.delete("birthday");
let body;
console.log(credentials);
if (credentials.memberType === "PARENT") {
body = {
birthday: credentials.birthday,
name: credentials.name,
phone: credentials.phone,
simplePassword: credentials.simplePassword,
email: credentials.email,
social: credentials.social,
memberType: credentials.memberType,
};
} else if (credentials.memberType === "CHILD") {
body = {
birthday: credentials.birthday,
name: credentials.name,
phone: credentials.phone,
email: credentials.email,
social: credentials.social,
memberType: credentials.memberType,
guardianName: credentials.guardianName,
guardianBirthday: credentials.guardianBirthday,
guardianPhone: credentials.guardianPhone,
};
}
const response = await fetch(BASE_URL + "/signup", {
method: "POST",
headers: {
"Content-Type": "application/json",
},
body: JSON.stringify(body),
});
if (!response.ok) {
if (response.status === 409) console.error("μ΄λ―Έ κ°μ
ν νμμ
λλ€.");
else console.error("failed to fetch:", response);
return null;
}
try {
const data = await response.json();
if (data.token) {
const session = await decrypt(data.token);
const user = {
id: session?.memberId,
role: session?.role,
Authorization: data.token,
};
return user;
}
} catch (error) {
console.error("JSON parsing error", error);
}
return null;
},
}),
],
callbacks: {
async jwt({ token, user }) {
if (user) {
token.id = user.id;
token.role = user.role;
token.Authorization = user.Authorization;
}
return token;
},
async session({ session, token }) {
session.user = {
id: token.id,
role: token.role,
Authorization: token.Authorization,
};
return session;
},
async authorized({ auth }) {
if (!auth) {
const cookie = await cookies();
const authorization = cookie.get("Authorization")?.value;
if (authorization) {
return signIn("credentials", {
token: authorization,
redirect: false,
});
}
}
return !!auth;
},
},
});