Skip to content

Commit 7f04382

Browse files
committed
fixeed browser scope bugs
1 parent 4c6c20a commit 7f04382

5 files changed

Lines changed: 35 additions & 22 deletions

File tree

src/lib/authentication.ts

Lines changed: 20 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -7,19 +7,27 @@ export interface Tokens {
77
idToken: string | undefined;
88
refreshToken: string | undefined;
99
}
10-
const cognitoAuthConfig = {
11-
authority: "https://cognito-idp.us-west-2.amazonaws.com/us-west-2_lg1qptg2n",
12-
client_id: "4d6esoka62s46lo4d398o3sqpi",
13-
redirect_uri: window.location.origin + "/auth/callback",
14-
response_type: "code",
15-
scope: "aws.cognito.signin.user.admin email openid phone profile"
16-
};
1710

18-
export const userManager = new UserManager({
19-
...cognitoAuthConfig
20-
});
11+
12+
let userManager: UserManager | null = null;
13+
export function createUserManager(origin: String) {
14+
if (userManager) {
15+
return userManager;
16+
}
17+
const cognitoAuthConfig = {
18+
authority: "https://cognito-idp.us-west-2.amazonaws.com/us-west-2_lg1qptg2n",
19+
client_id: "4d6esoka62s46lo4d398o3sqpi",
20+
redirect_uri: origin + "/auth/callback",
21+
response_type: "code",
22+
scope: "aws.cognito.signin.user.admin email openid phone profile"
23+
};
24+
userManager = new UserManager({
25+
...cognitoAuthConfig
26+
});
27+
return userManager;
28+
}
2129

2230

23-
export async function signinRequest() {
24-
await userManager.signinRedirect();
31+
export async function signinRequest(url: URL) {
32+
await (createUserManager(url.origin)).signinRedirect();
2533
}

src/lib/state.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -176,7 +176,7 @@ export async function initializeTooling() {
176176
label: 'Log in',
177177
onClick: () => {
178178
// Attempt a fresh sign-in redirect
179-
try { signinRequest(); } catch {}
179+
try { signinRequest(new URL(window.location.href)); } catch {}
180180
}
181181
},
182182
{

src/routes/auth/callback/+page.ts

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,11 @@ import { redirect } from '@sveltejs/kit';
33
export const ssr = false;
44
export const csr = true;
55

6-
import { userManager } from '$lib/authentication.js';
6+
import { createUserManager } from '$lib/authentication.js';
77
import { SessionState } from '$lib/state.js';
8+
import { create } from 'domain';
9+
import { page } from '$app/state';
10+
import { UserManager } from 'oidc-client-ts';
811

912
const TOKEN_STORAGE_KEY = 'ccported_tokens';
1013

@@ -17,9 +20,10 @@ type StoredTokens = {
1720

1821
export async function load() {
1922
// This runs in the browser only (ssr=false)
23+
const usernManager = createUserManager(page.url.origin);
2024
try {
2125
// Complete the sign-in redirect flow and obtain the user
22-
const user = await userManager.signinCallback(window.location.href);
26+
const user = await usernManager.signinCallback(window.location.href);
2327

2428
const tokens: StoredTokens = {
2529
accessToken: user?.access_token,
@@ -44,7 +48,7 @@ export async function load() {
4448
} catch (err) {
4549
console.error('[auth/callback] signinCallback failed, trying signinRedirectCallback()', err);
4650
try {
47-
const user = await (userManager as any).signinRedirectCallback?.(window.location.href);
51+
const user = await (usernManager as any).signinRedirectCallback?.(window.location.href);
4852
const tokens: StoredTokens = {
4953
accessToken: user?.access_token,
5054
idToken: user?.id_token,

src/routes/auth/login/+page.svelte

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,9 @@
22
import Navigation from "$lib/components/Navigation.svelte";
33
import { onMount } from "svelte";
44
import { browser } from "$app/environment";
5-
import { userManager, signinRequest } from "$lib/authentication.js";
6-
import { SessionState, State } from "$lib/state.js";
5+
import { createUserManager, signinRequest } from "$lib/authentication.js";
6+
import { SessionState } from "$lib/state.js";
7+
import { page } from "$app/state";
78
89
let user: any = null;
910
let loading = false;
@@ -13,7 +14,7 @@
1314
if (!browser) return;
1415
try {
1516
loading = true;
16-
await signinRequest();
17+
await signinRequest(page.url);
1718
} catch (err) {
1819
error = err instanceof Error ? err.message : String(err);
1920
} finally {
@@ -24,7 +25,7 @@
2425
async function signOut() {
2526
if (!browser) return;
2627
try {
27-
await userManager.removeUser();
28+
await (createUserManager(window.location.origin)).removeUser();
2829
user = null;
2930
SessionState.user = null;
3031
SessionState.loggedIn = false;
@@ -36,7 +37,7 @@
3637
onMount(async () => {
3738
if (!browser) return;
3839
try {
39-
const stored = await userManager.getUser();
40+
const stored = await (createUserManager(window.location.origin)).getUser();
4041
if (stored) {
4142
user = stored.profile || stored;
4243
SessionState.user = user;

static/version.txt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
2025-10-21T23:54:01.488Z
1+
2025-10-22T08:27:34.267Z

0 commit comments

Comments
 (0)