Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 7 additions & 4 deletions packages/clerk-js/rspack.config.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ const variants = {
clerk: 'clerk',
clerkNoRHC: 'clerk.no-rhc', // Omit Remotely Hosted Code
clerkBrowser: 'clerk.browser',
clerkExperimental: 'clerk.experimental',
clerkHeadless: 'clerk.headless',
clerkHeadlessBrowser: 'clerk.headless.browser',
clerkLegacyBrowser: 'clerk.legacy.browser',
Expand All @@ -24,6 +25,7 @@ const variantToSourceFile = {
[variants.clerk]: './src/index.ts',
[variants.clerkNoRHC]: './src/index.ts',
[variants.clerkBrowser]: './src/index.browser.ts',
[variants.clerkExperimental]: './src/index.ts',
[variants.clerkHeadless]: './src/index.headless.ts',
[variants.clerkHeadlessBrowser]: './src/index.headless.browser.ts',
[variants.clerkLegacyBrowser]: './src/index.legacy.browser.ts',
Expand Down Expand Up @@ -51,15 +53,16 @@ const common = ({ mode, variant, disableRHC = false }) => {
},
plugins: [
new rspack.DefinePlugin({
__DEV__: isDevelopment(mode),
__PKG_VERSION__: JSON.stringify(packageJSON.version),
__PKG_NAME__: JSON.stringify(packageJSON.name),
/**
* Build time feature flags.
*/
__BUILD_FLAG_KEYLESS_UI__: isDevelopment(mode),
__BUILD_DISABLE_RHC__: JSON.stringify(disableRHC),
__BUILD_FLAG_KEYLESS_UI__: isDevelopment(mode),
__BUILD_VARIANT_CHIPS__: variant === variants.clerkCHIPS,
__BUILD_VARIANT_EXPERIMENTAL__: variant === variants.clerkExperimental,
__DEV__: isDevelopment(mode),
__PKG_NAME__: JSON.stringify(packageJSON.name),
__PKG_VERSION__: JSON.stringify(packageJSON.version),
}),
new rspack.EnvironmentPlugin({
CLERK_ENV: mode,
Expand Down
40 changes: 40 additions & 0 deletions packages/clerk-js/src/core/resources/Session.ts
Original file line number Diff line number Diff line change
Expand Up @@ -37,10 +37,17 @@ import { TokenId } from '@/utils/tokenId';

import { clerkInvalidStrategy, clerkMissingWebAuthnPublicKeyOptions } from '../errors';
import { eventBus, events } from '../events';
import { TokenService } from '../services/TokenService';
import { SessionTokenCache } from '../tokenCache';
import { BaseResource, PublicUserData, Token, User } from './internal';
import { SessionVerification } from './SessionVerification';

type TokenFetcher = (params: {
organizationId?: string | null;
sessionId: string;
template?: string;
}) => Promise<Token>;

export class Session extends BaseResource implements SessionResource {
pathRoot = '/client/sessions';

Expand All @@ -58,6 +65,7 @@ export class Session extends BaseResource implements SessionResource {
abandonAt!: Date;
createdAt!: Date;
updatedAt!: Date;
private tokenService: TokenService | null = null;

static isSessionResource(resource: unknown): resource is Session {
return !!resource && resource instanceof Session;
Expand All @@ -68,6 +76,26 @@ export class Session extends BaseResource implements SessionResource {

this.fromJSON(data);
this.#hydrateCache(this.lastActiveToken);

if (__BUILD_VARIANT_EXPERIMENTAL__) {
this.tokenService = new TokenService(this.id, {
fetcher: this.createTokenFetcher(),
onTokenResolved: token => {
eventBus.emit(events.TokenUpdate, { token });
if (token.jwt) {
this.lastActiveToken = token;
eventBus.emit(events.SessionTokenResolved, null);
}
},
});

if (this.lastActiveToken) {
const cacheKey = this.tokenService.buildCacheKey();
const ingestedToken =
this.lastActiveToken instanceof Token ? this.lastActiveToken : new Token(this.lastActiveToken as any);
this.tokenService.ingestToken(ingestedToken, cacheKey);
}
}
}

end = (): Promise<SessionResource> => {
Expand Down Expand Up @@ -102,6 +130,10 @@ export class Session extends BaseResource implements SessionResource {
};

getToken: GetToken = async (options?: GetTokenOptions): Promise<string | null> => {
if (this.tokenService) {
return this.tokenService.getToken(options);
}

// This will retry the getToken call if it fails with a non-4xx error
// We're going to trigger 8 retries in the span of ~3 minutes,
// Example delays: 3s, 5s, 13s, 19s, 26s, 34s, 43s, 50s, total: ~193s
Expand Down Expand Up @@ -139,6 +171,14 @@ export class Session extends BaseResource implements SessionResource {
}
};

private createTokenFetcher(): TokenFetcher {
return async params => {
const path = params.template ? `${this.path()}/tokens/${params.template}` : `${this.path()}/tokens`;
const queryParams = params.template ? {} : { organizationId: params.organizationId };
return Token.create(path, queryParams);
};
}

// If it's a session token, retrieve it with their session id, otherwise it's a jwt template token
// and retrieve it using the session id concatenated with the jwt template name.
// e.g. session id is 'sess_abc12345' and jwt template name is 'haris'
Expand Down
Loading
Loading