-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathauth_options.ts
147 lines (130 loc) · 4.39 KB
/
auth_options.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
import { compare } from 'bcrypt'
import { NextAuthOptions } from 'next-auth'
import CredentialsProvider from 'next-auth/providers/credentials'
import GoogleProvider from 'next-auth/providers/google'
import prisma from '@/db'
const GOOGLE_CLIENT_ID = process.env.GOOGLE_CLIENT_ID!
const GOOGLE_CLIENT_SECRET = process.env.GOOGLE_CLIENT_SECRET!
export const authOptions: NextAuthOptions = {
session: {
strategy: 'jwt'
},
providers: [
CredentialsProvider({
name: 'Sign in',
credentials: {
email: {
label: 'Email',
type: 'email',
placeholder: '[email protected]'
},
password: { label: 'Password', type: 'password'}
},
async authorize(credentials) {
if(!credentials?.email || !credentials.password) {
throw new Error('Invalid credentials')
}
const user = await prisma.user.findUnique({
where: {
email: credentials.email
}
});
if(!user){
throw new Error('Invalid credentials')
}
if(!await compare(
credentials.password,
user.password!
)) { throw new Error('Invalid credentials') }
if(!user.active){
throw new Error('Please activate your account')
}
return {
id: user.id + '',
email: user.email,
username: user.username,
name: user.name
}
}
}),
GoogleProvider(
{ clientId: GOOGLE_CLIENT_ID,
clientSecret: GOOGLE_CLIENT_SECRET },
)
],
callbacks: {
async signIn({ account, profile }) {
if(account?.provider == 'credentials'){
return true
}
if(!profile?.email){
throw new Error('No profile')
}
// generates a random username for people using google login
// generates a new username if by chance it generates one that already exists
// todo: do something less janky here.
let username = profile!.name!
while(true){
if( // break if username is unique
!await prisma.user.findFirst({
where: {
username: username
}
})
) { break }
username += Math.floor(Math.random() * 10)
}
await prisma.user.upsert({
where: {
email: profile.email,
},
create: {
email: profile.email,
name: profile.name,
username: username,
credentialsProvider: false
},
update: {
name: profile.name
}
})
return true
},
session: ({ session, token }) => {
return {
...session,
user: {
...session.user,
id: token.id,
username: token.username
}
}
},
jwt: async ({ token, user, account }) => {
if(user && account && account.provider == "google"){ // if googleprovider, get the real ID and Username from the database before returning jwt
const realUser = await prisma.user.findUnique({
where: {
email: user.email!
}
})
if(realUser){
user.id = realUser.id.toString()
user.username = realUser.username
}
}
if(user){
return {
...token,
id: user.id,
username: user.username
}
}
return token
}
},
pages: {
signIn: '/login',
error: '/login'
},
debug: true
}