@@ -24,50 +24,50 @@ const SignInWithOAuth = () => {
24
24
firstFactorVerification : { externalVerificationRedirectURL } ,
25
25
} = signIn ;
26
26
27
- const result = await AuthSession . startAsync ( {
27
+ if ( ! externalVerificationRedirectURL )
28
+ throw "Something went wrong during the OAuth flow. Try again." ;
29
+
30
+ const authResult = await AuthSession . startAsync ( {
28
31
authUrl : externalVerificationRedirectURL . toString ( ) ,
29
32
returnUrl : redirectUrl ,
30
33
} ) ;
31
34
32
- // eslint-disable-next-line @typescript-eslint/ban-ts-comment
33
- // @ts -ignore
34
- const { type, params } = result || { } ;
35
-
36
- if ( type !== "success" ) {
35
+ if ( authResult . type !== "success" ) {
37
36
throw "Something went wrong during the OAuth flow. Try again." ;
38
37
}
39
38
40
39
// Get the rotatingTokenNonce from the redirect URL parameters
41
- const { rotating_token_nonce : rotatingTokenNonce } = params ;
40
+ const { rotating_token_nonce : rotatingTokenNonce } = authResult . params ;
42
41
43
42
await signIn . reload ( { rotatingTokenNonce } ) ;
44
43
45
44
const { createdSessionId } = signIn ;
46
45
47
- if ( ! createdSessionId ) {
48
- if ( signIn . firstFactorVerification . status === "transferable" ) {
49
- console . log ( "Didn't have an account transferring" ) ;
50
-
51
- await signUp . create ( { transfer : true } ) ;
52
-
53
- const { rotating_token_nonce : rotatingTokenNonce } = params ;
54
-
55
- await signUp . reload ( { rotatingTokenNonce } ) ;
56
-
57
- const { createdSessionId } = signUp ;
58
- if ( ! createdSessionId ) {
59
- throw "Something went wrong during the Sign up OAuth flow. Please ensure that all sign up requirements are met." ;
60
- }
61
- await setSession ( createdSessionId ) ;
62
-
63
- return ;
46
+ if ( createdSessionId ) {
47
+ // If we have a createdSessionId, then auth was successful
48
+ await setSession ( createdSessionId ) ;
49
+ } else {
50
+ // If we have no createdSessionId, then this is a first time sign-in, so
51
+ // we should process this as a signUp instead
52
+ // Throw if we're not in the right state for creating a new user
53
+ if (
54
+ ! signUp ||
55
+ signIn . firstFactorVerification . status !== "transferable"
56
+ ) {
57
+ throw "Something went wrong during the Sign up OAuth flow. Please ensure that all sign up requirements are met." ;
64
58
}
65
- throw "Something went wrong during the Sign in OAuth flow. Please ensure that all sign in requirements are met." ;
66
- }
67
59
68
- await setSession ( createdSessionId ) ;
60
+ console . log (
61
+ "Didn't have an account transferring, following through with new account sign up" ,
62
+ ) ;
69
63
70
- return ;
64
+ // Create user
65
+ await signUp . create ( { transfer : true } ) ;
66
+ await signUp . reload ( {
67
+ rotatingTokenNonce : authResult . params . rotating_token_nonce ,
68
+ } ) ;
69
+ await setSession ( signUp . createdSessionId ) ;
70
+ }
71
71
} catch ( err ) {
72
72
console . log ( JSON . stringify ( err , null , 2 ) ) ;
73
73
console . log ( "error signing in" , err ) ;
0 commit comments