Skip to content

Commit

Permalink
feat(backend,nextjs,utils): Remove interstitial
Browse files Browse the repository at this point in the history
  • Loading branch information
nikosdouvlis committed Nov 29, 2023
1 parent 89bc8ab commit c5a06aa
Show file tree
Hide file tree
Showing 12 changed files with 292 additions and 463 deletions.
2 changes: 1 addition & 1 deletion packages/backend/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ export type ClerkOptions = CreateBackendApiOptions &
export function Clerk(options: ClerkOptions) {
const opts = { ...options };
const apiClient = createBackendApiClient(opts);
const requestState = createAuthenticateRequest({ options: opts, apiClient });
const requestState = createAuthenticateRequest({ options: opts });
const telemetry = new TelemetryCollector({
...options.telemetry,
publishableKey: opts.publishableKey,
Expand Down
70 changes: 9 additions & 61 deletions packages/backend/src/tokens/authStatus.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,8 @@ import type { TokenVerificationErrorReason } from './errors';
export enum AuthStatus {
SignedIn = 'signed-in',
SignedOut = 'signed-out',
Interstitial = 'interstitial',
Unknown = 'unknown',
// will not be used
Handshake = 'handshake',
}

export type SignedInState = {
Expand All @@ -24,9 +24,6 @@ export type SignedInState = {
signUpUrl: string;
afterSignInUrl: string;
afterSignUpUrl: string;
isSignedIn: true;
isInterstitial: false;
isUnknown: false;
toAuth: () => SignedInAuthObject;
};

Expand All @@ -42,24 +39,14 @@ export type SignedOutState = {
signUpUrl: string;
afterSignInUrl: string;
afterSignUpUrl: string;
isSignedIn: false;
isInterstitial: false;
isUnknown: false;
toAuth: () => SignedOutAuthObject;
};

export type InterstitialState = Omit<SignedOutState, 'isInterstitial' | 'status' | 'toAuth'> & {
status: AuthStatus.Interstitial;
isInterstitial: true;
export type HandshakeState = Omit<SignedOutState, 'status' | 'toAuth'> & {
status: AuthStatus.Handshake;
toAuth: () => null;
};

export type UnknownState = Omit<InterstitialState, 'status' | 'isInterstitial' | 'isUnknown'> & {
status: AuthStatus.Unknown;
isInterstitial: false;
isUnknown: true;
};

export enum AuthErrorReason {
CookieAndUATMissing = 'cookie-and-uat-missing',
CookieMissing = 'cookie-missing',
Expand All @@ -79,7 +66,7 @@ export enum AuthErrorReason {

export type AuthReason = AuthErrorReason | TokenVerificationErrorReason;

export type RequestState = SignedInState | SignedOutState | InterstitialState | UnknownState;
export type RequestState = SignedInState | SignedOutState | HandshakeState;

type LoadResourcesOptions = {
loadSession?: boolean;
Expand Down Expand Up @@ -179,12 +166,10 @@ export async function signedIn<T extends AuthStatusOptionsType>(
signUpUrl,
afterSignInUrl,
afterSignUpUrl,
isSignedIn: true,
isInterstitial: false,
isUnknown: false,
toAuth: () => authObject,
};
}

export function signedOut<T extends AuthStatusOptionsType>(
options: T,
reason: AuthReason,
Expand Down Expand Up @@ -213,49 +198,15 @@ export function signedOut<T extends AuthStatusOptionsType>(
signUpUrl,
afterSignInUrl,
afterSignUpUrl,
isSignedIn: false,
isInterstitial: false,
isUnknown: false,
toAuth: () => signedOutAuthObject({ ...options, status: AuthStatus.SignedOut, reason, message }),
};
}

export function interstitial<T extends AuthStatusOptionsType>(
export function handshake<T extends AuthStatusOptionsType>(
options: T,
reason: AuthReason,
message = '',
): InterstitialState {
const {
publishableKey = '',
proxyUrl = '',
isSatellite = false,
domain = '',
signInUrl = '',
signUpUrl = '',
afterSignInUrl = '',
afterSignUpUrl = '',
} = options;

return {
status: AuthStatus.Interstitial,
reason,
message,
publishableKey,
isSatellite,
domain,
proxyUrl,
signInUrl,
signUpUrl,
afterSignInUrl,
afterSignUpUrl,
isSignedIn: false,
isInterstitial: true,
isUnknown: false,
toAuth: () => null,
};
}

export function unknownState(options: AuthStatusOptionsType, reason: AuthReason, message = ''): UnknownState {
): HandshakeState {
const {
publishableKey = '',
proxyUrl = '',
Expand All @@ -268,7 +219,7 @@ export function unknownState(options: AuthStatusOptionsType, reason: AuthReason,
} = options;

return {
status: AuthStatus.Unknown,
status: AuthStatus.Handshake,
reason,
message,
publishableKey,
Expand All @@ -279,9 +230,6 @@ export function unknownState(options: AuthStatusOptionsType, reason: AuthReason,
signUpUrl,
afterSignInUrl,
afterSignUpUrl,
isSignedIn: false,
isInterstitial: false,
isUnknown: true,
toAuth: () => null,
};
}
2 changes: 0 additions & 2 deletions packages/backend/src/tokens/errors.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,8 +22,6 @@ export enum TokenVerificationErrorReason {
RemoteJWKMissing = 'jwk-remote-missing',

JWKFailedToResolve = 'jwk-failed-to-resolve',

RemoteInterstitialFailedToLoad = 'interstitial-remote-failed-to-load',
}

export enum TokenVerificationErrorAction {
Expand Down
15 changes: 0 additions & 15 deletions packages/backend/src/tokens/factory.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,4 @@
import type { ApiClient } from '../api';
import { mergePreDefinedOptions } from '../util/mergePreDefinedOptions';
import type { LoadInterstitialOptions } from './interstitial';
import { loadInterstitialFromLocal } from './interstitial';
import type { AuthenticateRequestOptions } from './request';
import { authenticateRequest as authenticateRequestOriginal, debugRequestState } from './request';

Expand Down Expand Up @@ -36,11 +33,9 @@ const defaultOptions = {

export type CreateAuthenticateRequestOptions = {
options: BuildTimeOptions;
apiClient: ApiClient;
};

export function createAuthenticateRequest(params: CreateAuthenticateRequestOptions) {
const { apiClient } = params;
const buildTimeOptions = mergePreDefinedOptions(defaultOptions, params.options);

const authenticateRequest = (options: RunTimeOptions) => {
Expand All @@ -56,18 +51,8 @@ export function createAuthenticateRequest(params: CreateAuthenticateRequestOptio
});
};

const localInterstitial = (options: Omit<LoadInterstitialOptions, 'apiUrl'>) => {
const runTimeOptions = mergePreDefinedOptions(buildTimeOptions, options);
return loadInterstitialFromLocal({ ...options, ...runTimeOptions });
};

// TODO: Replace this function with remotePublicInterstitial
const remotePrivateInterstitial = () => apiClient.interstitial.getInterstitial();

return {
authenticateRequest,
localInterstitial,
remotePrivateInterstitial,
debugRequestState,
};
}
Loading

0 comments on commit c5a06aa

Please sign in to comment.