-
Notifications
You must be signed in to change notification settings - Fork 0
/
auth.ts
164 lines (148 loc) · 3.89 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
import CredentialProvider from 'next-auth/providers/credentials';
import type {
GetServerSidePropsContext,
NextApiRequest,
NextApiResponse,
} from 'next';
import type { NextAuthOptions } from 'next-auth';
import LineProvider from 'next-auth/providers/line';
import { prisma } from '@/lib/prisma';
import { UserAPI } from '@/lib/apis/UserAPI';
import { getServerSession } from 'next-auth';
export const config = {
session: {
strategy: 'jwt',
},
secret: process.env.SECRET,
providers: [
LineProvider({
clientId: process.env.LINE_CLIENT_ID as string,
clientSecret: process.env.LINE_CLIENT_SECRET as string,
}),
CredentialProvider({
name: 'Sign In',
credentials: {
email: {
label: 'Email',
type: 'email',
placeholder: '[email protected]',
},
password: { label: 'Password', type: 'password' },
},
async authorize(credentials) {
if (!credentials) {
return null;
}
const { user } = await UserAPI.signIn(credentials);
if (user) {
return {
id: user.id,
name: user.name,
email: user.email,
image: user.image,
};
}
return null;
},
}),
],
callbacks: {
async signIn({ user, account, profile, email, credentials }) {
if (!account) {
return false;
}
const { type, provider } = account;
if (type === 'credentials') {
console.log('called signIn');
console.log('Credentials login');
return true;
}
if (type === 'oauth' && provider === 'line') {
const { id, name, image } = user;
console.log('called signIn');
console.log('Line login');
const { user: exists, status } = await UserAPI.isExists(id);
if (!exists) {
await prisma.user.create({
data: {
name,
image,
provider: 'LINE',
providerId: id,
phone_number: '',
permissions: {
create: {},
},
},
include: {
permissions: true,
},
});
}
return true;
}
return false;
},
async jwt({ token, user, account }) {
if (user) {
token.user = user;
}
if (account) {
if (account.type === 'credentials') {
const findUser = await prisma.user.findFirst({
where: {
email: account.id as string,
},
select: {
permissions: {
select: {
can_book: true,
can_add_room: true,
},
},
},
});
if (user) {
token.user.permissions = findUser!.permissions;
}
}
if (account.type === 'oauth' && account.provider === 'line') {
const findUser = await prisma.user.findFirst({
where: {
providerId: account.providerAccountId as string,
},
select: {
id: true,
permissions: {
select: {
can_book: true,
can_add_room: true,
},
},
},
});
if (user) {
token.user.id = findUser!.id;
token.user.permissions = findUser!.permissions;
}
}
token.account = account;
}
return token;
},
async session({ session, token }) {
if (token?.user) {
session.user = token.user;
}
return session;
},
},
} satisfies NextAuthOptions;
export function auth(
...args:
| [GetServerSidePropsContext['req'], GetServerSidePropsContext['res']]
| [NextApiRequest, NextApiResponse]
| []
) {
return getServerSession(...args, config);
}