Skip to content

Commit e0e2422

Browse files
committed
fix: couple of small updates to generateAuthUrl, including auto setting store with nonce and state.
1 parent 375a260 commit e0e2422

File tree

2 files changed

+11
-6
lines changed

2 files changed

+11
-6
lines changed

lib/utils/generateAuthUrl.ts

+10-5
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
import { getActiveStorage, StorageKeys } from "../main";
12
import { IssuerRouteTypes, LoginOptions } from "../types";
23
import { generateRandomString } from "./generateRandomString";
34
import { mapLoginMethodParamsForUrl } from "./mapLoginMethodParamsForUrl";
@@ -14,7 +15,7 @@ export const generateAuthUrl = (
1415
options: LoginOptions,
1516
): { url: URL; state: string; nonce: string } => {
1617
const authUrl = new URL(`${domain}/oauth2/auth`);
17-
18+
const activeStorage = getActiveStorage();
1819
const searchParams: Record<string, string> = {
1920
client_id: options.clientId,
2021
response_type: options.responseType || "code",
@@ -24,19 +25,23 @@ export const generateAuthUrl = (
2425

2526
if (!options.state) {
2627
options.state = generateRandomString(32);
28+
if (activeStorage) {
29+
activeStorage.setSessionItem(StorageKeys.state, options.state);
30+
}
2731
}
2832
searchParams["state"] = options.state;
2933

3034
if (!options.nonce) {
3135
options.nonce = generateRandomString(16);
3236
}
3337
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);
3840
}
3941

42+
searchParams["code_challenge"] = (options.codeChallenge) ? options.codeChallenge : generateRandomString(32);
43+
searchParams["code_challenge_method"] = "S256";
44+
4045
if (options.codeChallengeMethod) {
4146
searchParams["code_challenge_method"] = options.codeChallengeMethod;
4247
}

lib/utils/mapLoginMethodParamsForUrl.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ export const mapLoginMethodParamsForUrl = (
1111
redirect_uri: options.redirectURL
1212
? sanatizeURL(options.redirectURL)
1313
: undefined,
14-
audience: options.audience,
14+
audience: options.audience || "",
1515
scope: options.scope?.join(" ") || "email profile openid offline",
1616
prompt: options.prompt,
1717
lang: options.lang,

0 commit comments

Comments
 (0)