Javascript
Examples for dynamic interactions
You will interact with the authenticators through an HTML page and Javascript using the Webauthn API.
A package is available at https://github.com/web-auth/webauthn-helper. It contains functions that will ease the interaction with the login or the registration endpoints.
It is mandatory to use the HTTPS scheme to use Webauthn otherwise it will not work.

Installation

You can use npm or yarn to install the package:
1
npm i @web-auth/webauthn-helper
2
#or
3
yarn add @web-auth/webauthn-helper
Copied!

Registration

1
// Import the registration hook
2
import {useRegistration} from 'webauthn-helper';
3
4
// Create your register function.
5
// By default the urls are "/register" and "/register/options"
6
// but you can change those urls if needed.
7
const register = useRegistration({
8
actionUrl: '/api/register',
9
optionsUrl: '/api/register/options'
10
});
11
12
13
// We can call this register function whenever we need (e.g. form submission)
14
register({
15
username: 'john.doe',
16
displayName: 'JD'
17
})
18
.then((response) => console.log('Registration success'))
19
.catch((error) => console.log('Registration failure'))
20
;
Copied!
Additional options can be set during the registration process. See the section “Deep into the framework” to know more. Hereafter another example:
1
register({
2
username: 'john.doe',
3
displayName: 'JD',
4
attestation: 'none',
5
authenticatorSelection: {
6
authenticatorAttachment: 'platform',
7
requireResidentKey: true,
8
userVerification: 'required'
9
}
10
})
11
.then((response) => console.log('Registration success'))
12
.catch((error) => console.log('Registration failure'))
13
;
Copied!
The specification Webauthn L2 deprecates the use of the parameter requireResidentKey; you should use residentKey instead with one of the following value: required, preferred or discouraged.
To have the same behavior as above, please use required.

Authentication

1
// Import the login hook
2
import {useLogin} from 'webauthn-helper';
3
4
// Create your login function.
5
// By default the urls are "/login" and "/login/options"
6
// but you can change those urls if needed.
7
const login = useLogin({
8
actionUrl: '/api/login',
9
optionsUrl: '/api/login/options'
10
});
11
12
13
// We can call this login function whenever we need (e.g. form submission)
14
login({
15
username: 'john.doe'
16
})
17
.then((response) => console.log('Authentication success'))
18
.catch((error) => console.log('Authentication failure'))
19
;
Copied!
As done during the registration, additional options are available. See the section “Deep into the framework” to know more. Hereafter another example:
1
login({
2
username: 'john.doe',
3
userVerification: 'required'
4
})
5
.then((response) => console.log('Authentication success'))
6
.catch((error) => console.log('Authentication failure'))
7
;
Copied!
Last modified 1mo ago
Export as PDF
Copy link