Skip to content

Commit

Permalink
Add init definition for handling registration and signup
Browse files Browse the repository at this point in the history
  • Loading branch information
deepjyoti30-st committed Dec 10, 2024
1 parent 8a26ab1 commit 98a116b
Show file tree
Hide file tree
Showing 4 changed files with 54 additions and 2 deletions.
41 changes: 40 additions & 1 deletion lib/ts/recipe/webauthn/recipeImplementation.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ import { RecipeFunctionOptions, RecipeImplementationInput } from "../recipeModul
import { PreAndPostAPIHookAction } from "./types";
import { GeneralErrorResponse, User } from "../../types";
import Multitenancy from "../multitenancy/recipe";
import { RegistrationResponseJSON, startRegistration } from "@simplewebauthn/browser";

export default function getRecipeImplementation(
recipeImplInput: RecipeImplementationInput<PreAndPostAPIHookAction>
Expand Down Expand Up @@ -353,11 +354,49 @@ export default function getRecipeImplementation(
const registrationOptions = await this.registerOptions({ options, userContext, email });
if (registrationOptions?.status !== "OK") {
// If we did not get an OK status, we need to return the error as is.

// If the `status` is `RECOVER_ACCOUNT_TOKEN_INVALID_ERROR`, we need to throw an
// error since that should never happen as we are registering with an email
// and not a token.
if (registrationOptions?.status === "RECOVER_ACCOUNT_TOKEN_INVALID_ERROR") {
throw new Error("Got `RECOVER_ACCOUNT_TOKEN_INVALID_ERROR` status that should never happen");
}

return registrationOptions;
}

// We should have received a valid registration options response.
// TODO: Pass the registration options to simplewebauthn
let registrationResponse: RegistrationResponseJSON;
try {
registrationResponse = await startRegistration({ optionsJSON: registrationOptions });
} catch (error: any) {
if (error.name === "InvalidStateError") {
return { status: "AUTHENTICATOR_ALREADY_REGISTERED" };
}

throw error;
}

// We should have a valid registration response for the passed credentials
// and we are good to go ahead and verify them.
return await this.signUp({
webauthnGeneratedOptionsId: registrationOptions.webauthnGeneratedOptionsId,
credential: {
id: registrationResponse.id,
rawId: registrationResponse.rawId,
response: {
clientDataJSON: registrationResponse.response.clientDataJSON,
attestationObject: registrationResponse.response.attestationObject,
transports: registrationResponse.response.transports,
userHandle: "TBD", // TODO: Fetch from the response
},
authenticatorAttachment: registrationResponse.authenticatorAttachment || "platform", // TODO: Fix acc to what Victor suggests
type: registrationResponse.type,
clientExtensionResults: {}, // TODO: Fetch from the response.
},
options,
userContext,
});
},
};
}
Expand Down
1 change: 1 addition & 0 deletions lib/ts/recipe/webauthn/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -250,6 +250,7 @@ export type RecipeInterface = {
| { status: "INVALID_GENERATED_OPTIONS_ERROR"; fetchResponse: Response }
| { status: "INVALID_AUTHENTICATOR_ERROR"; reason: string; fetchResponse: Response }
| { status: "EMAIL_ALREADY_EXISTS_ERROR"; fetchResponse: Response }
| { status: "AUTHENTICATOR_ALREADY_REGISTERED" }
>;
authenticateAndSignIn: (input: { email: string; options?: RecipeFunctionOptions; userContext: any }) => Promise<
| {
Expand Down
13 changes: 12 additions & 1 deletion package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@
},
"homepage": "https://github.com/supertokens/supertokens-web-js#readme",
"dependencies": {
"@simplewebauthn/browser": "^13.0.0",
"supertokens-js-override": "0.0.4",
"supertokens-website": "^20.1.5"
},
Expand Down

0 comments on commit 98a116b

Please sign in to comment.