1
+ import { getActiveStorage , StorageKeys } from "../main" ;
1
2
import { IssuerRouteTypes , LoginOptions } from "../types" ;
2
3
import { generateRandomString } from "./generateRandomString" ;
3
4
import { mapLoginMethodParamsForUrl } from "./mapLoginMethodParamsForUrl" ;
@@ -14,7 +15,7 @@ export const generateAuthUrl = (
14
15
options : LoginOptions ,
15
16
) : { url : URL ; state : string ; nonce : string } => {
16
17
const authUrl = new URL ( `${ domain } /oauth2/auth` ) ;
17
-
18
+ const activeStorage = getActiveStorage ( ) ;
18
19
const searchParams : Record < string , string > = {
19
20
client_id : options . clientId ,
20
21
response_type : options . responseType || "code" ,
@@ -24,19 +25,23 @@ export const generateAuthUrl = (
24
25
25
26
if ( ! options . state ) {
26
27
options . state = generateRandomString ( 32 ) ;
28
+ if ( activeStorage ) {
29
+ activeStorage . setSessionItem ( StorageKeys . state , options . state ) ;
30
+ }
27
31
}
28
32
searchParams [ "state" ] = options . state ;
29
33
30
34
if ( ! options . nonce ) {
31
35
options . nonce = generateRandomString ( 16 ) ;
32
36
}
33
37
searchParams [ "nonce" ] = options . nonce ;
34
-
35
- if ( options . codeChallenge ) {
36
- searchParams [ "code_challenge" ] = options . codeChallenge ;
37
- searchParams [ "code_challenge_method" ] = "S256" ;
38
+ if ( activeStorage ) {
39
+ activeStorage . setSessionItem ( StorageKeys . nonce , options . nonce ) ;
38
40
}
39
41
42
+ searchParams [ "code_challenge" ] = ( options . codeChallenge ) ? options . codeChallenge : generateRandomString ( 32 ) ;
43
+ searchParams [ "code_challenge_method" ] = "S256" ;
44
+
40
45
if ( options . codeChallengeMethod ) {
41
46
searchParams [ "code_challenge_method" ] = options . codeChallengeMethod ;
42
47
}
0 commit comments