Skip to content

Commit

Permalink
Add definition of prebuiltui
Browse files Browse the repository at this point in the history
  • Loading branch information
deepjyoti30-st committed Dec 19, 2024
1 parent 04a6d99 commit b7baba2
Show file tree
Hide file tree
Showing 3 changed files with 97 additions and 1 deletion.
77 changes: 77 additions & 0 deletions lib/ts/recipe/webauthn/prebuiltui.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,77 @@
import { isTest } from "../../utils";
import { RecipeRouter } from "../recipeRouter";

import { useRecipeComponentOverrideContext } from "./componentOverrideContext";
import WebauthnRecipe from "./recipe";

import type { GenericComponentOverrideMap } from "../../components/componentOverride/componentOverrideContext";
import type { AuthComponent, RecipeFeatureComponentMap } from "../../types";

export class WebauthnPreBuiltUI extends RecipeRouter {
static instance?: WebauthnPreBuiltUI;
languageTranslations = {
en: {},
};

constructor(public readonly recipeInstance: WebauthnRecipe) {
super();
}

// Static methods
static getInstanceOrInitAndGetInstance(): WebauthnPreBuiltUI {
if (WebauthnPreBuiltUI.instance === undefined) {
const recipeInstance = WebauthnRecipe.getInstanceOrThrow();
WebauthnPreBuiltUI.instance = new WebauthnPreBuiltUI(recipeInstance);
}

return WebauthnPreBuiltUI.instance;
}
static getFeatures(
useComponentOverrides: () => GenericComponentOverrideMap<any> = useRecipeComponentOverrideContext
): RecipeFeatureComponentMap {
return WebauthnPreBuiltUI.getInstanceOrInitAndGetInstance().getFeatures(useComponentOverrides);
}
static getFeatureComponent(
componentName: "mfaWebauthn",
props: any,
useComponentOverrides: () => GenericComponentOverrideMap<any> = useRecipeComponentOverrideContext
): JSX.Element {
return WebauthnPreBuiltUI.getInstanceOrInitAndGetInstance().getFeatureComponent(
componentName,
props,
useComponentOverrides
);
}

// Instance methods
getFeatures = (
useComponentOverrides: () => GenericComponentOverrideMap<any> = useRecipeComponentOverrideContext
): RecipeFeatureComponentMap => {
const features: RecipeFeatureComponentMap = {};
// TODO: Define after components are defined
return features;
};

getFeatureComponent = (
// eslint-disable-next-line @typescript-eslint/no-unused-vars
_: "mfaWebauthn",
props: any,
useComponentOverrides: () => GenericComponentOverrideMap<any> = useRecipeComponentOverrideContext
): JSX.Element => {
return <div></div>;
};

getAuthComponents(): AuthComponent[] {
return [];
}

// For tests
static reset(): void {
if (!isTest()) {
return;
}

WebauthnPreBuiltUI.instance = undefined;
return;
}
}
19 changes: 19 additions & 0 deletions lib/ts/recipe/webauthn/recipe.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@

import WebauthnWebJS from "supertokens-web-js/lib/build/recipe/webauthn";

import { SSR_ERROR } from "../../constants";
import AuthRecipe from "../authRecipe";

import { getFunctionOverrides } from "./functionOverrides";
Expand Down Expand Up @@ -101,4 +102,22 @@ export default class Webauthn extends AuthRecipe<
}),
};
}

static getInstance(): Webauthn | undefined {
return Webauthn.instance;
}

static getInstanceOrThrow(): Webauthn {
if (Webauthn.instance === undefined) {
let error = "No instance of Webauthn found. Make sure to call the Webauthn.init method.";

// eslint-disable-next-line supertokens-auth-react/no-direct-window-object
if (typeof window === "undefined") {
error = error + SSR_ERROR;
}
throw Error(error);
}

return Webauthn.instance;
}
}
2 changes: 1 addition & 1 deletion lib/ts/recipe/webauthn/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -106,4 +106,4 @@ export type NormalisedConfig = Record<string, unknown> &

export type RecipeImplementation = WebJSRecipeInterface<typeof WebJSRecipe>;

export type ComponentOverrideMap = Record<string, unknown>;
export type ComponentOverrideMap = Record<string, undefined>;

0 comments on commit b7baba2

Please sign in to comment.