@@ -9,26 +9,53 @@ import { cookies } from "next/headers";
9
9
import { redirect } from "next/navigation" ;
10
10
import type { NextRequest } from "next/server" ;
11
11
12
- import { NextAuth , NextAuthSession , type NextAuthOptions } from "../shared" ;
12
+ import {
13
+ NextAuth ,
14
+ NextAuthSession ,
15
+ type NextAuthOptions ,
16
+ BuiltinProviderNames ,
17
+ } from "../shared" ;
13
18
14
- export { NextAuthSession , type NextAuthOptions } ;
19
+ export {
20
+ NextAuthSession ,
21
+ type NextAuthOptions ,
22
+ type BuiltinProviderNames ,
23
+ type TokenData ,
24
+ } ;
15
25
16
- type ParamsOrError < Result extends object > =
17
- | ( { error : null } & Result )
18
- | ( { error : Error } & { [ Key in keyof Result ] ?: undefined } ) ;
26
+ type ParamsOrError < Result extends object , ErrorDetails extends object = { } > =
27
+ | ( { error : null } & { [ Key in keyof ErrorDetails ] ?: undefined } & Result )
28
+ | ( { error : Error } & ErrorDetails & { [ Key in keyof Result ] ?: undefined } ) ;
19
29
20
30
export interface CreateAuthRouteHandlers {
21
31
onOAuthCallback (
22
- params : ParamsOrError < { tokenData : TokenData ; isSignUp : boolean } >
32
+ params : ParamsOrError < {
33
+ tokenData : TokenData ;
34
+ provider : BuiltinOAuthProviderNames ;
35
+ isSignUp : boolean ;
36
+ } >
23
37
) : void ;
24
38
onEmailPasswordSignIn ( params : ParamsOrError < { tokenData : TokenData } > ) : void ;
25
39
onEmailPasswordSignUp (
26
40
params : ParamsOrError < { tokenData : TokenData | null } >
27
41
) : void ;
28
42
onEmailPasswordReset ( params : ParamsOrError < { tokenData : TokenData } > ) : void ;
29
- onEmailVerify ( params : ParamsOrError < { tokenData : TokenData } > ) : void ;
43
+ onEmailVerify (
44
+ params : ParamsOrError <
45
+ { tokenData : TokenData } ,
46
+ { verificationToken ?: string }
47
+ >
48
+ ) : void ;
30
49
onBuiltinUICallback (
31
- params : ParamsOrError < { tokenData : TokenData | null ; isSignUp : boolean } >
50
+ params : ParamsOrError <
51
+ (
52
+ | {
53
+ tokenData : TokenData ;
54
+ provider : BuiltinProviderNames ;
55
+ }
56
+ | { tokenData : null ; provider : null }
57
+ ) & { isSignUp : boolean }
58
+ >
32
59
) : void ;
33
60
onSignout ( ) : void ;
34
61
}
@@ -48,6 +75,10 @@ export class NextAppAuth extends NextAuth {
48
75
) ;
49
76
}
50
77
78
+ async getProvidersInfo ( ) {
79
+ return ( await this . core ) . getProvidersInfo ( ) ;
80
+ }
81
+
51
82
createAuthRouteHandlers ( {
52
83
onOAuthCallback,
53
84
onEmailPasswordSignIn,
@@ -137,7 +168,14 @@ export class NextAppAuth extends NextAuth {
137
168
} ) ;
138
169
cookies ( ) . delete ( this . options . pkceVerifierCookieName ) ;
139
170
140
- return onOAuthCallback ( { error : null , tokenData, isSignUp } ) ;
171
+ return onOAuthCallback ( {
172
+ error : null ,
173
+ tokenData,
174
+ provider : req . nextUrl . searchParams . get (
175
+ "provider"
176
+ ) as BuiltinOAuthProviderNames ,
177
+ isSignUp,
178
+ } ) ;
141
179
}
142
180
case "emailpassword/verify" : {
143
181
if ( ! onEmailVerify ) {
@@ -158,6 +196,7 @@ export class NextAppAuth extends NextAuth {
158
196
if ( ! verifier ) {
159
197
return onEmailVerify ( {
160
198
error : new Error ( "no pkce verifier cookie found" ) ,
199
+ verificationToken,
161
200
} ) ;
162
201
}
163
202
let tokenData : TokenData ;
@@ -168,6 +207,7 @@ export class NextAppAuth extends NextAuth {
168
207
} catch ( err ) {
169
208
return onEmailVerify ( {
170
209
error : err instanceof Error ? err : new Error ( String ( err ) ) ,
210
+ verificationToken,
171
211
} ) ;
172
212
}
173
213
cookies ( ) . set ( {
@@ -203,6 +243,7 @@ export class NextAppAuth extends NextAuth {
203
243
return onBuiltinUICallback ( {
204
244
error : null ,
205
245
tokenData : null ,
246
+ provider : null ,
206
247
isSignUp : true ,
207
248
} ) ;
208
249
}
@@ -236,7 +277,14 @@ export class NextAppAuth extends NextAuth {
236
277
} ) ;
237
278
cookies ( ) . delete ( this . options . pkceVerifierCookieName ) ;
238
279
239
- return onBuiltinUICallback ( { error : null , tokenData, isSignUp } ) ;
280
+ return onBuiltinUICallback ( {
281
+ error : null ,
282
+ tokenData,
283
+ provider : req . nextUrl . searchParams . get (
284
+ "provider"
285
+ ) as BuiltinProviderNames ,
286
+ isSignUp,
287
+ } ) ;
240
288
}
241
289
case "builtin/signin" :
242
290
case "builtin/signup" : {
@@ -328,6 +376,12 @@ export class NextAppAuth extends NextAuth {
328
376
error : err instanceof Error ? err : new Error ( String ( err ) ) ,
329
377
} ) ;
330
378
}
379
+ cookies ( ) . set ( {
380
+ name : this . options . pkceVerifierCookieName ,
381
+ value : result . verifier ,
382
+ httpOnly : true ,
383
+ sameSite : "strict" ,
384
+ } ) ;
331
385
if ( result . status === "complete" ) {
332
386
cookies ( ) . set ( {
333
387
name : this . options . authCookieName ,
@@ -340,18 +394,12 @@ export class NextAppAuth extends NextAuth {
340
394
tokenData : result . tokenData ,
341
395
} ) ;
342
396
} else {
343
- cookies ( ) . set ( {
344
- name : this . options . pkceVerifierCookieName ,
345
- value : result . verifier ,
346
- httpOnly : true ,
347
- sameSite : "strict" ,
348
- } ) ;
349
397
return onEmailPasswordSignUp ( { error : null , tokenData : null } ) ;
350
398
}
351
399
}
352
400
case "emailpassword/send-reset-email" : {
353
- if ( ! this . options . passwordResetUrl ) {
354
- throw new Error ( `'passwordResetUrl ' option not configured` ) ;
401
+ if ( ! this . options . passwordResetPath ) {
402
+ throw new Error ( `'passwordResetPath ' option not configured` ) ;
355
403
}
356
404
const [ email ] = _extractParams (
357
405
await _getReqBody ( req ) ,
@@ -360,7 +408,13 @@ export class NextAppAuth extends NextAuth {
360
408
) ;
361
409
const { verifier } = await (
362
410
await this . core
363
- ) . sendPasswordResetEmail ( email , this . options . passwordResetUrl ) ;
411
+ ) . sendPasswordResetEmail (
412
+ email ,
413
+ new URL (
414
+ this . options . passwordResetPath ,
415
+ this . options . baseUrl
416
+ ) . toString ( )
417
+ ) ;
364
418
cookies ( ) . set ( {
365
419
name : this . options . pkceVerifierCookieName ,
366
420
value : verifier ,
@@ -465,6 +519,12 @@ export class NextAppAuth extends NextAuth {
465
519
password ,
466
520
`${ this . _authRoute } /emailpassword/verify`
467
521
) ;
522
+ cookies ( ) . set ( {
523
+ name : this . options . pkceVerifierCookieName ,
524
+ value : result . verifier ,
525
+ httpOnly : true ,
526
+ sameSite : "strict" ,
527
+ } ) ;
468
528
if ( result . status === "complete" ) {
469
529
cookies ( ) . set ( {
470
530
name : this . options . authCookieName ,
@@ -473,28 +533,24 @@ export class NextAppAuth extends NextAuth {
473
533
sameSite : "strict" ,
474
534
} ) ;
475
535
return result . tokenData ;
476
- } else {
477
- cookies ( ) . set ( {
478
- name : this . options . pkceVerifierCookieName ,
479
- value : result . verifier ,
480
- httpOnly : true ,
481
- sameSite : "strict" ,
482
- } ) ;
483
- return null ;
484
536
}
537
+ return null ;
485
538
} ,
486
539
emailPasswordSendPasswordResetEmail : async (
487
540
data : FormData | { email : string }
488
541
) => {
489
- if ( ! this . options . passwordResetUrl ) {
490
- throw new Error ( `'passwordResetUrl ' option not configured` ) ;
542
+ if ( ! this . options . passwordResetPath ) {
543
+ throw new Error ( `'passwordResetPath ' option not configured` ) ;
491
544
}
492
545
const [ email ] = _extractParams ( data , [ "email" ] , "email missing" ) ;
493
546
const { verifier } = await (
494
547
await this . core
495
548
) . sendPasswordResetEmail (
496
549
email ,
497
- `${ this . options . baseUrl } /${ this . options . passwordResetUrl } `
550
+ new URL (
551
+ this . options . passwordResetPath ,
552
+ this . options . baseUrl
553
+ ) . toString ( )
498
554
) ;
499
555
cookies ( ) . set ( {
500
556
name : this . options . pkceVerifierCookieName ,
@@ -504,7 +560,7 @@ export class NextAppAuth extends NextAuth {
504
560
} ) ;
505
561
} ,
506
562
emailPasswordResetPassword : async (
507
- data : FormData | { resetToken : string ; password : string }
563
+ data : FormData | { reset_token : string ; password : string }
508
564
) => {
509
565
const verifier = cookies ( ) . get (
510
566
this . options . pkceVerifierCookieName
0 commit comments