Passkey MFA
Learn how to implement multi-factor authentication (MFA) with Passkey using Passkey SDK.
Passkey MFA is a multi-factor authentication (MFA) method that enhances account security by adding passkeys as a second factor to traditional password-based logins.
It ensures that only users with access to both the password and the registered device can successfully authenticate, providing an extra layer of protection against unauthorized access.
This example demonstrates how to implement MFA in Node.js using the @teamhanko/passkeys-sdk. If you’re not using a JS/TS backend, you can achieve the same result by utilizing the Passkey API directly.
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.
Allow users to register passkeys for MFA
On your backend, you’ll have to call tenant({ ... }).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.
Frontend
On your frontend, the registerMfaPasskey()
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
Frontend
Feel free to customize the MFA flow based on your app’s requirements. In our example implementation, if mfaRequired=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
Was this page helpful?