Skip to content

Commit 70ab209

Browse files
Refactor the code to create SignInTheme
1 parent 9a8242a commit 70ab209

File tree

4 files changed

+84
-46
lines changed

4 files changed

+84
-46
lines changed

lib/ts/recipe/webauthn/components/features/signIn/index.tsx

Lines changed: 3 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -20,12 +20,11 @@ import * as React from "react";
2020
import AuthComponentWrapper from "../../../../../components/authCompWrapper";
2121
import { useUserContext } from "../../../../../usercontext";
2222
import { useRethrowInRender } from "../../../../../utils";
23-
import { handleFormSubmit } from "../../../../emailpassword/components/library/functions/form";
2423
import { useSessionContext } from "../../../../session";
2524
import Session from "../../../../session/recipe";
26-
import { ContinueWithPasskeyTheme } from "../../themes/continueWithPasskey";
25+
import SignInTheme from "../../themes/signIn";
2726

28-
import type { UserContext, PartialAuthComponentProps, APIFormField } from "../../../../../types";
27+
import type { UserContext, PartialAuthComponentProps } from "../../../../../types";
2928
import type { AuthSuccessContext } from "../../../../authRecipe/types";
3029
import type Recipe from "../../../recipe";
3130
import type { ComponentOverrideMap, SignInThemeProps } from "../../../types";
@@ -109,46 +108,10 @@ const SignInFeatureInner: React.FC<
109108
props.onSignInUpSwitcherClick
110109
)!;
111110

