Skip to content

Commit

Permalink
added missing docs for credential registration
Browse files Browse the repository at this point in the history
  • Loading branch information
niftyvictor committed Jan 9, 2025
1 parent af7ad0d commit af951fa
Show file tree
Hide file tree
Showing 3 changed files with 135 additions and 2 deletions.
1 change: 0 additions & 1 deletion v3/docs/authentication/webauthn/initial-setup.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -188,7 +188,6 @@ supertokens.init({
// apiKey: <YOUR_API_KEY>
},
appInfo: {
// learn more about this on https://supertokens.com/docs/emailpassword/appinfo
appName: "<YOUR_APP_NAME>",
apiDomain: "<YOUR_API_DOMAIN>",
websiteDomain: "<YOUR_WEBSITE_DOMAIN>",
Expand Down
2 changes: 1 addition & 1 deletion v3/docs/authentication/webauthn/introduction.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -115,4 +115,4 @@ Before implementing WebAuthn, ensure:

## Getting Started

For detailed implementation instructions, check out our [Quick Setup](/docs/authentication/webauthn/initial-setup) guide.
For detailed implementation instructions, check out our [Initial Setup](/docs/authentication/webauthn/initial-setup) guide.
134 changes: 134 additions & 0 deletions v3/docs/authentication/webauthn/registering-multiple-credentials.mdx
Original file line number Diff line number Diff line change
@@ -0,0 +1,134 @@
---
title: Registering Multiple Credentials
hide_title: true
sidebar_position: 4
---

import { UIType } from "/src/components/UITypeSwitch";

# Registering Multiple Credentials

## Overview

Registering multiple credentials is a feature that is useful in cases where the user has multiple devices and wants to register credentials on all of them.

For example, the user may want to register a passkey on their work laptop and another one on their personal laptop, both for the same account.

## Backend

By default, the backend exposes an API endpoint `registerCredential` that you can use to register a credential for an authenticated user.

```ts
import supertokens from "supertokens-node";
import Session from "supertokens-node/recipe/session";
import WebAuthN from "supertokens-node/recipe/webauthn";

supertokens.init({
// Replace this with the framework you are using
framework: "express",
supertokens: {
// We use try.supertokens for demo purposes.
// At the end of the tutorial we will show you how to create
// your own SuperTokens core instance and then update your config.
connectionURI: "https://try.supertokens.io",
// apiKey: <YOUR_API_KEY>
},
appInfo: {
appName: "<YOUR_APP_NAME>",
apiDomain: "<YOUR_API_DOMAIN>",
websiteDomain: "<YOUR_WEBSITE_DOMAIN>",
apiBasePath: "/auth",
websiteBasePath: "/auth",
},
recipeList: [
WebAuthN.init(),
Session.init()
]
});
```

## Frontend

<UIType.Switch />

<UIType.PrebuiltUIContent>

prebuilt

</UIType.PrebuiltUIContent>

<UIType.CustomUIContent>

### 1. Initialize the Frontend SDK


Call the SDK init function at the start of your application. The invocation includes the main configuration details, as well as the recipes that you will be using in your setup.

```ts

import SuperTokens from 'supertokens-web-js';
import Session from 'supertokens-web-js/recipe/session';
import WebAuthN from 'supertokens-web-js/recipe/webauthn'

SuperTokens.init({
appInfo: {
apiDomain: "",
apiBasePath: "",
appName: "...",
},
recipeList: [
Session.init(),
WebAuthN.init(),
],
});
```

### 2. Add the UI

You will have to first add the UI for the user to register a credential. This usually would mean adding a button that the user can click to start the registration process.

The button will have to call `registerCredential` to create the credential and send it to the backend.


```ts
import { registerCredential } from "supertokens-web-js/recipe/webauthn";

async function handleRegisterCredential(email: string) {
try {
let response = await registerCredential();

if (
response.status === "INVALID_AUTHENTICATOR_ERROR"
) {
// the reason string is a user friendly message
// about what went wrong. It can also contain a support code which users
// can tell you so you know why their sign in / up was not allowed.
window.alert(response.reason)
} else if (
response.status === "AUTHENTICATOR_ALREADY_REGISTERED" ||
response.status === "INVALID_GENERATED_OPTIONS_ERROR" ||
response.status === "INVALID_CREDENTIALS_ERROR" ||
response.status === "RECOVER_ACCOUNT_TOKEN_INVALID_ERROR" ||
response.status === "GENERATED_OPTIONS_NOT_FOUND_ERROR"
) {
// These errors represent various issues with the authenticator, credential or the flow itself.
// These should be handled individually by you.
// The user should be informed that they should retry the sign up process or get in touch with you.
window.alert("Please try again");
} else {
// User signed up successfully.
window.alert("Your credential has been registered successfully");
}
} catch (err: any) {
if (err.isSuperTokensGeneralError === true) {
// this may be a custom error message sent from the API by you,
// or if the input email / phone number is not valid.
window.alert(err.message);
} else {
window.alert("Oops! Something went wrong.");
}
}
}
```

</UIType.CustomUIContent>

0 comments on commit af951fa

Please sign in to comment.