1
1
import type { ClerkAPIResponseError } from '@clerk/shared/error' ;
2
2
import { isClerkAPIResponseError } from '@clerk/shared/error' ;
3
+ import { joinURL } from '@clerk/shared/url' ;
3
4
import type {
4
5
LoadedClerk ,
5
6
OAuthStrategy ,
@@ -41,12 +42,14 @@ export interface SignInMachineContext extends MachineContext {
41
42
resource : SignInResource | null ;
42
43
router : ClerkRouter ;
43
44
thirdPartyProviders : EnabledThirdPartyProviders ;
45
+ signUpPath : string ;
44
46
}
45
47
46
48
export interface SignInMachineInput {
47
49
clerk : LoadedClerk ;
48
50
form : ActorRefFrom < typeof FormMachine > ;
49
51
router : ClerkRouter ;
52
+ signUpPath : string ;
50
53
}
51
54
52
55
export type SignInMachineTags = 'state:start' | 'state:first-factor' | 'state:second-factor' | 'external' ;
@@ -115,7 +118,11 @@ export const SignInMachine = setup({
115
118
console . error ( event . error ) ;
116
119
} ,
117
120
navigateTo ( { context } , { path } : { path : string } ) {
118
- context . router . replace ( path ) ;
121
+ const resolvedPath = joinURL ( context . router . basePath , path ) ;
122
+ context . router . replace ( resolvedPath ) ;
123
+ } ,
124
+ navigateToSignUp ( { context } ) {
125
+ context . router . push ( context . signUpPath ) ;
119
126
} ,
120
127
raiseFailure : raise ( ( { event } ) => {
121
128
assertActorEventError ( event ) ;
@@ -143,8 +150,7 @@ export const SignInMachine = setup({
143
150
isCurrentFactorPassword : ( { context } ) => context . currentFactor ?. strategy === 'password' ,
144
151
isCurrentFactorTOTP : ( { context } ) => context . currentFactor ?. strategy === 'totp' ,
145
152
isCurrentPath : ( { context } , params : { path : string } ) => {
146
- const path = params ?. path ;
147
- return path ? context . router . pathname ( ) === path : false ;
153
+ return context . router . match ( params ?. path ) ;
148
154
} ,
149
155
isLoggedIn : ( { context } ) => Boolean ( context . clerk . user ) ,
150
156
isSignInComplete : ( { context } ) => context ?. resource ?. status === 'complete' ,
@@ -179,6 +185,7 @@ export const SignInMachine = setup({
179
185
resource : null ,
180
186
router : input . router ,
181
187
thirdPartyProviders : getEnabledThirdPartyProviders ( input . clerk . __unstable__environment ) ,
188
+ signUpPath : input . signUpPath ,
182
189
} ) ,
183
190
initial : 'Init' ,
184
191
on : {
@@ -198,22 +205,22 @@ export const SignInMachine = setup({
198
205
} ,
199
206
{
200
207
description : 'If the SignIn resource is empty, invoke the sign-in start flow' ,
201
- guard : or ( [ not ( 'hasSignInResource' ) , { type : 'isCurrentPath' , params : { path : '/sign-in ' } } ] ) ,
208
+ guard : or ( [ not ( 'hasSignInResource' ) , { type : 'isCurrentPath' , params : { path : '/' } } ] ) ,
202
209
target : 'Start' ,
203
210
} ,
204
211
{
205
212
description : 'Go to FirstFactor flow state' ,
206
- guard : and ( [ 'needsFirstFactor' , { type : 'isCurrentPath' , params : { path : '/sign-in/ continue' } } ] ) ,
213
+ guard : and ( [ 'needsFirstFactor' , { type : 'isCurrentPath' , params : { path : '/continue' } } ] ) ,
207
214
target : 'FirstFactor' ,
208
215
} ,
209
216
{
210
217
description : 'Go to SecondFactor flow state' ,
211
- guard : and ( [ 'needsSecondFactor' , { type : 'isCurrentPath' , params : { path : '/sign-in/ continue' } } ] ) ,
218
+ guard : and ( [ 'needsSecondFactor' , { type : 'isCurrentPath' , params : { path : '/continue' } } ] ) ,
212
219
target : 'SecondFactor' ,
213
220
} ,
214
221
{
215
222
description : 'Go to SSO Callback state' ,
216
- guard : { type : 'isCurrentPath' , params : { path : '/sign-in/ sso-callback' } } ,
223
+ guard : { type : 'isCurrentPath' , params : { path : '/sso-callback' } } ,
217
224
target : 'SSOCallback' ,
218
225
} ,
219
226
{
@@ -241,12 +248,20 @@ export const SignInMachine = setup({
241
248
Start : {
242
249
id : 'Start' ,
243
250
tags : 'state:start' ,
244
- description : 'The intial state of the sign-in flow.' ,
251
+ description : 'The initial state of the sign-in flow.' ,
245
252
initial : 'AwaitingInput' ,
246
253
on : {
247
254
'AUTHENTICATE.OAUTH' : '#SignIn.AuthenticatingWithRedirect' ,
248
255
'AUTHENTICATE.SAML' : '#SignIn.AuthenticatingWithRedirect' ,
249
256
} ,
257
+ entry : [
258
+ {
259
+ type : 'navigateTo' ,
260
+ params : {
261
+ path : '/' ,
262
+ } ,
263
+ } ,
264
+ ] ,
250
265
onDone : [
251
266
{
252
267
guard : 'isSignInComplete' ,
@@ -297,7 +312,7 @@ export const SignInMachine = setup({
297
312
FirstFactor : {
298
313
tags : 'state:first-factor' ,
299
314
initial : 'DeterminingState' ,
300
- entry : ' assignStartingFirstFactor',
315
+ entry : [ { type : 'navigateTo' , params : { path : '/continue' } } , ' assignStartingFirstFactor'] ,
301
316
onDone : [
302
317
{
303
318
guard : 'isSignInComplete' ,
@@ -385,7 +400,7 @@ export const SignInMachine = setup({
385
400
SecondFactor : {
386
401
tags : 'state:second-factor' ,
387
402
initial : 'DeterminingState' ,
388
- entry : ' assignStartingSecondFactor',
403
+ entry : [ { type : 'navigateTo' , params : { path : '/continue' } } , ' assignStartingSecondFactor'] ,
389
404
onDone : [
390
405
{
391
406
guard : 'isSignInComplete' ,
@@ -507,48 +522,24 @@ export const SignInMachine = setup({
507
522
'CLERKJS.NAVIGATE.RESET_PASSWORD' : '#SignIn.NotImplemented' ,
508
523
'CLERKJS.NAVIGATE.SIGN_IN' : {
509
524
actions : [
510
- log ( 'Navigating to sign in' ) ,
525
+ log ( 'Navigating to sign in root ' ) ,
511
526
{
512
527
type : 'navigateTo' ,
513
528
params : {
514
- path : '/sign-in ' ,
529
+ path : '/' ,
515
530
} ,
516
531
} ,
517
532
] ,
518
533
} ,
519
534
'CLERKJS.NAVIGATE.SIGN_UP' : {
520
- actions : [
521
- log ( 'Navigating to sign in' ) ,
522
- {
523
- type : 'navigateTo' ,
524
- params : {
525
- path : '/sign-up' ,
526
- } ,
527
- } ,
528
- ] ,
535
+ actions : [ log ( 'Navigating to sign up' ) , 'navigateToSignUp' ] ,
529
536
} ,
530
537
'CLERKJS.NAVIGATE.VERIFICATION' : {
531
- actions : [
532
- log ( 'Navigating to sign in' ) ,
533
- {
534
- type : 'navigateTo' ,
535
- params : {
536
- path : '/sign-up' ,
537
- } ,
538
- } ,
539
- ] ,
538
+ actions : [ log ( 'Navigating to sign in' ) , 'navigateToSignUp' ] ,
540
539
} ,
541
540
'CLERKJS.NAVIGATE.CONTINUE' : {
542
541
description : 'Redirect to the sign-up flow' ,
543
- actions : [
544
- log ( 'Navigating to sign up' ) ,
545
- {
546
- type : 'navigateTo' ,
547
- params : {
548
- path : '/sign-up' ,
549
- } ,
550
- } ,
551
- ] ,
542
+ actions : [ log ( 'Navigating to sign up' ) , 'navigateToSignUp' ] ,
552
543
} ,
553
544
'CLERKJS.NAVIGATE.*' : {
554
545
target : '#SignIn.Start' ,
0 commit comments