112-
const callAPI = React.useCallback(
113-
async (_: APIFormField[], __: (id: string, value: string) => any) => {
114-
const email = prompt("Enter email ID");
115-
if (email === null) {
116-
alert("Please enter an email");
117-
return;
118-
}
119-
120-
const response = await childProps.recipeImplementation.authenticateCredentialWithSignIn({
121-
email: email,
122-
userContext,
123-
});
124-
125-
return response;
126-
},
127-
[childProps, userContext]
128-
);
129-
130-
// Define the code to handle sign in properly through this component.
131-
const handleWebauthnSignInClick = async () => {
132-
await handleFormSubmit({
133-
callAPI: callAPI,
134-
clearError: () => alert("Clear error"),
135-
onError: () => alert("Error"),
136-
onFetchError: () => alert("Fetch error"),
137-
onSuccess: (payload) => console.warn("payload: ", payload),
138-
});
139-
};
140-
141111
return (
142112
<React.Fragment>
143113
{/* No custom theme, use default. */}
144-
{props.children === undefined && (
145-
<ContinueWithPasskeyTheme
146-
{...props}
147-
continueWithPasskeyClicked={handleWebauthnSignInClick}
148-
config={props.recipe.config}
149-
continueFor="SIGN_IN"
150-
/>
151-
)}
114+
{props.children === undefined && <SignInTheme {...childProps} />}
152115

153116
{/* Otherwise, custom theme is provided, propagate props. */}
154117
{props.children &&

lib/ts/recipe/webauthn/components/themes/continueWithPasskey/index.tsx

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,6 @@ import SuperTokens from "../../../../../superTokens";
2020
import { Button } from "../../../../emailpassword/components/library";
2121
import { ThemeBase } from "../themeBase";
2222

23-
import type { PartialAuthComponentProps } from "../../../../../types";
2423
import type { ContinueFor, NormalisedConfig } from "../../../types";
2524

2625
interface ContinueWithPasskeyProps {
@@ -45,9 +44,7 @@ const ContinueWithPasskey: React.FC<ContinueWithPasskeyProps> = ({ continueFor,
4544

4645
const ContinueWithPasskeyWithOverride = withOverride("WebauthnContinueWithPasskey", ContinueWithPasskey);
4746

48-
export const ContinueWithPasskeyTheme = (
49-
props: PartialAuthComponentProps & { config: NormalisedConfig } & ContinueWithPasskeyProps
50-
) => {
47+
export const ContinueWithPasskeyTheme = (props: { config: NormalisedConfig } & ContinueWithPasskeyProps) => {
5148
const rootStyle = SuperTokens.getInstanceOrThrow().rootStyle;
5249
return (
5350
<ThemeBase userStyles={[rootStyle, props.config.recipeRootStyle]}>
Lines changed: 78 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,78 @@
1+
/* Copyright (c) 2024, VRAI Labs and/or its affiliates. All rights reserved.
2+
*
3+
* This software is licensed under the Apache License, Version 2.0 (the
4+
* "License") as published by the Apache Software Foundation.
5+
*
6+
* You may not use this file except in compliance with the License. You may
7+
* obtain a copy of the License at http://www.apache.org/licenses/LICENSE-2.0
8+
*
9+
* Unless required by applicable law or agreed to in writing, software
10+
* distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
11+
* WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
12+
* License for the specific language governing permissions and limitations
13+
* under the License.
14+
*/
15+
16+
import * as React from "react";
17+
18+
import SuperTokens from "../../../../../superTokens";
19+
import { useUserContext } from "../../../../../usercontext";
20+
import UserContextWrapper from "../../../../../usercontext/userContextWrapper";
21+
import { handleFormSubmit } from "../../../../emailpassword/components/library/functions/form";
22+
import { ContinueWithPasskeyTheme } from "../continueWithPasskey";
23+
import { ThemeBase } from "../themeBase";
24+
25+
import type { APIFormField } from "../../../../../types";
26+
import type { SignInThemeProps } from "../../../types";
27+
28+
function PasskeySignInTheme(props: SignInThemeProps): JSX.Element {
29+
const userContext = useUserContext();
30+
31+
const callAPI = React.useCallback(
32+
async (_: APIFormField[], __: (id: string, value: string) => any) => {
33+
const email = prompt("Enter email ID");
34+
if (email === null) {
35+
alert("Please enter an email");
36+
return;
37+
}
38+
39+
const response = await props.recipeImplementation.authenticateCredentialWithSignIn({
40+
email: email,
41+
userContext,
42+
});
43+
44+
return response;
45+
},
46+
[props, userContext]
47+
);
48+
49+
// Define the code to handle sign in properly through this component.
50+
const handleWebauthnSignInClick = async () => {
51+
await handleFormSubmit({
52+
callAPI: callAPI,
53+
clearError: () => alert("Clear error"),
54+
onError: () => alert("Error"),
55+
onFetchError: () => alert("Fetch error"),
56+
onSuccess: (payload) => console.warn("payload: ", payload),
57+
});
58+
};
59+
60+
const rootStyle = SuperTokens.getInstanceOrThrow().rootStyle;
61+
62+
const activeStyle = props.config.signUpFeature.style;
63+
64+
return (
65+
<UserContextWrapper userContext={props.userContext}>
66+
<ThemeBase userStyles={[rootStyle, props.config.recipeRootStyle, activeStyle]}>
67+
<ContinueWithPasskeyTheme
68+
{...props}
69+
continueWithPasskeyClicked={handleWebauthnSignInClick}
70+
config={props.config}
71+
continueFor="SIGN_IN"
72+
/>
73+
</ThemeBase>
74+
</UserContextWrapper>
75+
);
76+
}
77+
78+
export default PasskeySignInTheme;

lib/ts/recipe/webauthn/components/themes/signUp/index.tsx

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ import { SignUpForm, SignUpScreen } from "./signUpForm";
2626

2727
import type { SignUpThemeProps } from "../../../types";
2828

29-
function SignUpTheme(props: SignUpThemeProps): JSX.Element {
29+
function PasskeySignUpTheme(props: SignUpThemeProps): JSX.Element {
3030
const rootStyle = SuperTokens.getInstanceOrThrow().rootStyle;
3131

3232
const activeStyle = props.config.signUpFeature.style;
@@ -88,4 +88,4 @@ function SignUpTheme(props: SignUpThemeProps): JSX.Element {
8888
);
8989
}
9090

91-
export default SignUpTheme;
91+
export default PasskeySignUpTheme;

0 commit comments

Comments
 (0)