Skip to content

Commit

Permalink
feat(clerk-js,types): Support afterSignOutUrl and related methods in …
Browse files Browse the repository at this point in the history
…Clerk instance
  • Loading branch information
dimkl committed Dec 19, 2023
1 parent ebafd1a commit bce4ca7
Show file tree
Hide file tree
Showing 4 changed files with 87 additions and 44 deletions.
18 changes: 17 additions & 1 deletion packages/clerk-js/src/core/clerk.ts
Original file line number Diff line number Diff line change
Expand Up @@ -133,6 +133,7 @@ const defaultOptions: ClerkOptions = {
signUpUrl: undefined,
afterSignInUrl: undefined,
afterSignUpUrl: undefined,
afterSignOutUrl: undefined,
};

export class Clerk implements ClerkInterface {
Expand Down Expand Up @@ -303,7 +304,7 @@ export class Clerk implements ClerkInterface {
}
const opts = callbackOrOptions && typeof callbackOrOptions === 'object' ? callbackOrOptions : options || {};

const redirectUrl = opts?.redirectUrl || '/';
const redirectUrl = opts?.redirectUrl || this.buildAfterSignOutUrl();
const defaultCb = () => this.navigate(redirectUrl);
const cb = typeof callbackOrOptions === 'function' ? callbackOrOptions : defaultCb;

Expand Down Expand Up @@ -761,6 +762,14 @@ export class Clerk implements ClerkInterface {
return this.buildUrlWithAuth(this.#options.afterSignUpUrl);
}

public buildAfterSignOutUrl(): string {
if (!this.#options.afterSignOutUrl) {
return '/';
}

return this.buildUrlWithAuth(this.#options.afterSignOutUrl);
}

public buildCreateOrganizationUrl(): string {
if (!this.#environment || !this.#environment.displayConfig) {
return '';
Expand Down Expand Up @@ -851,6 +860,13 @@ export class Clerk implements ClerkInterface {
return;
};

public redirectToAfterSignOut = async (): Promise<unknown> => {
if (inBrowser()) {
return this.navigate(this.buildAfterSignOutUrl());
}
return;
};

public handleEmailLinkVerification = async (
params: HandleEmailLinkVerificationParams,
customNavigate?: (to: string) => Promise<unknown>,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -232,7 +232,7 @@ export const useUserButtonContext = () => {
const afterMultiSessionSingleSignOutUrl = ctx.afterMultiSessionSingleSignOutUrl || displayConfig.afterSignOutOneUrl;
const navigateAfterMultiSessionSingleSignOut = () => clerk.redirectWithAuth(afterMultiSessionSingleSignOutUrl);

const afterSignOutUrl = ctx.afterSignOutUrl || '/';
const afterSignOutUrl = ctx.afterSignOutUrl || clerk.buildAfterSignOutUrl();
const navigateAfterSignOut = () => navigate(afterSignOutUrl);

const afterSwitchSessionUrl = ctx.afterSwitchSessionUrl || displayConfig.afterSwitchSessionUrl;
Expand Down
21 changes: 21 additions & 0 deletions packages/react/src/isomorphicClerk.ts
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,7 @@ type IsomorphicLoadedClerk = Without<
| 'buildOrganizationProfileUrl'
| 'buildAfterSignUpUrl'
| 'buildAfterSignInUrl'
| 'buildAfterSignOutUrl'
| 'buildUrlWithAuth'
| 'handleRedirectCallback'
| 'handleUnauthenticated'
Expand Down Expand Up @@ -115,6 +116,8 @@ type IsomorphicLoadedClerk = Without<
buildAfterSignInUrl: () => string | void;
// TODO: Align return type
buildAfterSignUpUrl: () => string | void;
// TODO: Align return type
buildAfterSignOutUrl: () => string | void;
// TODO: Align optional props
mountUserButton: (node: HTMLDivElement, props: UserButtonProps) => void;
mountOrganizationList: (node: HTMLDivElement, props: OrganizationListProps) => void;
Expand Down Expand Up @@ -278,6 +281,15 @@ export class IsomorphicClerk implements IsomorphicLoadedClerk {
}
};

buildAfterSignOutUrl = (): string | void => {
const callback = () => this.clerkjs?.buildAfterSignOutUrl() || '';
if (this.clerkjs && this.#loaded) {
return callback();
} else {
this.premountMethodCalls.set('buildAfterSignOutUrl', callback);
}
};

buildUserProfileUrl = (): string | void => {
const callback = () => this.clerkjs?.buildUserProfileUrl() || '';
if (this.clerkjs && this.#loaded) {
Expand Down Expand Up @@ -835,6 +847,15 @@ export class IsomorphicClerk implements IsomorphicLoadedClerk {
}
};

redirectToAfterSignOut = (): void => {
const callback = () => this.clerkjs?.redirectToAfterSignOut();
if (this.clerkjs && this.#loaded) {
callback();
} else {
this.premountMethodCalls.set('redirectToAfterSignOut', callback);
}
};

redirectToOrganizationProfile = async (): Promise<unknown> => {
const callback = () => this.clerkjs?.redirectToOrganizationProfile();
if (this.clerkjs && this.#loaded) {
Expand Down
90 changes: 48 additions & 42 deletions packages/types/src/clerk.ts
Original file line number Diff line number Diff line change
Expand Up @@ -345,15 +345,20 @@ export interface Clerk {
buildOrganizationProfileUrl(): string;

/**
* Returns the configured afterSignIn url of the instance.
* Returns the configured afterSignInUrl of the instance.
*/
buildAfterSignInUrl(): string;

/**
* Returns the configured afterSignIn url of the instance.
* Returns the configured afterSignInUrl of the instance.
*/
buildAfterSignUpUrl(): string;

/**
* Returns the configured afterSignOutUrl of the instance.
*/
buildAfterSignOutUrl(): string;

/**
*
* Redirects to the provided url after decorating it with the auth token for development instances.
Expand Down Expand Up @@ -401,6 +406,11 @@ export interface Clerk {
*/
redirectToAfterSignUp: () => void;

/**
* Redirects to the configured afterSignOut URL.
*/
redirectToAfterSignOut: () => void;

/**
* Completes an OAuth or SAML redirection flow started by
* {@link Clerk.client.signIn.authenticateWithRedirect} or {@link Clerk.client.signUp.authenticateWithRedirect}
Expand Down Expand Up @@ -439,17 +449,7 @@ export interface Clerk {
handleUnauthenticated: () => Promise<unknown>;
}

export type HandleOAuthCallbackParams = {
/**
* Full URL or path to navigate after successful sign up.
*/
afterSignUpUrl?: string | null;

/**
* Full URL or path to navigate after successful sign in.
*/
afterSignInUrl?: string | null;

export type HandleOAuthCallbackParams = AfterActionURLs & {
/**
* Full URL or path to navigate after successful sign in
* or sign up.
Expand Down Expand Up @@ -517,35 +517,34 @@ type ClerkOptionsNavigation = ClerkOptionsNavigationFn & {
routerDebug?: boolean;
};

export type ClerkOptions = ClerkOptionsNavigation & {
appearance?: Appearance;
localization?: LocalizationResource;
polling?: boolean;
selectInitialSession?: (client: ClientResource) => ActiveSessionResource | null;
/** Controls if ClerkJS will load with the standard browser setup using Clerk cookies */
standardBrowser?: boolean;
/** Optional support email for display in authentication screens */
supportEmail?: string;
touchSession?: boolean;
signInUrl?: string;
signUpUrl?: string;
afterSignInUrl?: string;
afterSignUpUrl?: string;
allowedRedirectOrigins?: Array<string | RegExp>;
isSatellite?: boolean | ((url: URL) => boolean);
export type ClerkOptions = ClerkOptionsNavigation &
AfterActionURLs & {
appearance?: Appearance;
localization?: LocalizationResource;
polling?: boolean;
selectInitialSession?: (client: ClientResource) => ActiveSessionResource | null;
/** Controls if ClerkJS will load with the standard browser setup using Clerk cookies */
standardBrowser?: boolean;
/** Optional support email for display in authentication screens */
supportEmail?: string;
touchSession?: boolean;
signInUrl?: string;
signUpUrl?: string;
allowedRedirectOrigins?: Array<string | RegExp>;
isSatellite?: boolean | ((url: URL) => boolean);

/**
* Telemetry options
*/
telemetry?:
| false
| {
disabled?: boolean;
debug?: boolean;
};

sdkMetadata?: SDKMetadata;
};
/**
* Telemetry options
*/
telemetry?:
| false
| {
disabled?: boolean;
debug?: boolean;
};

sdkMetadata?: SDKMetadata;
};

export interface NavigateOptions {
replace?: boolean;
Expand Down Expand Up @@ -576,7 +575,7 @@ export type SignUpInitialValues = {
username?: string;
};

export type RedirectOptions = {
type AfterActionURLs = {
/**
* Full URL or path to navigate after successful sign in.
*/
Expand All @@ -588,6 +587,13 @@ export type RedirectOptions = {
*/
afterSignUpUrl?: string | null;

/**
* Full URL or path to navigate after successful sign out.
*/
afterSignOutUrl?: string | null;
};

export type RedirectOptions = AfterActionURLs & {
/**
* Full URL or path to navigate after successful sign in,
* or sign up.
Expand Down

0 comments on commit bce4ca7

Please sign in to comment.