Skip to content

Commit

Permalink
Merge pull request #34 from Authress/add-nextjs-expectations
Browse files Browse the repository at this point in the history
Ignore usage of cookie manager when cookies are not defined.
  • Loading branch information
wparad authored Feb 5, 2024
2 parents 4e3156e + 37726b7 commit 49d50f1
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 8 deletions.
16 changes: 9 additions & 7 deletions src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down
13 changes: 12 additions & 1 deletion src/userIdentityTokenStorageManager.js
Original file line number Diff line number Diff line change
Expand Up @@ -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 }));
Expand All @@ -20,6 +27,10 @@ class UserIdentityTokenStorageManager {
}

get() {
if (typeof window === 'undefined' || typeof document === 'undefined') {
return null;
}

let cookies = {};
try {
cookies = cookieManager.parse(document.cookie);
Expand Down Expand Up @@ -71,7 +82,7 @@ class UserIdentityTokenStorageManager {
}

clearCookies(cookieName) {
if (typeof window === 'undefined') {
if (typeof window === 'undefined' || typeof document === 'undefined') {
return;
}

Expand Down

0 comments on commit 49d50f1

Please sign in to comment.