From 37726b7c97222f5ef8fc1de6c397a754614d08e5 Mon Sep 17 00:00:00 2001 From: Warren Parad Date: Mon, 5 Feb 2024 21:50:33 +0100 Subject: [PATCH] Ignore usage of cookie manager when cookies are not defined. --- src/index.js | 16 +++++++++------- src/userIdentityTokenStorageManager.js | 13 ++++++++++++- 2 files changed, 21 insertions(+), 8 deletions(-) diff --git a/src/index.js b/src/index.js index d762a42..decb41a 100644 --- a/src/index.js +++ b/src/index.js @@ -282,14 +282,16 @@ class LoginClient { const newUrl = new URL(windowManager.getCurrentLocation()); let authRequest = {}; - try { - authRequest = JSON.parse(localStorage.getItem(AuthenticationRequestNonceKey) || '{}'); - localStorage.removeItem(AuthenticationRequestNonceKey); - if (Object.hasOwnProperty.call(authRequest, 'enableCredentials')) { - this.enableCredentials = authRequest.enableCredentials; + if (typeof localStorage !== 'undefined') { + try { + authRequest = JSON.parse(localStorage.getItem(AuthenticationRequestNonceKey) || '{}'); + localStorage.removeItem(AuthenticationRequestNonceKey); + if (Object.hasOwnProperty.call(authRequest, 'enableCredentials')) { + this.enableCredentials = authRequest.enableCredentials; + } + } catch (error) { + this.logger && this.logger.debug && this.logger.debug({ title: 'LocalStorage failed in Browser', error }); } - } catch (error) { - this.logger && this.logger.debug && this.logger.debug({ title: 'LocalStorage failed in Browser', error }); } // Your app was redirected to from the Authress Hosted Login page. The next step is to show the user the login widget and enable them to login. diff --git a/src/userIdentityTokenStorageManager.js b/src/userIdentityTokenStorageManager.js index 1c5513d..d592f99 100644 --- a/src/userIdentityTokenStorageManager.js +++ b/src/userIdentityTokenStorageManager.js @@ -4,12 +4,19 @@ const AuthenticationCredentialsStorageKey = 'AuthenticationCredentialsStorage'; class UserIdentityTokenStorageManager { getUserCookie() { + if (typeof window === 'undefined' || typeof document === 'undefined') { + return null; + } // Skip empty cookies when fetching const val = document.cookie.split(';').filter(c => c.split('=')[0].trim() === 'user').map(c => c.replace(/^user=/, '')).find(c => c && c.trim()) || null; return val; } set(value, expiry) { + if (typeof window === 'undefined' || typeof document === 'undefined') { + return; + } + try { const cookies = cookieManager.parse(document.cookie); localStorage.setItem(AuthenticationCredentialsStorageKey, JSON.stringify({ idToken: value, expiry: expiry && expiry.getTime(), jsCookies: !!cookies.authorization })); @@ -20,6 +27,10 @@ class UserIdentityTokenStorageManager { } get() { + if (typeof window === 'undefined' || typeof document === 'undefined') { + return null; + } + let cookies = {}; try { cookies = cookieManager.parse(document.cookie); @@ -71,7 +82,7 @@ class UserIdentityTokenStorageManager { } clearCookies(cookieName) { - if (typeof window === 'undefined') { + if (typeof window === 'undefined' || typeof document === 'undefined') { return; }