Skip to content

Commit

Permalink
feat(types,clerk-js,backend-core,clerk-react): Replace thrown error w…
Browse files Browse the repository at this point in the history
…ith null return in getToken
  • Loading branch information
nikosdouvlis committed Mar 24, 2022
1 parent 1dd0af2 commit d972f93
Show file tree
Hide file tree
Showing 5 changed files with 8 additions and 10 deletions.
6 changes: 3 additions & 3 deletions packages/backend-core/src/util/createGetToken.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,14 +20,14 @@ type CreateGetToken = (params: {
*/
export const createGetToken: CreateGetToken = params => {
const { cookieToken, fetcher, headerToken, sessionId } = params || {};
return (options: ServerGetTokenOptions = {}) => {
return async (options: ServerGetTokenOptions = {}) => {
if (!sessionId) {
throw new Error('getToken cannot be called without a session.');
return null;
}
if (options.template) {
return fetcher(sessionId, options.template);
}
return Promise.resolve(headerToken || cookieToken) as Promise<string>;
return (headerToken || cookieToken) as string;
};
};

Expand Down
4 changes: 2 additions & 2 deletions packages/clerk-js/src/core/resources/Session.ts
Original file line number Diff line number Diff line change
Expand Up @@ -51,9 +51,9 @@ export class Session extends BaseResource implements SessionResource {
});
};

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

const { leewayInSeconds = 10, template, skipCache = false } = options || {};
Expand Down
4 changes: 1 addition & 3 deletions packages/react/src/hooks/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,9 +18,7 @@ const clerkLoaded = (isomorphicClerk: IsomorphicClerk) => {
export const createGetToken = (isomorphicClerk: IsomorphicClerk) => async (options: any) => {
await clerkLoaded(isomorphicClerk);
if (!isomorphicClerk.session) {
throw new Error(
'getToken cannot be called without a session. Check if sessionId has a value before calling getToken',
);
return null;
}
return isomorphicClerk.session.getToken(options);
};
Expand Down
2 changes: 1 addition & 1 deletion packages/types/src/session.ts
Original file line number Diff line number Diff line change
Expand Up @@ -55,4 +55,4 @@ export interface PublicUserData {
}

export type GetTokenOptions = { template: string; leewayInSeconds?: number; skipCache?: boolean };
export type GetToken = (options?: GetTokenOptions) => Promise<string>;
export type GetToken = (options?: GetTokenOptions) => Promise<string | null>;
2 changes: 1 addition & 1 deletion packages/types/src/ssr.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import { SessionResource } from './session';
import { UserResource } from './user';

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

export type ServerSideAuth = {
sessionId: string | null;
Expand Down

0 comments on commit d972f93

Please sign in to comment.