Skip to content

Commit d972f93

Browse files
committed
feat(types,clerk-js,backend-core,clerk-react): Replace thrown error with null return in getToken
1 parent 1dd0af2 commit d972f93

File tree

5 files changed

+8
-10
lines changed

5 files changed

+8
-10
lines changed

packages/backend-core/src/util/createGetToken.ts

+3-3
Original file line numberDiff line numberDiff line change
@@ -20,14 +20,14 @@ type CreateGetToken = (params: {
2020
*/
2121
export const createGetToken: CreateGetToken = params => {
2222
const { cookieToken, fetcher, headerToken, sessionId } = params || {};
23-
return (options: ServerGetTokenOptions = {}) => {
23+
return async (options: ServerGetTokenOptions = {}) => {
2424
if (!sessionId) {
25-
throw new Error('getToken cannot be called without a session.');
25+
return null;
2626
}
2727
if (options.template) {
2828
return fetcher(sessionId, options.template);
2929
}
30-
return Promise.resolve(headerToken || cookieToken) as Promise<string>;
30+
return (headerToken || cookieToken) as string;
3131
};
3232
};
3333

packages/clerk-js/src/core/resources/Session.ts

+2-2
Original file line numberDiff line numberDiff line change
@@ -51,9 +51,9 @@ export class Session extends BaseResource implements SessionResource {
5151
});
5252
};
5353

54-
getToken: GetToken = async (options?: GetTokenOptions): Promise<string> => {
54+
getToken: GetToken = async (options?: GetTokenOptions): Promise<string | null> => {
5555
if (!this.user) {
56-
throw new Error('You cannot call getToken when user is null');
56+
return null;
5757
}
5858

5959
const { leewayInSeconds = 10, template, skipCache = false } = options || {};

packages/react/src/hooks/utils.ts

+1-3
Original file line numberDiff line numberDiff line change
@@ -18,9 +18,7 @@ const clerkLoaded = (isomorphicClerk: IsomorphicClerk) => {
1818
export const createGetToken = (isomorphicClerk: IsomorphicClerk) => async (options: any) => {
1919
await clerkLoaded(isomorphicClerk);
2020
if (!isomorphicClerk.session) {
21-
throw new Error(
22-
'getToken cannot be called without a session. Check if sessionId has a value before calling getToken',
23-
);
21+
return null;
2422
}
2523
return isomorphicClerk.session.getToken(options);
2624
};

packages/types/src/session.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -55,4 +55,4 @@ export interface PublicUserData {
5555
}
5656

5757
export type GetTokenOptions = { template: string; leewayInSeconds?: number; skipCache?: boolean };
58-
export type GetToken = (options?: GetTokenOptions) => Promise<string>;
58+
export type GetToken = (options?: GetTokenOptions) => Promise<string | null>;

packages/types/src/ssr.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ import { SessionResource } from './session';
33
import { UserResource } from './user';
44

55
export type ServerGetTokenOptions = { template?: string };
6-
export type ServerGetToken = (options?: ServerGetTokenOptions) => Promise<string>;
6+
export type ServerGetToken = (options?: ServerGetTokenOptions) => Promise<string | null>;
77

88
export type ServerSideAuth = {
99
sessionId: string | null;

0 commit comments

Comments
 (0)