1
- import { AccountNotLinkedError } from '../../lib/errors'
2
- import dispatchEvent from '../lib/dispatch-event'
1
+ import { AccountNotLinkedError } from "../../lib/errors"
2
+ import dispatchEvent from "../lib/dispatch-event"
3
+ import adapterErrorHandler from "../../adapters/error-handler"
3
4
4
5
/**
5
6
* This function handles the complex flow of signing users in, and either creating,
@@ -12,20 +13,29 @@ import dispatchEvent from '../lib/dispatch-event'
12
13
* All verification (e.g. OAuth flows or email address verificaiton flows) are
13
14
* done prior to this handler being called to avoid additonal complexity in this
14
15
* handler.
16
+ * @param {import("types").Session } sessionToken
17
+ * @param {import("types").Profile } profile
18
+ * @param {import("types").Account } account
19
+ * @param {import("types/internals").AppOptions } options
15
20
*/
16
- export default async function callbackHandler ( sessionToken , profile , providerAccount , options ) {
21
+ export default async function callbackHandler (
22
+ sessionToken ,
23
+ profile ,
24
+ providerAccount ,
25
+ options
26
+ ) {
17
27
// Input validation
18
- if ( ! profile ) throw new Error ( 'Missing profile' )
19
- if ( ! providerAccount ?. id || ! providerAccount . type ) throw new Error ( 'Missing or invalid provider account' )
20
- if ( ! [ 'email' , 'oauth' ] . includes ( providerAccount . type ) ) throw new Error ( 'Provider not supported' )
28
+ if ( ! profile ) throw new Error ( "Missing profile" )
29
+ if ( ! providerAccount ?. id || ! providerAccount . type )
30
+ throw new Error ( "Missing or invalid provider account" )
31
+ if ( ! [ "email" , "oauth" ] . includes ( providerAccount . type ) )
32
+ throw new Error ( "Provider not supported" )
21
33
22
34
const {
23
35
adapter,
24
36
jwt,
25
37
events,
26
- session : {
27
- jwt : useJwtSession
28
- }
38
+ session : { jwt : useJwtSession } ,
29
39
} = options
30
40
31
41
// If no adapter is configured then we don't have a database and cannot
@@ -34,7 +44,7 @@ export default async function callbackHandler (sessionToken, profile, providerAc
34
44
return {
35
45
user : profile ,
36
46
account : providerAccount ,
37
- session : { }
47
+ session : { } ,
38
48
}
39
49
}
40
50
@@ -47,8 +57,8 @@ export default async function callbackHandler (sessionToken, profile, providerAc
47
57
linkAccount,
48
58
createSession,
49
59
getSession,
50
- deleteSession
51
- } = await adapter . getAdapter ( options )
60
+ deleteSession,
61
+ } = adapterErrorHandler ( await adapter . getAdapter ( options ) , options . logger )
52
62
53
63
let session = null
54
64
let user = null
@@ -74,9 +84,11 @@ export default async function callbackHandler (sessionToken, profile, providerAc
74
84
}
75
85
}
76
86
77
- if ( providerAccount . type === ' email' ) {
87
+ if ( providerAccount . type === " email" ) {
78
88
// If signing in with an email, check if an account with the same email address exists already
79
- const userByEmail = profile . email ? await getUserByEmail ( profile . email ) : null
89
+ const userByEmail = profile . email
90
+ ? await getUserByEmail ( profile . email )
91
+ : null
80
92
if ( userByEmail ) {
81
93
// If they are not already signed in as the same user, this flow will
82
94
// sign them out of the current session and sign them in as the new user
@@ -107,11 +119,14 @@ export default async function callbackHandler (sessionToken, profile, providerAc
107
119
return {
108
120
session,
109
121
user,
110
- isNewUser
122
+ isNewUser,
111
123
}
112
- } else if ( providerAccount . type === ' oauth' ) {
124
+ } else if ( providerAccount . type === " oauth" ) {
113
125
// If signing in with oauth account, check to see if the account exists already
114
- const userByProviderAccountId = await getUserByProviderAccountId ( providerAccount . provider , providerAccount . id )
126
+ const userByProviderAccountId = await getUserByProviderAccountId (
127
+ providerAccount . provider ,
128
+ providerAccount . id
129
+ )
115
130
if ( userByProviderAccountId ) {
116
131
if ( isSignedIn ) {
117
132
// If the user is already signed in with this account, we don't need to do anything
@@ -122,7 +137,7 @@ export default async function callbackHandler (sessionToken, profile, providerAc
122
137
return {
123
138
session,
124
139
user,
125
- isNewUser
140
+ isNewUser,
126
141
}
127
142
}
128
143
// If the user is currently signed in, but the new account they are signing in
@@ -132,11 +147,13 @@ export default async function callbackHandler (sessionToken, profile, providerAc
132
147
}
133
148
// If there is no active session, but the account being signed in with is already
134
149
// associated with a valid user then create session to sign the user in.
135
- session = useJwtSession ? { } : await createSession ( userByProviderAccountId )
150
+ session = useJwtSession
151
+ ? { }
152
+ : await createSession ( userByProviderAccountId )
136
153
return {
137
154
session,
138
155
user : userByProviderAccountId ,
139
- isNewUser
156
+ isNewUser,
140
157
}
141
158
} else {
142
159
if ( isSignedIn ) {
@@ -151,13 +168,16 @@ export default async function callbackHandler (sessionToken, profile, providerAc
151
168
providerAccount . accessToken ,
152
169
providerAccount . accessTokenExpires
153
170
)
154
- await dispatchEvent ( events . linkAccount , { user, providerAccount : providerAccount } )
171
+ await dispatchEvent ( events . linkAccount , {
172
+ user,
173
+ providerAccount : providerAccount ,
174
+ } )
155
175
156
176
// As they are already signed in, we don't need to do anything after linking them
157
177
return {
158
178
session,
159
179
user,
160
- isNewUser
180
+ isNewUser,
161
181
}
162
182
}
163
183
@@ -178,7 +198,9 @@ export default async function callbackHandler (sessionToken, profile, providerAc
178
198
//
179
199
// OAuth providers should require email address verification to prevent this, but in
180
200
// practice that is not always the case; this helps protect against that.
181
- const userByEmail = profile . email ? await getUserByEmail ( profile . email ) : null
201
+ const userByEmail = profile . email
202
+ ? await getUserByEmail ( profile . email )
203
+ : null
182
204
if ( userByEmail ) {
183
205
// We end up here when we don't have an account with the same [provider].id *BUT*
184
206
// we do already have an account with the same email address as the one in the
@@ -207,14 +229,17 @@ export default async function callbackHandler (sessionToken, profile, providerAc
207
229
providerAccount . accessToken ,
208
230
providerAccount . accessTokenExpires
209
231
)
210
- await dispatchEvent ( events . linkAccount , { user, providerAccount : providerAccount } )
232
+ await dispatchEvent ( events . linkAccount , {
233
+ user,
234
+ providerAccount : providerAccount ,
235
+ } )
211
236
212
237
session = useJwtSession ? { } : await createSession ( user )
213
238
isNewUser = true
214
239
return {
215
240
session,
216
241
user,
217
- isNewUser
242
+ isNewUser,
218
243
}
219
244
}
220
245
}
0 commit comments