@workos-inc/authkit-js
Advanced tools
Comparing version 0.4.0 to 0.4.1
@@ -640,14 +640,19 @@ "use strict"; | ||
let _authkitClientState = "INITIAL"; | ||
const _needsRefresh = () => { | ||
if (_authkitClientState !== "AUTHENTICATED") { | ||
return true; | ||
const _shouldRefresh = () => { | ||
switch (_authkitClientState) { | ||
case "INITIAL": | ||
case "AUTHENTICATING": | ||
return true; | ||
case "ERROR": | ||
return false; | ||
case "AUTHENTICATED": | ||
const accessToken = memoryStorage.getItem(storageKeys.accessToken); | ||
const expiresAt = memoryStorage.getItem(storageKeys.expiresAt); | ||
if (!accessToken || !expiresAt) { | ||
return true; | ||
} | ||
const LEEWAY = 10 * 1e3; | ||
const refreshTime = expiresAt - LEEWAY; | ||
return refreshTime < Date.now(); | ||
} | ||
const accessToken = memoryStorage.getItem(storageKeys.accessToken); | ||
const expiresAt = memoryStorage.getItem(storageKeys.expiresAt); | ||
if (!accessToken || !expiresAt) { | ||
return true; | ||
} | ||
const LEEWAY = 10 * 1e3; | ||
const refreshTime = expiresAt - LEEWAY; | ||
return refreshTime < Date.now(); | ||
}; | ||
@@ -688,3 +693,3 @@ async function signIn(opts = {}) { | ||
async function getAccessToken() { | ||
if (_needsRefresh()) { | ||
if (_shouldRefresh()) { | ||
try { | ||
@@ -709,3 +714,3 @@ await refreshSession(); | ||
_refreshTimer = setTimeout(() => { | ||
if (_needsRefresh() && onBeforeAutoRefresh()) { | ||
if (_shouldRefresh() && onBeforeAutoRefresh()) { | ||
refreshSession().catch((e) => { | ||
@@ -712,0 +717,0 @@ console.error(e); |
{ | ||
"name": "@workos-inc/authkit-js", | ||
"version": "0.4.0", | ||
"version": "0.4.1", | ||
"description": "AuthKit SDK", | ||
@@ -5,0 +5,0 @@ "main": "./dist/index.js", |
@@ -65,22 +65,26 @@ import { CreateClientOptions, User } from "./interfaces"; | ||
const _needsRefresh = () => { | ||
if (_authkitClientState !== "AUTHENTICATED") { | ||
return true; | ||
} | ||
const _shouldRefresh = () => { | ||
switch (_authkitClientState) { | ||
case "INITIAL": | ||
case "AUTHENTICATING": | ||
return true; | ||
case "ERROR": | ||
return false; | ||
case "AUTHENTICATED": | ||
const accessToken = memoryStorage.getItem(storageKeys.accessToken) as | ||
| string | ||
| undefined; | ||
const expiresAt = memoryStorage.getItem(storageKeys.expiresAt) as | ||
| number | ||
| undefined; | ||
const accessToken = memoryStorage.getItem(storageKeys.accessToken) as | ||
| string | ||
| undefined; | ||
const expiresAt = memoryStorage.getItem(storageKeys.expiresAt) as | ||
| number | ||
| undefined; | ||
if (!accessToken || !expiresAt) { | ||
return true; | ||
} | ||
if (!accessToken || !expiresAt) { | ||
return true; | ||
// TODO: should LEEWAY be configurable? | ||
const LEEWAY = 10 * 1000; // 10 seconds | ||
const refreshTime = expiresAt - LEEWAY; | ||
return refreshTime < Date.now(); | ||
} | ||
// TODO: should LEEWAY be configurable? | ||
const LEEWAY = 10 * 1000; // 10 seconds | ||
const refreshTime = expiresAt - LEEWAY; | ||
return refreshTime < Date.now(); | ||
}; | ||
@@ -129,3 +133,3 @@ | ||
async function getAccessToken(): Promise<string> { | ||
if (_needsRefresh()) { | ||
if (_shouldRefresh()) { | ||
try { | ||
@@ -153,3 +157,3 @@ await refreshSession(); | ||
_refreshTimer = setTimeout(() => { | ||
if (_needsRefresh() && onBeforeAutoRefresh()) { | ||
if (_shouldRefresh() && onBeforeAutoRefresh()) { | ||
refreshSession() | ||
@@ -156,0 +160,0 @@ .catch((e) => { |
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
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
195352
2750