-
Notifications
You must be signed in to change notification settings - Fork 19
Expand file tree
/
Copy pathauthOptions.ts
More file actions
185 lines (170 loc) · 5 KB
/
authOptions.ts
File metadata and controls
185 lines (170 loc) · 5 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
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
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
import { JwtPayload, jwtDecode } from 'jwt-decode'
import { Session, SessionOptions, User } from 'next-auth'
import CredentialsProvider from 'next-auth/providers/credentials'
import { JWT } from 'next-auth/jwt'
import { Provider } from 'next-auth/providers/index'
import { apiRoutes } from '@/config/apiRoutes'
import { envConfig } from '@/config/envConfig'
type PasskeyUser = {
userName: string
email?: string
}
interface MyAuthOptions {
providers: Provider[]
callbacks?: {
jwt({ token, user }: { token: JWT; user?: User }): Promise<JWT>
session({
session,
token,
}: {
session: Session
token: JWT
}): Promise<Session>
}
secret?: string
session?: Partial<SessionOptions> | undefined
pages?: { signIn: string }
}
interface jwtDataPayload extends JwtPayload {
email?: string
name?: string
}
export const authOptions: MyAuthOptions = {
providers: [
CredentialsProvider({
name: 'Credentials',
credentials: {
email: {
label: 'Email',
type: 'email',
placeholder: 'email@example.com',
},
password: { label: 'Password', type: 'password' },
isPasskey: { label: 'IsPasskey', type: 'boolean' },
isPassword: { label: 'isPassword', type: 'boolean' },
obj: { label: 'obj', type: 'string' },
verifyAuthenticationObj: {
label: 'verifyAuthenticationObj',
type: 'string',
},
},
async authorize(credentials) {
let parsedVerifyAuthObj: Record<string, unknown> = {}
let parsedObj: PasskeyUser = { userName: '' }
try {
const {
email,
password,
isPasskey,
isPassword,
verifyAuthenticationObj,
obj,
} = credentials || {}
let sanitizedPayload = {}
if (isPassword) {
sanitizedPayload = {
email,
password,
isPasskey,
}
} else {
try {
parsedVerifyAuthObj = JSON.parse(verifyAuthenticationObj || '{}')
parsedObj = JSON.parse(obj || '{}')
} catch (err) {
console.error('Failed to parse incoming JSON strings:', err)
return null
}
sanitizedPayload = {
...parsedVerifyAuthObj,
}
}
// eslint-disable-next-line init-declarations
let res
if (isPassword) {
res = await fetch(
`${envConfig.NEXT_PUBLIC_BASE_URL}${apiRoutes.auth.sinIn}`,
{
method: 'POST',
headers: { 'Content-Type': 'application/json' },
body: JSON.stringify(sanitizedPayload),
},
)
} else {
if (obj) {
res = await fetch(
`${envConfig.NEXT_PUBLIC_BASE_URL}/${apiRoutes.auth.fidoVerifyAuthentication}${parsedObj.userName}`,
{
method: 'POST',
headers: { 'Content-Type': 'application/json' },
body: JSON.stringify(sanitizedPayload),
},
)
}
}
if (!res?.ok) {
console.error('Error fetching user:', res?.statusText)
return null
}
const user = await res.json()
if (user.statusCode === 200 && user.data) {
const decodedToken = jwtDecode<jwtDataPayload>(
user.data.access_token,
)
if (!decodedToken?.email) {
return null
}
return {
id: user.data.session_state || user.data.email,
email: decodedToken?.email,
name: decodedToken?.name || decodedToken?.email,
accessToken: user.data.access_token,
refreshToken: user.data.refresh_token,
tokenType: user.data.token_type,
expiresAt: user.data.expires_in,
}
}
return null
} catch (error) {
console.error('Authorize error:', error)
return null
}
},
}),
],
callbacks: {
async jwt({ token, user }: { token: JWT; user?: User }): Promise<JWT> {
if (user) {
token.id = user.id
token.email = user.email
token.accessToken = user.accessToken || ''
token.expiresAt = user.expiresAt
token.refreshToken = user.refreshToken
}
return token
},
async session({
session,
token,
}: {
session: Session
token: JWT
}): Promise<Session> {
session.user = {
id: token.id as string,
email: token.email as string,
}
session.accessToken = token.accessToken as string
session.refreshToken = token.refreshToken as string
session.expiresAt = token.expiresAt
return session
},
},
secret: process.env.NEXTAUTH_SECRET,
session: {
strategy: 'jwt',
},
pages: {
signIn: '/auth/sign-in',
},
}