Next.js with NextAuth (now known as Auth.js)
Learn how to add passkeys to your Next.js app which already uses Auth.js for authentication.
Install passkey provider
Once you’ve initialized your Next app with NextAuth, install passkey provider and the webauthn-json package.
Get your tenant ID and API key
Get your tenant ID and API key from Hanko Cloud and add them to your .env.local
file.
If you’re self-hosting:
- make sure to pass the
baseUrl
to bothtenant
(in[...nextauth].ts
) andsignInWithPasskey()
(in your component). - get your tenant ID via the admin API
Add `PasskeyProvider`
Allow your users to register passkeys as a login method for their account
Your users will have to add passkeys to their account somehow. It’s up to you how and where you let them do this, but typically this would be a button on an “Account Settings” page.
On your backend, you’ll have to call tenant({ ... }).registration.initialize()
and .registration.finalize()
to create and store a passkey for your user.
On your frontend, you’ll have to call create()
from @github/webauthn-json
with the object .registration.initialize()
returned.
create()
will return a PublicKeyCredential
object, which you’ll have to pass to .registration.finalize()
.
Backend:
Frontend:
Allow your users to log in with passkeys
Let’s add a button that triggers the “Sign in with passkey” dialog:
Optional: Autofill support
If you don’t want to add a button just for passkeys, you can add autofill support to your username (or email) fields:
Clicking on any passkey in the autofill popup will immediately log the user in, going through the same flow as if they had clicked the “Sign in with passkey” button earlier in this guide.
To add autofill support:
- add
autoComplete="username webauthn"
to your username field - call
signInWithPasskey.conditional()
when the login form loads
Example:
Was this page helpful?