From f78b025cda558ff212d024cf27a1521a76b6a72b Mon Sep 17 00:00:00 2001 From: Deepjyoti Barman Date: Mon, 9 Dec 2024 16:08:47 +0530 Subject: [PATCH] Add wrapper definitions for sign in with options and signup --- lib/ts/recipe/webauthn/index.ts | 80 +++++++++++++++++++++++++++++++-- 1 file changed, 77 insertions(+), 3 deletions(-) diff --git a/lib/ts/recipe/webauthn/index.ts b/lib/ts/recipe/webauthn/index.ts index b13f3d8..f3407ac 100644 --- a/lib/ts/recipe/webauthn/index.ts +++ b/lib/ts/recipe/webauthn/index.ts @@ -13,10 +13,11 @@ * under the License. */ +import { GeneralErrorResponse, User } from "../../types"; import { getNormalisedUserContext } from "../../utils"; -import { RecipeFunctionOptions } from "../emailpassword"; +import { RecipeFunctionOptions } from "../recipeModule/types"; import Recipe from "./recipe"; -import { ResidentKey, UserInput, UserVerification } from "./types"; +import { CredentialPayload, ResidentKey, UserInput, UserVerification } from "./types"; export default class RecipeWrapper { static init(config?: UserInput) { @@ -35,7 +36,7 @@ export default class RecipeWrapper { * * @param options (OPTIONAL) Use this to configure additional properties (for example pre api hooks) * - * @returns `{ status: "OK", ...}` if successful along a description of the created device (secret, etc.) + * @returns `{ status: "OK", ...}` if successful along a description of the created webauthn details (challenge, etc.) */ static registerOptions( input: { options?: RecipeFunctionOptions; userContext: any } & ( @@ -93,6 +94,79 @@ export default class RecipeWrapper { userContext: getNormalisedUserContext(input?.userContext), }); } + + /** + * TODO: Add description once Victor shares the difference between registerOptions and this. + * + * @param email Email to add signin options against. + * + * @param userContext (OPTIONAL) Refer to {@link https://supertokens.com/docs/emailpassword/advanced-customizations/user-context the documentation} + * + * @param options (OPTIONAL) Use this to configure additional properties (for example pre api hooks) + * + * @returns `{ status: "OK", ...}` if successful along a description of the webauthn options (challenge, etc.) + */ + static signinOptions(input: { email: string; options?: RecipeFunctionOptions; userContext: any }): Promise< + | { + status: "OK"; + webauthnGeneratedOptionsId: string; + challenge: string; + timeout: number; + userVerification: UserVerification; + fetchResponse: Response; + } + | { + status: "INVALID_GENERATED_OPTIONS_ERROR"; + fetchResponse: Response; + } + | GeneralErrorResponse + > { + return Recipe.getInstanceOrThrow().recipeImplementation.signInOptions({ + ...input, + userContext: getNormalisedUserContext(input?.userContext), + }); + } + + /** + * Signup to ST with the webauthn options ID and the credential received from the + * device. + * + * @param webauthnGeneratedOptionsId ID of the stored options + * + * @param credential Details of the credential + * + * @param userContext (OPTIONAL) Refer to {@link https://supertokens.com/docs/emailpassword/advanced-customizations/user-context the documentation} + * + * @param options (OPTIONAL) Use this to configure additional properties (for example pre api hooks) + * + * @returns `{ status: "OK", ...}` if successful along a description of the user details (id, etc.) + */ + static signUp(input: { + webauthnGeneratedOptionsId: string; + credential: CredentialPayload; + options?: RecipeFunctionOptions; + userContext: any; + }): Promise< + | { + status: "OK"; + user: User; + } + | GeneralErrorResponse + | { + status: "SIGN_UP_NOT_ALLOWED"; + reason: string; + } + | { status: "INVALID_CREDENTIALS_ERROR" } + | { status: "GENERATED_OPTIONS_NOT_FOUND_ERROR" } + | { status: "INVALID_GENERATED_OPTIONS_ERROR" } + | { status: "INVALID_AUTHENTICATOR_ERROR"; reason: string } + | { status: "EMAIL_ALREADY_EXISTS_ERROR" } + > { + return Recipe.getInstanceOrThrow().recipeImplementation.signUp({ + ...input, + userContext: input?.userContext, + }); + } } const init = RecipeWrapper.init;