Skip to content

Commit

Permalink
feat: add minimal SSR support
Browse files Browse the repository at this point in the history
  • Loading branch information
pamapa committed Dec 15, 2021
1 parent 5ac2ade commit 2a6f632
Show file tree
Hide file tree
Showing 4 changed files with 24 additions and 9 deletions.
13 changes: 10 additions & 3 deletions src/OidcClientSettings.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
import { WebStorageStateStore } from "./WebStorageStateStore";
import type { OidcMetadata } from "./OidcMetadata";
import type { StateStore } from "./StateStore";
import { InMemoryWebStorage } from "./InMemoryWebStorage";

const DefaultResponseType = "code";
const DefaultScope = "openid";
Expand Down Expand Up @@ -87,7 +88,7 @@ export interface OidcClientSettings {
mergeClaims?: boolean;

/**
* Storage object used to persist interaction state (default: local storage).
* Storage object used to persist interaction state (default: window.localStorage, InMemoryWebStorage iff no window).
* E.g. `stateStore: new WebStorageStateStore({ store: window.localStorage })`
*/
stateStore?: StateStore;
Expand Down Expand Up @@ -164,7 +165,7 @@ export class OidcClientSettingsStore {
userInfoJwtIssuer = "OP",
mergeClaims = false,
// other behavior
stateStore = new WebStorageStateStore(),
stateStore,
// extra query params
extraQueryParams = {},
extraTokenParams = {},
Expand Down Expand Up @@ -199,7 +200,13 @@ export class OidcClientSettingsStore {
this.userInfoJwtIssuer = userInfoJwtIssuer;
this.mergeClaims = !!mergeClaims;

this.stateStore = stateStore;
if (stateStore) {
this.stateStore = stateStore;
}
else {
const store = typeof window !== "undefined" ? window.localStorage : new InMemoryWebStorage();
this.stateStore = new WebStorageStateStore({ store });
}

this.extraQueryParams = extraQueryParams;
this.extraTokenParams = extraTokenParams;
Expand Down
13 changes: 10 additions & 3 deletions src/UserManagerSettings.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
import { OidcClientSettings, OidcClientSettingsStore } from "./OidcClientSettings";
import type { PopupWindowFeatures } from "./utils";
import { WebStorageStateStore } from "./WebStorageStateStore";
import { InMemoryWebStorage } from "./InMemoryWebStorage";

const DefaultAccessTokenExpiringNotificationTimeInSeconds = 60;
const DefaultCheckSessionIntervalInSeconds = 2;
Expand Down Expand Up @@ -53,7 +54,7 @@ export interface UserManagerSettings extends OidcClientSettings {
accessTokenExpiringNotificationTimeInSeconds?: number;

/**
* Storage object used to persist User for currently authenticated user (default: session storage).
* Storage object used to persist User for currently authenticated user (default: window.sessionStorage, InMemoryWebStorage iff no window).
* E.g. `userStore: new WebStorageStateStore({ store: window.localStorage })`
*/
userStore?: WebStorageStateStore;
Expand Down Expand Up @@ -112,7 +113,7 @@ export class UserManagerSettingsStore extends OidcClientSettingsStore {
revokeAccessTokenOnSignout = false,
accessTokenExpiringNotificationTimeInSeconds = DefaultAccessTokenExpiringNotificationTimeInSeconds,

userStore = new WebStorageStateStore({ store: sessionStorage }),
userStore,
} = args;

super(args);
Expand Down Expand Up @@ -143,6 +144,12 @@ export class UserManagerSettingsStore extends OidcClientSettingsStore {
this.revokeAccessTokenOnSignout = revokeAccessTokenOnSignout;
this.accessTokenExpiringNotificationTimeInSeconds = accessTokenExpiringNotificationTimeInSeconds;

this.userStore = userStore;
if (userStore) {
this.userStore = userStore;
}
else {
const store = typeof window !== "undefined" ? window.sessionStorage : new InMemoryWebStorage();
this.userStore = new WebStorageStateStore({ store });
}
}
}
3 changes: 2 additions & 1 deletion src/utils/CryptoUtils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,8 @@ export class CryptoUtils {
* Generates RFC4122 version 4 guid
*/
public static generateUUIDv4(): string {
const hasRandomValues = window.crypto && Object.prototype.hasOwnProperty.call(window.crypto, "getRandomValues");
const hasRandomValues = typeof window !== "undefined" && window.crypto &&
Object.prototype.hasOwnProperty.call(window.crypto, "getRandomValues");
const uuid = hasRandomValues ? CryptoUtils._cryptoUUIDv4() : CryptoUtils._UUIDv4();
return uuid.replace(/-/g, "");
}
Expand Down
4 changes: 2 additions & 2 deletions src/utils/Timer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,10 +18,10 @@ export type IntervalTimer = {
*/
export const g_timer: IntervalTimer = {
setInterval: function (cb: () => void, duration?: number): number {
return window.setInterval(cb, duration);
return setInterval(cb, duration);
},
clearInterval: function (handle: number): void {
return window.clearInterval(handle);
return clearInterval(handle);
},
};

Expand Down

0 comments on commit 2a6f632

Please sign in to comment.