Skip to content

Commit

Permalink
feat(clerk-remix): Remove load options from getAuth
Browse files Browse the repository at this point in the history
  • Loading branch information
nikosdouvlis committed Feb 25, 2022
1 parent 287a438 commit 5c1e23d
Show file tree
Hide file tree
Showing 4 changed files with 9 additions and 18 deletions.
2 changes: 1 addition & 1 deletion packages/nextjs/src/middleware/utils/getAuthData.ts
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@ export async function getAuthData(

const [user, session] = await Promise.all([
loadUser ? users.getUser(sessionClaims.sub as string) : Promise.resolve(undefined),
loadSession ? await sessions.getSession(sessionClaims.sid as string) : Promise.resolve(undefined),
loadSession ? sessions.getSession(sessionClaims.sid as string) : Promise.resolve(undefined),
]);

return {
Expand Down
9 changes: 3 additions & 6 deletions packages/remix/src/ssr/getAuth.ts
Original file line number Diff line number Diff line change
@@ -1,18 +1,15 @@
import { noRequestPassedInGetAuth } from '../errors';
import { getAuthData } from './getAuthData';
import { GetAuthOptions, GetAuthReturn, LoaderFunctionArgs } from './types';
import { GetAuthReturn, LoaderFunctionArgs } from './types';
import { sanitizeAuthData } from './utils';

export async function getAuth<Options extends GetAuthOptions>(
argsOrReq: Request | LoaderFunctionArgs,
options?: Options,
): GetAuthReturn<Options> {
export async function getAuth(argsOrReq: Request | LoaderFunctionArgs): GetAuthReturn {
if (!argsOrReq) {
throw new Error(noRequestPassedInGetAuth);
}

const request = 'request' in argsOrReq ? argsOrReq.request : argsOrReq;
const { authData } = await getAuthData(request, options || {});
const { authData } = await getAuthData(request);
// @ts-expect-error This can only return null during interstitial,
// but the public types should not know that
return sanitizeAuthData(authData || {});
Expand Down
6 changes: 3 additions & 3 deletions packages/remix/src/ssr/getAuthData.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import { AuthStatus, Session, User } from '@clerk/backend-core';
import Clerk, { sessions, users } from '@clerk/clerk-sdk-node';
import { GetSessionTokenOptions } from '@clerk/types';

import { GetAuthOptions } from './types';
import { RootAuthLoaderOptions } from './types';
import { extractInterstitialHtmlContent, parseCookies } from './utils';

export type AuthData = {
Expand All @@ -15,7 +15,7 @@ export type AuthData = {

export async function getAuthData(
req: Request,
opts: GetAuthOptions,
opts: RootAuthLoaderOptions = {},
): Promise<{ authData: AuthData | null; interstitial?: string }> {
const { loadSession, loadUser } = opts;
const { headers } = req;
Expand Down Expand Up @@ -60,7 +60,7 @@ export async function getAuthData(

const [user, session] = await Promise.all([
loadUser ? users.getUser(sessionClaims.sub as string) : Promise.resolve(undefined),
loadSession ? await sessions.getSession(sessionClaims.sid as string) : Promise.resolve(undefined),
loadSession ? sessions.getSession(sessionClaims.sid as string) : Promise.resolve(undefined),
]);

return {
Expand Down
10 changes: 2 additions & 8 deletions packages/remix/src/ssr/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,19 +7,13 @@ export type InferRootLoaderData<U = any> = {
clerkState: { __type: 'clerkState' };
};

export type GetAuthReturn<Options> = Promise<
ServerSideAuth &
(Options extends { loadSession: true } ? { session: Session | null } : {}) &
(Options extends { loadUser: true } ? { user: User | null } : {})
>;
export type GetAuthReturn = Promise<ServerSideAuth>;

export type GetAuthOptions = {
export type RootAuthLoaderOptions = {
loadUser?: boolean;
loadSession?: boolean;
};

export type RootAuthLoaderOptions = GetAuthOptions;

export type RootAuthLoaderCallback<Options> = (args: LoaderFunctionArgsWithAuth<Options>) => LoaderFunctionReturn;

export type LoaderFunctionArgs = Parameters<LoaderFunction>[0];
Expand Down

0 comments on commit 5c1e23d

Please sign in to comment.