@@ -12,7 +12,10 @@ import {
12
12
13
13
import '../utils/oauth/enableOAuthListener' ;
14
14
import { cognitoHostedUIIdentityProviderMap } from '../types/models' ;
15
- import { getAuthUserAgentValue , openAuthSession } from '../../../utils' ;
15
+ import {
16
+ openAuthSession as _openAuthSession ,
17
+ getAuthUserAgentValue ,
18
+ } from '../../../utils' ;
16
19
import { assertUserNotAuthenticated } from '../utils/signInHelpers' ;
17
20
import { SignInWithRedirectInput } from '../types' ;
18
21
import {
@@ -25,6 +28,7 @@ import {
25
28
} from '../utils/oauth' ;
26
29
import { createOAuthError } from '../utils/oauth/createOAuthError' ;
27
30
import { listenForOAuthFlowCancellation } from '../utils/oauth/cancelOAuthFlow' ;
31
+ import { OpenAuthSession } from '../../../utils/types' ;
28
32
29
33
/**
30
34
* Signs in a user with OAuth. Redirects the application to an Identity Provider.
@@ -62,6 +66,7 @@ export async function signInWithRedirect(
62
66
lang : input ?. options ?. lang ,
63
67
nonce : input ?. options ?. nonce ,
64
68
} ,
69
+ authSessionOpener : input ?. options ?. authSessionOpener ,
65
70
} ) ;
66
71
}
67
72
@@ -72,17 +77,20 @@ const oauthSignIn = async ({
72
77
customState,
73
78
preferPrivateSession,
74
79
options,
80
+ authSessionOpener,
75
81
} : {
76
82
oauthConfig : OAuthConfig ;
77
83
provider : string ;
78
84
clientId : string ;
79
85
customState ?: string ;
80
86
preferPrivateSession ?: boolean ;
81
87
options ?: SignInWithRedirectInput [ 'options' ] ;
88
+ authSessionOpener ?: OpenAuthSession ;
82
89
} ) => {
83
90
const { domain, redirectSignIn, responseType, scopes } = oauthConfig ;
84
91
const { loginHint, lang, nonce } = options ?? { } ;
85
92
const randomState = generateState ( ) ;
93
+ const openAuthSession = authSessionOpener || _openAuthSession ;
86
94
87
95
/* encodeURIComponent is not URL safe, use urlSafeEncode instead. Cognito
88
96
single-encodes/decodes url on first sign in and double-encodes/decodes url
@@ -101,27 +109,24 @@ const oauthSignIn = async ({
101
109
oAuthStore . storeOAuthState ( state ) ;
102
110
oAuthStore . storePKCE ( value ) ;
103
111
104
- const queryString = Object . entries ( {
105
- redirect_uri : redirectUri ,
106
- response_type : responseType ,
107
- client_id : clientId ,
108
- identity_provider : provider ,
109
- scope : scopes . join ( ' ' ) ,
110
- // eslint-disable-next-line camelcase
111
- ...( loginHint && { login_hint : loginHint } ) ,
112
- ...( lang && { lang } ) ,
113
- ...( nonce && { nonce } ) ,
114
- state,
115
- ...( responseType === 'code' && {
116
- code_challenge : toCodeChallenge ( ) ,
117
- code_challenge_method : method ,
118
- } ) ,
119
- } )
120
- . map ( ( [ k , v ] ) => `${ encodeURIComponent ( k ) } =${ encodeURIComponent ( v ) } ` )
121
- . join ( '&' ) ;
112
+ const params = new URLSearchParams ( [
113
+ [ 'redirect_uri' , redirectUri ] ,
114
+ [ 'response_type' , responseType ] ,
115
+ [ 'client_id' , clientId ] ,
116
+ [ 'identity_provider' , provider ] ,
117
+ [ 'scope' , scopes . join ( ' ' ) ] ,
118
+ ] ) ;
122
119
123
- // TODO(v6): use URL object instead
124
- const oAuthUrl = `https://${ domain } /oauth2/authorize?${ queryString } ` ;
120
+ loginHint && params . append ( 'login_hint' , loginHint ) ;
121
+ lang && params . append ( 'lang' , lang ) ;
122
+ nonce && params . append ( 'nonce' , nonce ) ;
123
+ params . append ( 'state' , state ) ;
124
+ if ( responseType === 'code' ) {
125
+ params . append ( 'code_challenge' , toCodeChallenge ( ) ) ;
126
+ params . append ( 'code_challenge_method' , method ) ;
127
+ }
128
+ const oAuthUrl = new URL ( '/oauth2/authorize' , `https://${ domain } /` ) ;
129
+ oAuthUrl . search = params . toString ( ) ;
125
130
126
131
// this will only take effect in the following scenarios:
127
132
// 1. the user cancels the OAuth flow on web via back button, and
@@ -130,8 +135,11 @@ const oauthSignIn = async ({
130
135
131
136
// the following is effective only in react-native as openAuthSession resolves only in react-native
132
137
const { type, error, url } =
133
- ( await openAuthSession ( oAuthUrl , redirectSignIn , preferPrivateSession ) ) ??
134
- { } ;
138
+ ( await openAuthSession (
139
+ oAuthUrl . href ,
140
+ redirectSignIn ,
141
+ preferPrivateSession ,
142
+ ) ) ?? { } ;
135
143
136
144
try {
137
145
if ( type === 'error' ) {
0 commit comments