Skip to content

Commit

Permalink
Give insightfull error if user have deleted the session storage
Browse files Browse the repository at this point in the history
  • Loading branch information
garronej committed Aug 8, 2024
1 parent 8945a41 commit 140ab7f
Showing 1 changed file with 32 additions and 1 deletion.
33 changes: 32 additions & 1 deletion src/oidc.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1125,13 +1125,42 @@ export async function createOidc<

const onTokenChanges = new Set<() => void>();

const assertSessionStorageNotCleared = () => {
const hasOidcSessionStorageEntry = (() => {
for (let i = 0; i < sessionStorage.length; i++) {
const key = sessionStorage.key(i);
assert(key !== null);

if (!key.startsWith("oidc.")) {
continue;
}

return true;
}

return false;
})();

if (hasOidcSessionStorageEntry) {
return;
}

throw new Error(
[
`You have manually cleared the sessionStorage. oidc-spa can't operate in this condition.`,
`Make sure you do not delete the "oidc." prefixed entries in the sessionStorage.`
].join(" ")
);
};

const oidc = id<Oidc.LoggedIn<DecodedIdToken>>({
...common,
"isUserLoggedIn": true,
"getTokens": () => currentTokens,
"logout": async params => {
assertSessionStorageNotCleared();

await oidcClientTsUserManager.signoutRedirect({
"id_token_hint": currentTokens.idToken,
"post_logout_redirect_uri": ((): string => {
switch (params.redirectTo) {
case "current page":
Expand Down Expand Up @@ -1160,6 +1189,8 @@ export async function createOidc<
return new Promise<never>(() => {});
},
"renewTokens": async () => {
assertSessionStorageNotCleared();

const oidcClientTsUser = await oidcClientTsUserManager.signinSilent();

assert(oidcClientTsUser !== null);
Expand Down

0 comments on commit 140ab7f

Please sign in to comment.