@cimpress/simple-auth-wrapper
Advanced tools
Comparing version 7.1.5 to 7.1.6
@@ -91,2 +91,5 @@ 'use strict'; | ||
// Check whether the current time is past the access token or session expiry time. | ||
// Check for the session's expiration time whenever the the web browser's tab regains focus | ||
@@ -198,2 +201,16 @@ | ||
this.isLoggedIn = function () { | ||
try { | ||
if (_this.sessionEnabled) { | ||
var expiresAt = JSON.parse(localStorage.getItem('sessionExpiresAt')); | ||
return new Date().getTime() < expiresAt - _this.sessionExpirationOffset * 1000; | ||
} else { | ||
var _expiresAt2 = JSON.parse(localStorage.getItem('tokenExpiresAt')); | ||
return new Date().getTime() < _expiresAt2 - _this.tokenExpirationOffset * 1000; | ||
} | ||
} catch (e) { | ||
return false; | ||
} | ||
}; | ||
this.handleFocusChange = _asyncToGenerator( /*#__PURE__*/regeneratorRuntime.mark(function _callee() { | ||
@@ -884,4 +901,9 @@ var expiration, tokenExpiration; | ||
case 0: | ||
_context12.prev = 0; | ||
_context12.next = 3; | ||
if (!_this.sessionEnabled) { | ||
_context12.next = 18; | ||
break; | ||
} | ||
_context12.prev = 1; | ||
_context12.next = 4; | ||
return fetch(SESSION_API_URL + '/' + sessionId + '/lifecycle/logout', { | ||
@@ -894,7 +916,7 @@ method: 'POST', | ||
case 3: | ||
case 4: | ||
result = _context12.sent; | ||
if (!result.ok) { | ||
_context12.next = 9; | ||
_context12.next = 10; | ||
break; | ||
@@ -905,19 +927,27 @@ } | ||
window.location = nextUri ? window.location.origin + nextUri : window.location.origin; | ||
_context12.next = 10; | ||
_context12.next = 11; | ||
break; | ||
case 9: | ||
case 10: | ||
throw new Error(result); | ||
case 10: | ||
_context12.next = 15; | ||
case 11: | ||
_context12.next = 16; | ||
break; | ||
case 12: | ||
_context12.prev = 12; | ||
_context12.t0 = _context12['catch'](0); | ||
case 13: | ||
_context12.prev = 13; | ||
_context12.t0 = _context12['catch'](1); | ||
console.error(_context12.t0); | ||
case 15: | ||
case 16: | ||
_context12.next = 20; | ||
break; | ||
case 18: | ||
_this.clearLocalParams(); | ||
window.location = nextUri ? window.location.origin + nextUri : window.location.origin; | ||
case 20: | ||
case 'end': | ||
@@ -927,3 +957,3 @@ return _context12.stop(); | ||
} | ||
}, _callee12, _this, [[0, 12]]); | ||
}, _callee12, _this, [[1, 13]]); | ||
})); | ||
@@ -930,0 +960,0 @@ |
{ | ||
"name": "@cimpress/simple-auth-wrapper", | ||
"version": "7.1.5", | ||
"version": "7.1.6", | ||
"description": "A simple utility class to wrap basic Auth0 functionality", | ||
@@ -5,0 +5,0 @@ "main": "lib/index.js", |
@@ -37,3 +37,3 @@ import shaJs from 'sha.js' | ||
constructor (options) { | ||
constructor(options) { | ||
merge(this, DEFAULT_OPTIONS, options); | ||
@@ -97,2 +97,17 @@ | ||
// Check whether the current time is past the access token or session expiry time. | ||
isLoggedIn = () => { | ||
try { | ||
if (this.sessionEnabled) { | ||
const expiresAt = JSON.parse(localStorage.getItem('sessionExpiresAt')); | ||
return new Date().getTime() < (expiresAt - (this.sessionExpirationOffset * 1000)); | ||
} else { | ||
const expiresAt = JSON.parse(localStorage.getItem('tokenExpiresAt')); | ||
return new Date().getTime() < (expiresAt - (this.tokenExpirationOffset * 1000)); | ||
} | ||
} catch (e) { | ||
return false; | ||
} | ||
}; | ||
// Check for the session's expiration time whenever the the web browser's tab regains focus | ||
@@ -488,18 +503,24 @@ handleFocusChange = async () => { | ||
logout = async (nextUri, sessionId) => { | ||
try { | ||
const result = await fetch(`${SESSION_API_URL}/${sessionId}/lifecycle/logout`, { | ||
method: 'POST', | ||
headers: new Headers({ | ||
'x-session-id': sessionId | ||
}) | ||
}); | ||
if (this.sessionEnabled) { | ||
try { | ||
const result = await fetch(`${SESSION_API_URL}/${sessionId}/lifecycle/logout`, { | ||
method: 'POST', | ||
headers: new Headers({ | ||
'x-session-id': sessionId | ||
}) | ||
}); | ||
if (result.ok) { | ||
this.clearLocalParams(); | ||
window.location = nextUri ? window.location.origin + nextUri : window.location.origin; | ||
} else { | ||
throw new Error(result); | ||
if (result.ok) { | ||
this.clearLocalParams(); | ||
window.location = nextUri ? window.location.origin + nextUri : window.location.origin; | ||
} else { | ||
throw new Error(result); | ||
} | ||
} catch (err) { | ||
console.error(err) | ||
} | ||
} catch (err) { | ||
console.error(err) | ||
} else { | ||
this.clearLocalParams(); | ||
window.location = nextUri ? window.location.origin + nextUri : window.location.origin; | ||
} | ||
@@ -506,0 +527,0 @@ } |
@@ -35,3 +35,4 @@ import AuthorizationCodeGrantPKCE from '../src/PKCE.js'; | ||
return Promise.resolve({ | ||
sessionId: 'test-session-id' | ||
sessionId: 'test-session-id', | ||
expiresAt: new Date(new Date().getTime() + 14400000), | ||
}) | ||
@@ -48,3 +49,3 @@ }); | ||
const getProfileSpy = jest.spyOn(testModule, "getProfile") | ||
expect(testModule.isLoggedIn()).toBe(false) | ||
return testModule | ||
@@ -61,2 +62,3 @@ .login() | ||
expect(getProfileSpy).toHaveBeenCalled(); | ||
expect(testModule.isLoggedIn()).toBe(true) | ||
}); | ||
@@ -270,5 +272,5 @@ }); | ||
const createSessionSpy = jest.spyOn(testModule, "createSession"); | ||
const getProfileSpy = jest.spyOn(testModule, "getProfile") | ||
const getTokenSpy = jest.spyOn(testModule, "getAccessToken") | ||
const getProfileSpy = jest.spyOn(testModule, "getProfile"); | ||
const getTokenSpy = jest.spyOn(testModule, "getAccessToken"); | ||
expect(testModule.isLoggedIn()).toBe(false); | ||
return testModule | ||
@@ -289,2 +291,3 @@ .login() | ||
expect(getTokenSpy).toHaveBeenCalled(); | ||
expect(testModule.isLoggedIn()).toBe(true); | ||
}); | ||
@@ -434,3 +437,2 @@ }); | ||
fetchMock.mockResponse(); | ||
const clearLocalStorageSpy = jest.spyOn(testModule, "clearLocalStorage"); | ||
@@ -537,4 +539,5 @@ const clearSessionTimeoutSpy = jest.spyOn(testModule, "clearSessionTimeout"); | ||
const createSessionSpy = jest.spyOn(testModule, "createSession"); | ||
const getProfileSpy = jest.spyOn(testModule, "getProfile") | ||
const getTokenSpy = jest.spyOn(testModule, "getAccessToken") | ||
const getProfileSpy = jest.spyOn(testModule, "getProfile"); | ||
const getTokenSpy = jest.spyOn(testModule, "getAccessToken"); | ||
expect(testModule.isLoggedIn()).toBe(false); | ||
@@ -556,2 +559,3 @@ return testModule | ||
expect(getTokenSpy).toHaveBeenCalled(); | ||
expect(testModule.isLoggedIn()).toBe(true); | ||
}); | ||
@@ -701,3 +705,2 @@ }); | ||
fetchMock.mockResponse(); | ||
const clearLocalStorageSpy = jest.spyOn(testModule, "clearLocalStorage"); | ||
@@ -704,0 +707,0 @@ const clearSessionTimeoutSpy = jest.spyOn(testModule, "clearSessionTimeout"); |
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
269388
3700