@workos-inc/authkit-js
Advanced tools
Comparing version 0.2.0 to 0.2.1
@@ -51,3 +51,5 @@ interface User { | ||
getAccessToken: () => Promise<string | undefined>; | ||
refreshSession: () => Promise<void>; | ||
refreshSession: ({ organizationId, }?: { | ||
organizationId?: string; | ||
}) => Promise<void>; | ||
signOut: () => void; | ||
@@ -54,0 +56,0 @@ }>; |
@@ -99,4 +99,9 @@ "use strict"; | ||
}; | ||
async function authenticateWithRefreshToken(options) { | ||
const { baseUrl, clientId, refreshToken, useCookie } = options; | ||
async function authenticateWithRefreshToken({ | ||
baseUrl, | ||
clientId, | ||
refreshToken, | ||
organizationId, | ||
useCookie | ||
}) { | ||
const response = await fetch(`${baseUrl}/user_management/authenticate`, { | ||
@@ -112,3 +117,4 @@ method: "POST", | ||
grant_type: "refresh_token", | ||
...!useCookie && { refresh_token: refreshToken } | ||
...!useCookie && { refresh_token: refreshToken }, | ||
organization_id: organizationId | ||
}) | ||
@@ -599,2 +605,3 @@ }); | ||
var DEFAULT_HOSTNAME = "api.workos.com"; | ||
var ORGANIZATION_ID_SESSION_STORAGE_KEY = "workos_organization_id"; | ||
async function createClient(clientId, options = {}) { | ||
@@ -665,2 +672,5 @@ if (!clientId) { | ||
} | ||
function _getAccessToken() { | ||
return memoryStorage.getItem(storageKeys.accessToken); | ||
} | ||
async function getAccessToken() { | ||
@@ -670,3 +680,3 @@ if (_needsRefresh()) { | ||
} | ||
return memoryStorage.getItem(storageKeys.accessToken); | ||
return _getAccessToken(); | ||
} | ||
@@ -725,3 +735,5 @@ let _refreshTimer; | ||
_authkitClientState = "AUTHENTICATED"; | ||
_scheduleAutomaticRefresh(); | ||
setSessionData(authenticationResponse, { devMode }); | ||
_onRefresh && _onRefresh(authenticationResponse); | ||
onRedirectCallback({ state, ...authenticationResponse }); | ||
@@ -744,3 +756,5 @@ } | ||
const REFRESH_LOCK = "WORKOS_REFRESH_SESSION"; | ||
async function refreshSession() { | ||
async function refreshSession({ | ||
organizationId | ||
} = {}) { | ||
if (_authkitClientState !== "AUTHENTICATED" && _authkitClientState !== "INITIAL") { | ||
@@ -753,2 +767,15 @@ return; | ||
if (await lock.acquireLock(REFRESH_LOCK)) { | ||
if (organizationId) { | ||
sessionStorage.setItem( | ||
ORGANIZATION_ID_SESSION_STORAGE_KEY, | ||
organizationId | ||
); | ||
} else { | ||
const accessToken = _getAccessToken(); | ||
if (accessToken) { | ||
organizationId = getClaims(accessToken)?.org_id; | ||
} else { | ||
organizationId = sessionStorage.getItem(ORGANIZATION_ID_SESSION_STORAGE_KEY) ?? void 0; | ||
} | ||
} | ||
const authenticationResponse = await authenticateWithRefreshToken({ | ||
@@ -758,2 +785,3 @@ baseUrl: _baseUrl, | ||
refreshToken: getRefreshToken({ devMode }), | ||
organizationId, | ||
useCookie: _useCookie | ||
@@ -760,0 +788,0 @@ }); |
{ | ||
"name": "@workos-inc/authkit-js", | ||
"version": "0.2.0", | ||
"version": "0.2.1", | ||
"description": "AuthKit SDK", | ||
@@ -5,0 +5,0 @@ "main": "./dist/index.js", |
@@ -15,3 +15,3 @@ import { CreateClientOptions, User } from "./interfaces"; | ||
} from "./utils"; | ||
import { getRefreshToken } from "./utils/session-data"; | ||
import { getRefreshToken, getClaims } from "./utils/session-data"; | ||
import { RedirectParams } from "./interfaces/create-client-options.interface"; | ||
@@ -32,2 +32,4 @@ import Lock from "./vendor/browser-tabs-lock"; | ||
const ORGANIZATION_ID_SESSION_STORAGE_KEY = "workos_organization_id"; | ||
export async function createClient( | ||
@@ -120,2 +122,6 @@ clientId: string, | ||
function _getAccessToken() { | ||
return memoryStorage.getItem(storageKeys.accessToken) as string | undefined; | ||
} | ||
async function getAccessToken() { | ||
@@ -126,3 +132,4 @@ // TODO: should this respect onBeforeAutoRefresh ? | ||
} | ||
return memoryStorage.getItem(storageKeys.accessToken) as string | undefined; | ||
return _getAccessToken(); | ||
} | ||
@@ -193,3 +200,5 @@ | ||
_authkitClientState = "AUTHENTICATED"; | ||
_scheduleAutomaticRefresh(); | ||
setSessionData(authenticationResponse, { devMode }); | ||
_onRefresh && _onRefresh(authenticationResponse); | ||
onRedirectCallback({ state, ...authenticationResponse }); | ||
@@ -216,3 +225,5 @@ } | ||
async function refreshSession() { | ||
async function refreshSession({ | ||
organizationId, | ||
}: { organizationId?: string } = {}) { | ||
if ( | ||
@@ -228,3 +239,20 @@ _authkitClientState !== "AUTHENTICATED" && | ||
_authkitClientState = "AUTHENTICATING"; | ||
if (await lock.acquireLock(REFRESH_LOCK)) { | ||
if (organizationId) { | ||
sessionStorage.setItem( | ||
ORGANIZATION_ID_SESSION_STORAGE_KEY, | ||
organizationId, | ||
); | ||
} else { | ||
const accessToken = _getAccessToken(); | ||
if (accessToken) { | ||
organizationId = getClaims(accessToken)?.org_id; | ||
} else { | ||
organizationId = | ||
sessionStorage.getItem(ORGANIZATION_ID_SESSION_STORAGE_KEY) ?? | ||
undefined; | ||
} | ||
} | ||
const authenticationResponse = await authenticateWithRefreshToken({ | ||
@@ -234,2 +262,3 @@ baseUrl: _baseUrl, | ||
refreshToken: getRefreshToken({ devMode }), | ||
organizationId, | ||
useCookie: _useCookie, | ||
@@ -236,0 +265,0 @@ }); |
@@ -8,2 +8,3 @@ import { AuthenticationResponseRaw } from "../interfaces"; | ||
refreshToken: string | undefined; | ||
organizationId?: string; | ||
useCookie: boolean; | ||
@@ -14,6 +15,9 @@ } | ||
export async function authenticateWithRefreshToken( | ||
options: AuthenticateWithRefreshTokenOptions, | ||
) { | ||
const { baseUrl, clientId, refreshToken, useCookie } = options; | ||
export async function authenticateWithRefreshToken({ | ||
baseUrl, | ||
clientId, | ||
refreshToken, | ||
organizationId, | ||
useCookie, | ||
}: AuthenticateWithRefreshTokenOptions) { | ||
const response = await fetch(`${baseUrl}/user_management/authenticate`, { | ||
@@ -30,2 +34,3 @@ method: "POST", | ||
...(!useCookie && { refresh_token: refreshToken }), | ||
organization_id: organizationId, | ||
}), | ||
@@ -32,0 +37,0 @@ }); |
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
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
189244
2657