-
Notifications
You must be signed in to change notification settings - Fork 76
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
added missing docs for credential registration
- Loading branch information
1 parent
af7ad0d
commit af951fa
Showing
3 changed files
with
135 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
134 changes: 134 additions & 0 deletions
134
v3/docs/authentication/webauthn/registering-multiple-credentials.mdx
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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> |