This guide showcases how to add passkeys as second factor for password-based logins. If your goal is to set up a passkey-based passwordless login system instead, please refer to this guide and the Passkey API documentation for more suitable instructions.
Install Passkey SDK
Install the Passkey SDK:Get your tenant ID and API key
Get your tenant ID and API key from Hanko Cloud and add them to your.env
file.
.env
Allow users to register passkeys for MFA
On your backend, you’ll have to calltenant({ ... }).user(userId).mfa.registration.initialize()
and mfa.registration.finalize()
to create and store a passkey for your user which will be used as an MFA.
services.js
controllers.js
Frontend
On your frontend, theregisterMfaPasskey()
function handles the passkey registration process. It first sends a request to the server to initiate the registration process and receives the response for creating a new passkey.
It then uses the @github/webauthn-json
library to create a new passkey credential based on the received options from the response. Finally, it sends another request to the server with the newly created credential to complete the registration process.
Authenticate users with MFA
services.js
controllers.js
Frontend
Feel free to customize the MFA flow based on your app’s requirements. In our example implementation, ifmfaRequired=true
is received from the login API, the user is redirected to an MFA page where passkey-based multi-factor authentication is performed.
Try it yourself
React-Express Example
Full source code available on our GitHub