Passkey logins have proven to be a more secure and smoother way for users to sign in to applications. If you're looking to add a passkey authentication to your Vue.js app, you're in the right place. In this tutorial, we'll walk you through the process of integrating a passkey login into your Vue.js app using the Hanko Passkey API.
To bring passkeys to your app, you'll typically need two things:
To make things even simpler, we have created a sample app that you can use as a reference throughout the tutorial.
Make sure you have the passkey SDK installed in your backend.
The startServerPasskeyRegistration
function is an asynchronous function that takes a userID
parameter. It performs the following steps:
db.users
array based on the provided userID
.passkeyApi.registration.initialize
method with the user's ID and email (or an empty string if email is not available) to initialize the passkey registration process.createOptions
object received from the SDK, which contains the necessary data to proceed with the registration on the client-side.The finishServerPasskeyRegistration
function is another asynchronous function that takes a credential
parameter. It simply calls the passkeyApi.registration.finalize
method with the provided credential
to complete the passkey registration process on the server-side.
These functions act as service-level functions that encapsulate the logic for interacting with the Hanko Passkey SDK. They can be called from controller functions, which in turn are invoked by the corresponding routes in your application.
The handlePasskeyRegister()
controller function first extracts the user
object from the request (req
) and retrieves the userID
from it.
Next, it extracts the start
, finish
, and credential
properties from the request body. These properties are used to determine the specific action to be performed.
If the start
property is present, the controller function calls thestartServerPasskeyRegistration
service function, passing the userID
as an argument. This function initializes the passkey registration process and returns the createOptions
object. The controller then sends the createOptions
back to the client as a JSON response.
If the finish
property is present, the controller function calls the finishServerPasskeyRegistration
service function, passing the credential
obtained from the request body. This function finalizes the passkey registration process on the server-side. Once the registration is complete, the controller sends a JSON response with a success message.
Now, we setup a /passkeys/register
POST route. When a POST request is made to this, the handlePasskeyRegister
controller function is invoked to handle the passkey registration process.
Make sure to install the @github/webauthn-json
library in your frontend.
Next, create a registerPasskey
function that will be called when the user clicks a button to register a new passkey. It handles the client-side process of registering a passkey.
The function starts by sending a POST request to the server at the endpoint /api/passkeys/register
to initiate the passkey registration process. The request includes a JSON payload with the following properties:
start: true
: Indicates that the registration process is starting.finish: false
: Indicates that the registration process is not yet finished.credential: null
: Initially, no credential is provided.The function then awaits the response from the server and extracts the createOptions
from the response JSON.
Next, the function calls the create
function from @github/webauthn-json
library, passing the createOptions
as an argument.
After obtaining the credential
, the function sends another POST request to the same endpoint, but this time with a different payload:
start: false
: Indicates that the registration process is no longer starting.finish: true
: Indicates that the registration process is finished.credential
: The newly created passkey credential.Finally, if the response is successful, a passkey is registered for the user.
Now, to make it work we're missing one crucial step: getting the Tenant ID
and API key secret
from Hanko Cloud. For that, navigate over to Hanko, create an account, and set up your organization. Navigate to 'Create new project', select 'Passkey Infrastructure', and provide your project name and URL.
Note: It's recommended to create separate projects for your development and production environments. This way, you can use your frontend localhost URL for the development project and your production URL for the live project, ensuring a smooth transition between environments without the need to modify the 'App URL' later.
Obtain your Tenant ID
and create an API key, then add the Tenant ID
and API key secret
to your backend's '.env' file.
Now that you have your secrets added to your backend's .env
file, you should be all set to register a passkey. Go ahead and give it a try!
To enable passkey login, we'll create two more service functions to initiate and finish the login process.
Now, similar to handlePasskeyRegister
create a handlePasskeyLogin
controller.
This function handles process of logging in with a passkey. It receives a request with start
, finish
, and options
properties. If start
is true, it initiates the login process by calling startServerPasskeyLogin()
and returns the login options. If finish
is true, it completes the login process by calling finishServerPasskeyLogin()
with the provided options
. It then retrieves the user ID from the JWT token, finds the corresponding user in the database, creates a session ID, sets the user for the session, and logs them in.
In the /passkeys/login
POST route, we invoke the above controller.
Now we create a signInWithPasskey
function. This function will interact with the backend APIs to initiate and finalize the passkey login process.
Congratulations! You now have a fully functional passkey login in your Vue.js app, significantly enhancing the user experience and making the authentication process more seamless for your users 🚀 Feel free to reach out to us on Discord, if you get into any issues.
Github repo: https://github.com/teamhanko/passkeys-vue-express