@propelauth/javascript
Advanced tools
Comparing version 1.1.0 to 1.1.1
@@ -121,3 +121,10 @@ 'use strict'; | ||
} | ||
function hasLocalStorage() { | ||
return typeof localStorage !== 'undefined'; | ||
} | ||
function getLocalStorageNumber(key) { | ||
if (!hasLocalStorage()) { | ||
return null; | ||
} | ||
const value = localStorage.getItem(key); | ||
@@ -323,2 +330,8 @@ | ||
const onStorageChange = async function () { | ||
// If localStorage isn't available, nothing to do here. | ||
// This usually happens in frameworks that have some SSR components | ||
if (!hasLocalStorage()) { | ||
return; | ||
} | ||
const loggedOutAt = getLocalStorageNumber(LOGGED_OUT_AT_KEY); | ||
@@ -325,0 +338,0 @@ const loggedInAt = getLocalStorageNumber(LOGGED_IN_AT_KEY); // If we've detected a logout event after the last one our client is aware of, trigger a refresh |
@@ -117,3 +117,10 @@ let UserRole; | ||
} | ||
function hasLocalStorage() { | ||
return typeof localStorage !== 'undefined'; | ||
} | ||
function getLocalStorageNumber(key) { | ||
if (!hasLocalStorage()) { | ||
return null; | ||
} | ||
const value = localStorage.getItem(key); | ||
@@ -319,2 +326,8 @@ | ||
const onStorageChange = async function () { | ||
// If localStorage isn't available, nothing to do here. | ||
// This usually happens in frameworks that have some SSR components | ||
if (!hasLocalStorage()) { | ||
return; | ||
} | ||
const loggedOutAt = getLocalStorageNumber(LOGGED_OUT_AT_KEY); | ||
@@ -321,0 +334,0 @@ const loggedInAt = getLocalStorageNumber(LOGGED_IN_AT_KEY); // If we've detected a logout event after the last one our client is aware of, trigger a refresh |
@@ -1,2 +0,2 @@ | ||
!function(e,t){"object"==typeof exports&&"undefined"!=typeof module?t(exports):"function"==typeof define&&define.amd?define(["exports"],t):t((e="undefined"!=typeof globalThis?globalThis:e||self).PropelAuth={})}(this,(function(e){"use strict";var t;function n(t){return new Promise(((n,o)=>{const r=new XMLHttpRequest;r.onreadystatechange=function(){if(r.readyState===XMLHttpRequest.DONE){const s=r.status;if(s>=200&&s<300)try{const o=(t=r.responseText,JSON.parse(t,(function(t,n){if("org_id"===t)this.orgId=n;else if("org_name"===t)this.orgName=n;else if("user_role"===t)this.userRole=(o=n,e.UserRole[o]);else if("access_token"===t)this.accessToken=n;else if("expires_at_seconds"===t)this.expiresAtSeconds=n;else if("org_id_to_org_member_info"===t)this.orgIdToOrgMemberInfo=n;else{if("user_id"!==t)return n;this.userId=n}var o})));n(o)}catch(e){console.error("Unable to process authentication response",e),o({status:500,message:"Unable to process authentication response"})}else 401===s?n(null):o({status:s,message:r.responseText})}var t},r.open("get",`${t}/api/v1/refresh_token`),r.withCredentials=!0,r.ontimeout=function(){o({status:408,message:"Request timed out"})},r.send(null)}))}function o(){return Date.now()/1e3}function r(e){const t=localStorage.getItem(e);if(!t)return null;const n=parseInt(t,10);return Number.isNaN(n)?null:n}e.UserRole=void 0,(t=e.UserRole||(e.UserRole={}))[t.Member=0]="Member",t[t.Admin=1]="Admin",t[t.Owner=2]="Owner";const s="__PROPEL_AUTH_LOGGED_IN_AT",a="__PROPEL_AUTH_LOGGED_OUT_AT";e.createClient=function(e){!function(e){try{const t=new URL(e.authUrl);e.authUrl=t.origin}catch(e){throw console.error("Invalid authUrl",e),new Error("Unable to initialize auth client")}void 0===e.enableBackgroundTokenRefresh&&(e.enableBackgroundTokenRefresh=!0)}(e);const t={initialLoadFinished:!1,authenticationInfo:null,observers:[],lastLoggedInAtMessage:r(s),lastLoggedOutAtMessage:r(a),authUrl:e.authUrl,refreshInterval:null};function i(e){for(let n=0;n<t.observers.length;n++){const o=t.observers[n];o&&o(e)}}function u(e){var n;const r=null===(n=t.authenticationInfo)||void 0===n?void 0:n.accessToken;t.authenticationInfo=e;const u=null==e?void 0:e.accessToken;!function(e,n){return!e&&(n||!t.initialLoadFinished)}(u,r)?function(e,t){return!t&&e}(u,r)&&(i(!0),function(){const e=o();t.lastLoggedInAtMessage=e,localStorage.setItem(s,String(e))}()):(i(!1),function(){const e=o();t.lastLoggedOutAtMessage=e,localStorage.setItem(a,String(e))}()),t.initialLoadFinished=!0}async function c(e){try{const e=await n(t.authUrl);return u(e),e}catch(n){if(e)return t.authenticationInfo;throw u(null),n}}const l={addLoggedInChangeObserver(e){t.observers.includes(e)?console.error("Observer has been attached already."):e?t.observers.push(e):console.error("Cannot add a null observer")},removeLoggedInChangeObserver(e){const n=t.observers.indexOf(e);-1===n?console.error("Cannot find observer to remove"):t.observers.splice(n,1)},async getAuthenticationInfoOrNull(e){const n=o();if(e)return await c(!1);if(t.authenticationInfo){if(n+240>t.authenticationInfo.expiresAtSeconds){const e=n<t.authenticationInfo.expiresAtSeconds;return await c(e)}return t.authenticationInfo}return await c(!1)},redirectToSignupPage(){window.location.href=`${t.authUrl}/signup`},redirectToLoginPage(){window.location.href=`${t.authUrl}/login`},redirectToAccountPage(){window.location.href=`${t.authUrl}/account`},redirectToOrgPage(e){window.location.href=e?`${t.authUrl}/org?id=${e}`:`${t.authUrl}/org`},redirectToCreateOrgPage(){window.location.href=`${t.authUrl}/create_org`},async logout(e){const n=await(o=t.authUrl,new Promise(((e,t)=>{const n=new XMLHttpRequest;n.onreadystatechange=function(){if(n.readyState===XMLHttpRequest.DONE){const o=n.status;if(o>=200&&o<300){const t=JSON.parse(n.responseText);e(t)}else console.error("Logout error",n.status,n.responseText),t({status:o,message:n.responseText})}},n.open("post",`${o}/api/v1/logout`),n.withCredentials=!0,n.ontimeout=function(){t({status:408,message:"Request timed out"})},n.send(null)})));var o;u(null),e&&(window.location.href=n.redirect_to)},destroy(){t.observers=[],window.removeEventListener("storage",g),t.refreshInterval&&clearInterval(t.refreshInterval)}},g=async function(){const e=r(a),n=r(s);e&&(!t.lastLoggedOutAtMessage||e>t.lastLoggedOutAtMessage)&&(t.lastLoggedOutAtMessage=e,t.authenticationInfo&&await c(!0)),n&&(!t.lastLoggedInAtMessage||n>t.lastLoggedInAtMessage)&&(t.lastLoggedInAtMessage=n,t.authenticationInfo||await c(!0))};return window.addEventListener("storage",g),e.enableBackgroundTokenRefresh&&(l.getAuthenticationInfoOrNull(),t.refreshInterval=window.setInterval(l.getAuthenticationInfoOrNull,6e4)),l}})); | ||
!function(e,t){"object"==typeof exports&&"undefined"!=typeof module?t(exports):"function"==typeof define&&define.amd?define(["exports"],t):t((e="undefined"!=typeof globalThis?globalThis:e||self).PropelAuth={})}(this,(function(e){"use strict";var t;function n(t){return new Promise(((n,o)=>{const r=new XMLHttpRequest;r.onreadystatechange=function(){if(r.readyState===XMLHttpRequest.DONE){const s=r.status;if(s>=200&&s<300)try{const o=(t=r.responseText,JSON.parse(t,(function(t,n){if("org_id"===t)this.orgId=n;else if("org_name"===t)this.orgName=n;else if("user_role"===t)this.userRole=(o=n,e.UserRole[o]);else if("access_token"===t)this.accessToken=n;else if("expires_at_seconds"===t)this.expiresAtSeconds=n;else if("org_id_to_org_member_info"===t)this.orgIdToOrgMemberInfo=n;else{if("user_id"!==t)return n;this.userId=n}var o})));n(o)}catch(e){console.error("Unable to process authentication response",e),o({status:500,message:"Unable to process authentication response"})}else 401===s?n(null):o({status:s,message:r.responseText})}var t},r.open("get",`${t}/api/v1/refresh_token`),r.withCredentials=!0,r.ontimeout=function(){o({status:408,message:"Request timed out"})},r.send(null)}))}function o(){return Date.now()/1e3}function r(){return"undefined"!=typeof localStorage}function s(e){if(!r())return null;const t=localStorage.getItem(e);if(!t)return null;const n=parseInt(t,10);return Number.isNaN(n)?null:n}e.UserRole=void 0,(t=e.UserRole||(e.UserRole={}))[t.Member=0]="Member",t[t.Admin=1]="Admin",t[t.Owner=2]="Owner";const a="__PROPEL_AUTH_LOGGED_IN_AT",i="__PROPEL_AUTH_LOGGED_OUT_AT";e.createClient=function(e){!function(e){try{const t=new URL(e.authUrl);e.authUrl=t.origin}catch(e){throw console.error("Invalid authUrl",e),new Error("Unable to initialize auth client")}void 0===e.enableBackgroundTokenRefresh&&(e.enableBackgroundTokenRefresh=!0)}(e);const t={initialLoadFinished:!1,authenticationInfo:null,observers:[],lastLoggedInAtMessage:s(a),lastLoggedOutAtMessage:s(i),authUrl:e.authUrl,refreshInterval:null};function u(e){for(let n=0;n<t.observers.length;n++){const o=t.observers[n];o&&o(e)}}function l(e){var n;const r=null===(n=t.authenticationInfo)||void 0===n?void 0:n.accessToken;t.authenticationInfo=e;const s=null==e?void 0:e.accessToken;!function(e,n){return!e&&(n||!t.initialLoadFinished)}(s,r)?function(e,t){return!t&&e}(s,r)&&(u(!0),function(){const e=o();t.lastLoggedInAtMessage=e,localStorage.setItem(a,String(e))}()):(u(!1),function(){const e=o();t.lastLoggedOutAtMessage=e,localStorage.setItem(i,String(e))}()),t.initialLoadFinished=!0}async function c(e){try{const e=await n(t.authUrl);return l(e),e}catch(n){if(e)return t.authenticationInfo;throw l(null),n}}const d={addLoggedInChangeObserver(e){t.observers.includes(e)?console.error("Observer has been attached already."):e?t.observers.push(e):console.error("Cannot add a null observer")},removeLoggedInChangeObserver(e){const n=t.observers.indexOf(e);-1===n?console.error("Cannot find observer to remove"):t.observers.splice(n,1)},async getAuthenticationInfoOrNull(e){const n=o();if(e)return await c(!1);if(t.authenticationInfo){if(n+240>t.authenticationInfo.expiresAtSeconds){const e=n<t.authenticationInfo.expiresAtSeconds;return await c(e)}return t.authenticationInfo}return await c(!1)},redirectToSignupPage(){window.location.href=`${t.authUrl}/signup`},redirectToLoginPage(){window.location.href=`${t.authUrl}/login`},redirectToAccountPage(){window.location.href=`${t.authUrl}/account`},redirectToOrgPage(e){window.location.href=e?`${t.authUrl}/org?id=${e}`:`${t.authUrl}/org`},redirectToCreateOrgPage(){window.location.href=`${t.authUrl}/create_org`},async logout(e){const n=await(o=t.authUrl,new Promise(((e,t)=>{const n=new XMLHttpRequest;n.onreadystatechange=function(){if(n.readyState===XMLHttpRequest.DONE){const o=n.status;if(o>=200&&o<300){const t=JSON.parse(n.responseText);e(t)}else console.error("Logout error",n.status,n.responseText),t({status:o,message:n.responseText})}},n.open("post",`${o}/api/v1/logout`),n.withCredentials=!0,n.ontimeout=function(){t({status:408,message:"Request timed out"})},n.send(null)})));var o;l(null),e&&(window.location.href=n.redirect_to)},destroy(){t.observers=[],window.removeEventListener("storage",g),t.refreshInterval&&clearInterval(t.refreshInterval)}},g=async function(){if(!r())return;const e=s(i),n=s(a);e&&(!t.lastLoggedOutAtMessage||e>t.lastLoggedOutAtMessage)&&(t.lastLoggedOutAtMessage=e,t.authenticationInfo&&await c(!0)),n&&(!t.lastLoggedInAtMessage||n>t.lastLoggedInAtMessage)&&(t.lastLoggedInAtMessage=n,t.authenticationInfo||await c(!0))};return window.addEventListener("storage",g),e.enableBackgroundTokenRefresh&&(d.getAuthenticationInfoOrNull(),t.refreshInterval=window.setInterval(d.getAuthenticationInfoOrNull,6e4)),d}})); | ||
//# sourceMappingURL=javascript.min.js.map |
export declare function currentTimeSeconds(): number; | ||
export declare function hasLocalStorage(): boolean; | ||
export declare function getLocalStorageNumber(key: string): number | null; |
@@ -7,3 +7,3 @@ { | ||
}, | ||
"version": "1.1.0", | ||
"version": "1.1.1", | ||
"keywords": [ | ||
@@ -10,0 +10,0 @@ "auth", |
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
109955
679