Skip to content

Commit

Permalink
feat(remix,backend): Repo level config for remix (#1470)
Browse files Browse the repository at this point in the history
* feat(remix,backend): Repo level config for remix
* fix(remix): Remove unused utility
* fix(remix): Add ClerkState missing types
* test(backend,remix): Update tests
* chore(repo): Changeset
  • Loading branch information
desiprisg authored Aug 4, 2023
1 parent bc07de9 commit 30fcdd5
Show file tree
Hide file tree
Showing 11 changed files with 107 additions and 4 deletions.
5 changes: 5 additions & 0 deletions .changeset/nine-peas-care.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'@clerk/remix': minor
---

Configure sign in/up and afterSignIn/Up paths for remix via env variables.
5 changes: 5 additions & 0 deletions .changeset/wicked-schools-tell.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'@clerk/backend': minor
---

Include `signUpUrl`, `afterSignInUrl` and `afterSignUpUrl` to `authenticateRequest` options.
48 changes: 45 additions & 3 deletions packages/backend/src/tokens/authStatus.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,9 @@ export type SignedInState = {
isSatellite: boolean;
domain: string;
signInUrl: string;
signUpUrl: string;
afterSignInUrl: string;
afterSignUpUrl: string;
isSignedIn: true;
isInterstitial: false;
isUnknown: false;
Expand All @@ -38,6 +41,9 @@ export type SignedOutState = {
isSatellite: boolean;
domain: string;
signInUrl: string;
signUpUrl: string;
afterSignInUrl: string;
afterSignUpUrl: string;
isSignedIn: false;
isInterstitial: false;
isUnknown: false;
Expand Down Expand Up @@ -94,6 +100,9 @@ export async function signedIn<T>(options: T, sessionClaims: JwtPayload): Promis
loadUser,
loadOrganization,
signInUrl,
signUpUrl,
afterSignInUrl,
afterSignUpUrl,
} = options as any;

const { sid: sessionId, org_id: orgId, sub: userId } = sessionClaims;
Expand Down Expand Up @@ -143,6 +152,9 @@ export async function signedIn<T>(options: T, sessionClaims: JwtPayload): Promis
domain,
isSatellite,
signInUrl,
signUpUrl,
afterSignInUrl,
afterSignUpUrl,
isSignedIn: true,
isInterstitial: false,
isUnknown: false,
Expand All @@ -151,7 +163,17 @@ export async function signedIn<T>(options: T, sessionClaims: JwtPayload): Promis
}

export function signedOut<T>(options: T, reason: AuthReason, message = ''): SignedOutState {
const { frontendApi, publishableKey, proxyUrl, isSatellite, domain, signInUrl } = options as any;
const {
frontendApi,
publishableKey,
proxyUrl,
isSatellite,
domain,
signInUrl,
signUpUrl,
afterSignInUrl,
afterSignUpUrl,
} = options as any;

return {
status: AuthStatus.SignedOut,
Expand All @@ -163,6 +185,9 @@ export function signedOut<T>(options: T, reason: AuthReason, message = ''): Sign
isSatellite,
domain,
signInUrl,
signUpUrl,
afterSignInUrl,
afterSignUpUrl,
isSignedIn: false,
isInterstitial: false,
isUnknown: false,
Expand All @@ -171,7 +196,17 @@ export function signedOut<T>(options: T, reason: AuthReason, message = ''): Sign
}

export function interstitial<T>(options: T, reason: AuthReason, message = ''): InterstitialState {
const { frontendApi, publishableKey, proxyUrl, isSatellite, domain, signInUrl } = options as any;
const {
frontendApi,
publishableKey,
proxyUrl,
isSatellite,
domain,
signInUrl,
signUpUrl,
afterSignInUrl,
afterSignUpUrl,
} = options as any;
return {
status: AuthStatus.Interstitial,
reason,
Expand All @@ -182,6 +217,9 @@ export function interstitial<T>(options: T, reason: AuthReason, message = ''): I
domain,
proxyUrl,
signInUrl,
signUpUrl,
afterSignInUrl,
afterSignUpUrl,
isSignedIn: false,
isInterstitial: true,
isUnknown: false,
Expand All @@ -190,7 +228,8 @@ export function interstitial<T>(options: T, reason: AuthReason, message = ''): I
}

export function unknownState<T>(options: T, reason: AuthReason, message = ''): UnknownState {
const { frontendApi, publishableKey, isSatellite, domain, signInUrl } = options as any;
const { frontendApi, publishableKey, isSatellite, domain, signInUrl, signUpUrl, afterSignInUrl, afterSignUpUrl } =
options as any;
return {
status: AuthStatus.Unknown,
reason,
Expand All @@ -200,6 +239,9 @@ export function unknownState<T>(options: T, reason: AuthReason, message = ''): U
isSatellite,
domain,
signInUrl,
signUpUrl,
afterSignInUrl,
afterSignUpUrl,
isSignedIn: false,
isInterstitial: false,
isUnknown: true,
Expand Down
15 changes: 15 additions & 0 deletions packages/backend/src/tokens/request.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,9 @@ function assertSignedOut(
isUnknown: false,
isSatellite: false,
signInUrl: '',
signUpUrl: '',
afterSignInUrl: '',
afterSignUpUrl: '',
domain: '',
message: '',
toAuth: {},
Expand Down Expand Up @@ -72,6 +75,9 @@ function assertInterstitial(
isUnknown: false,
isSatellite: false,
signInUrl: '',
signUpUrl: '',
afterSignInUrl: '',
afterSignUpUrl: '',
domain: '',
toAuth: {},
...expectedState,
Expand All @@ -88,6 +94,9 @@ function assertUnknown(assert, requestState: RequestState, reason: AuthReason) {
isUnknown: true,
isSatellite: false,
signInUrl: '',
signUpUrl: '',
afterSignInUrl: '',
afterSignUpUrl: '',
domain: '',
reason,
toAuth: {},
Expand Down Expand Up @@ -128,6 +137,9 @@ function assertSignedIn(
isUnknown: false,
isSatellite: false,
signInUrl: '',
signUpUrl: '',
afterSignInUrl: '',
afterSignUpUrl: '',
domain: '',
...expectedState,
});
Expand All @@ -150,6 +162,9 @@ export default (QUnit: QUnit) => {
skipJwksCache: true,
isSatellite: false,
signInUrl: '',
signUpUrl: '',
afterSignInUrl: '',
afterSignUpUrl: '',
domain: '',
searchParams: new URLSearchParams(),
} satisfies AuthenticateRequestOptions;
Expand Down
3 changes: 3 additions & 0 deletions packages/backend/src/tokens/request.ts
Original file line number Diff line number Diff line change
Expand Up @@ -101,6 +101,9 @@ export type AuthenticateRequestOptions = OptionalVerifyTokenOptions &
* @experimental
*/
signInUrl?: string;
signUpUrl?: string;
afterSignInUrl?: string;
afterSignUpUrl?: string;
request?: Request;
};

Expand Down
2 changes: 1 addition & 1 deletion packages/nextjs/src/utils/mergeNextClerkPropsWithEnv.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,5 +14,5 @@ export const mergeNextClerkPropsWithEnv = (props: Omit<NextClerkProviderProps, '
signUpUrl: props.signUpUrl || process.env.NEXT_PUBLIC_CLERK_SIGN_UP_URL || '',
afterSignInUrl: props.afterSignInUrl || process.env.NEXT_PUBLIC_CLERK_AFTER_SIGN_IN_URL || '',
afterSignUpUrl: props.afterSignUpUrl || process.env.NEXT_PUBLIC_CLERK_AFTER_SIGN_UP_URL || '',
} as any as NextClerkProviderProps;
} as unknown as NextClerkProviderProps;
};
6 changes: 6 additions & 0 deletions packages/remix/src/client/RemixClerkProvider.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,9 @@ export function ClerkProvider({ children, ...rest }: RemixClerkProviderProps): J
__isSatellite,
__clerk_debug,
__signInUrl,
__signUpUrl,
__afterSignInUrl,
__afterSignUpUrl,
__clerkJSUrl,
__clerkJSVersion,
} = clerkState?.__internal_clerk_state || {};
Expand All @@ -68,6 +71,9 @@ export function ClerkProvider({ children, ...rest }: RemixClerkProviderProps): J
domain={__domain as any}
isSatellite={__isSatellite}
signInUrl={__signInUrl}
signUpUrl={__signUpUrl}
afterSignInUrl={__afterSignInUrl}
afterSignUpUrl={__afterSignUpUrl}
clerkJSUrl={__clerkJSUrl}
clerkJSVersion={__clerkJSVersion}
{...restProps}
Expand Down
3 changes: 3 additions & 0 deletions packages/remix/src/client/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,9 @@ export type ClerkState = {
__domain: string | undefined;
__isSatellite: boolean;
__signInUrl: string | undefined;
__signUpUrl: string | undefined;
__afterSignInUrl: string | undefined;
__afterSignUpUrl: string | undefined;
__clerk_debug: any;
__clerkJSUrl: string | undefined;
__clerkJSVersion: string | undefined;
Expand Down
18 changes: 18 additions & 0 deletions packages/remix/src/ssr/authenticateRequest.ts
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,21 @@ export function authenticateRequest(args: LoaderFunctionArgs, opts: RootAuthLoad
const signInUrl =
opts.signInUrl || getEnvVariable('CLERK_SIGN_IN_URL') || (context?.CLERK_SIGN_IN_URL as string) || '';

const signUpUrl =
opts.signUpUrl || getEnvVariable('CLERK_SIGN_UP_URL') || (context?.CLERK_SIGN_UP_URL as string) || '';

const afterSignInUrl =
opts.afterSignInUrl ||
getEnvVariable('CLERK_AFTER_SIGN_IN_URL') ||
(context?.CLERK_AFTER_SIGN_IN_URL as string) ||
'';

const afterSignUpUrl =
opts.afterSignUpUrl ||
getEnvVariable('CLERK_AFTER_SIGN_UP_URL') ||
(context?.CLERK_AFTER_SIGN_UP_URL as string) ||
'';

if (isSatellite && !proxyUrl && !domain) {
throw new Error(satelliteAndMissingProxyUrlAndDomain);
}
Expand All @@ -96,6 +111,9 @@ export function authenticateRequest(args: LoaderFunctionArgs, opts: RootAuthLoad
isSatellite,
domain,
signInUrl,
signUpUrl,
afterSignInUrl,
afterSignUpUrl,
request,
});
}
3 changes: 3 additions & 0 deletions packages/remix/src/ssr/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,9 @@ export type RootAuthLoaderOptions = {
loadOrganization?: boolean;
authorizedParties?: [];
signInUrl?: string;
signUpUrl?: string;
afterSignInUrl?: string;
afterSignUpUrl?: string;
} & Pick<VerifyTokenOptions, 'audience'> &
MultiDomainAndOrProxy;

Expand Down
3 changes: 3 additions & 0 deletions packages/remix/src/ssr/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -110,6 +110,9 @@ export const injectRequestStateIntoResponse = async (response: Response, request
__domain: requestState.domain,
__isSatellite: requestState.isSatellite,
__signInUrl: requestState.signInUrl,
__signUpUrl: requestState.signUpUrl,
__afterSignInUrl: requestState.afterSignInUrl,
__afterSignUpUrl: requestState.afterSignUpUrl,
__clerk_debug: debugRequestState(requestState),
__clerkJSUrl: getEnvVariable('CLERK_JS'),
__clerkJSVersion: getEnvVariable('CLERK_JS_VERSION'),
Expand Down

0 comments on commit 30fcdd5

Please sign in to comment.