Skip to content

Commit b7baba2

Browse files
Add definition of prebuiltui
1 parent 04a6d99 commit b7baba2

File tree

3 files changed

+97
-1
lines changed

3 files changed

+97
-1
lines changed
Lines changed: 77 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,77 @@
1+
import { isTest } from "../../utils";
2+
import { RecipeRouter } from "../recipeRouter";
3+
4+
import { useRecipeComponentOverrideContext } from "./componentOverrideContext";
5+
import WebauthnRecipe from "./recipe";
6+
7+
import type { GenericComponentOverrideMap } from "../../components/componentOverride/componentOverrideContext";
8+
import type { AuthComponent, RecipeFeatureComponentMap } from "../../types";
9+
10+
export class WebauthnPreBuiltUI extends RecipeRouter {
11+
static instance?: WebauthnPreBuiltUI;
12+
languageTranslations = {
13+
en: {},
14+
};
15+
16+
constructor(public readonly recipeInstance: WebauthnRecipe) {
17+
super();
18+
}
19+
20+
// Static methods
21+
static getInstanceOrInitAndGetInstance(): WebauthnPreBuiltUI {
22+
if (WebauthnPreBuiltUI.instance === undefined) {
23+
const recipeInstance = WebauthnRecipe.getInstanceOrThrow();
24+
WebauthnPreBuiltUI.instance = new WebauthnPreBuiltUI(recipeInstance);
25+
}
26+
27+
return WebauthnPreBuiltUI.instance;
28+
}
29+
static getFeatures(
30+
useComponentOverrides: () => GenericComponentOverrideMap<any> = useRecipeComponentOverrideContext
31+
): RecipeFeatureComponentMap {
32+
return WebauthnPreBuiltUI.getInstanceOrInitAndGetInstance().getFeatures(useComponentOverrides);
33+
}
34+
static getFeatureComponent(
35+
componentName: "mfaWebauthn",
36+
props: any,
37+
useComponentOverrides: () => GenericComponentOverrideMap<any> = useRecipeComponentOverrideContext
38+
): JSX.Element {
39+
return WebauthnPreBuiltUI.getInstanceOrInitAndGetInstance().getFeatureComponent(
40+
componentName,
41+
props,
42+
useComponentOverrides
43+
);
44+
}
45+
46+
// Instance methods
47+
getFeatures = (
48+
useComponentOverrides: () => GenericComponentOverrideMap<any> = useRecipeComponentOverrideContext
49+
): RecipeFeatureComponentMap => {
50+
const features: RecipeFeatureComponentMap = {};
51+
// TODO: Define after components are defined
52+
return features;
53+
};
54+
55+
getFeatureComponent = (
56+
// eslint-disable-next-line @typescript-eslint/no-unused-vars
57+
_: "mfaWebauthn",
58+
props: any,
59+
useComponentOverrides: () => GenericComponentOverrideMap<any> = useRecipeComponentOverrideContext
60+
): JSX.Element => {
61+
return <div></div>;
62+
};
63+
64+
getAuthComponents(): AuthComponent[] {
65+
return [];
66+
}
67+
68+
// For tests
69+
static reset(): void {
70+
if (!isTest()) {
71+
return;
72+
}
73+
74+
WebauthnPreBuiltUI.instance = undefined;
75+
return;
76+
}
77+
}

lib/ts/recipe/webauthn/recipe.tsx

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515

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

18+
import { SSR_ERROR } from "../../constants";
1819
import AuthRecipe from "../authRecipe";
1920

2021
import { getFunctionOverrides } from "./functionOverrides";
@@ -101,4 +102,22 @@ export default class Webauthn extends AuthRecipe<
101102
}),
102103
};
103104
}
105+
106+
static getInstance(): Webauthn | undefined {
107+
return Webauthn.instance;
108+
}
109+
110+
static getInstanceOrThrow(): Webauthn {
111+
if (Webauthn.instance === undefined) {
112+
let error = "No instance of Webauthn found. Make sure to call the Webauthn.init method.";
113+
114+
// eslint-disable-next-line supertokens-auth-react/no-direct-window-object
115+
if (typeof window === "undefined") {
116+
error = error + SSR_ERROR;
117+
}
118+
throw Error(error);
119+
}
120+
121+
return Webauthn.instance;
122+
}
104123
}

lib/ts/recipe/webauthn/types.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -106,4 +106,4 @@ export type NormalisedConfig = Record<string, unknown> &
106106

107107
export type RecipeImplementation = WebJSRecipeInterface<typeof WebJSRecipe>;
108108

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

0 commit comments

Comments
 (0)