Home Technology peripherals It Industry Developer Guide: How to Implement Passkeys

Developer Guide: How to Implement Passkeys

Feb 08, 2025 am 09:55 AM

This guide shows developers how to add passkey-based authentication for better security and user experience. It explains both manual server and client setup, and how Descope simplifies this with a visual interface and pre-built flows.

This tutorial is by Kumar Harsh, a software developer and technical author from India. See more of his work on his website!

Using passkeys improves application security and user convenience. This guide details passkey implementation, from setup to deployment. Following these steps creates a secure and user-friendly authentication experience, reducing unauthorized access and building user trust.

Passkey Authentication: The Basics

Unlike vulnerable passwords, passkeys use public-key cryptography to enhance both security and user experience. Think of a vault: your identity is secured, accessible only with a unique key pair. Passkeys work similarly; each user has a private key (safely stored on their device), and a public key shared with websites.

Login involves the website sending a challenge to the user's device. The private key creates a unique signature (a digital fingerprint), verifying identity without revealing the key itself. This keeps logins secure even if the website is compromised.

Here's a visual representation:

Developer Guide: How to Implement Passkeys

Fig: Passkey authentication ceremony

Implementing Passkeys: The Manual Approach (and why you shouldn't)

While passkeys are innovative, manual implementation is complex and discouraged due to setup difficulty and security risks. It's best to use a supported library.

Generally, manual implementation involves:

  • Server-side setup: This includes key generation, signature verification, attestation handling, and database integration for user information and passkey credentials.
  • Frontend integration: Using JavaScript's navigator.credentials.create() and navigator.credentials.get(), the client guides users through authenticator interactions (fingerprint scans, PINs, etc.) and sends data to the server.

Libraries like SimpleWebAuthn simplify this. It handles key generation, making the process easier than a manual approach.

(A React client and Node.js Express server example project using SimpleWebAuthn is available.)

Server-Side Setup (using SimpleWebAuthn)

The server uses the @simple-webauthn/server package with four key endpoints:

  1. /generate-registration-options: Configures and generates registration options for the client, preventing multiple registrations from the same device. It also generates a challenge.
  2. /verify-registration: Verifies successful on-device registration and saves user details (public key, credential ID, device information) in the database.
  3. /generate-authentication-options: Returns authentication options, retrieving the registered device ID.
  4. /verify-authentication: Verifies successful on-device authentication using a registered device.

This example omits database integration; you'll need to add that for production use.

Client-Side Setup (using SimpleWebAuthn)

The client needs two buttons (for registration and authentication). onRegistrationStart requests configuration from /generate-registration-options, then uses startRegistration() from @simple-webauthn/browser. After successful registration, /verify-registration verifies the result. Authentication follows a similar pattern using /generate-authentication-options, startAuthentication(), and /verify-authentication.

This manual method is complex and requires additional setup and maintenance for user identification and data storage. HTTPS setup requires extra steps.

The Easier Way: Implementing Passkeys with Descope

Descope simplifies passkey implementation, eliminating complex manual setup. It provides a no-code interface for managing authentication flows.

Creating Descope Flows

  1. Sign up for a Descope account.
  2. In the dashboard, create an auth flow. Select Passkeys (WebAuthn) as the primary method.
  3. Optionally, add a 2FA method.
  4. Choose a login screen (potentially with additional registration methods).
  5. Descope generates your auth flow and provides a code snippet with your project ID.

Setting up a React Project

  1. Create a new React project using npm create vite.
  2. Install the Descope SDK: npm install @descope/react-sdk.
  3. Configure the AuthProvider in main.jsx with your project ID.
  4. Use Descope components and hooks in App.js to implement the authentication flow.

This simplified example shows how to integrate Descope's sign-up or sign-in flow:

// Simplified App.js example
import { useDescope, useSession } from '@descope/react-sdk';
import { Descope } from '@descope/react-sdk';

const App = () => {
  const { isAuthenticated, isSessionLoading } = useSession();
  const { logout } = useDescope();

  return (
    <div>
      {!isAuthenticated && (
        <Descope flowId="sign-up-or-in" onSuccess={(e) => console.log(e.detail.user)} onError={(e) => console.log('Error!')} />
      )}
      {isAuthenticated && <button onClick={logout}>Logout</button>}
    </div>
  );
};
Copy after login

Trying Out the App

Run npm run dev and access the app at http://localhost:5173. Register a user, set up passkeys, and test login. You can also enable passkey autofill in the Descope dashboard.

(The complete code is available on GitHub.)

Descope's Drag-and-Drop Authentication

Descope streamlines passkey implementation with a drag-and-drop interface for managing authentication flows. This simplifies setup and modification, making it accessible to developers of all skill levels. It allows for easier, faster, and more accessible logins.

Sign up for a free Descope account to learn more.

The above is the detailed content of Developer Guide: How to Implement Passkeys. For more information, please follow other related articles on the PHP Chinese website!

Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

Video Face Swap

Video Face Swap

Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

Hot Topics

Java Tutorial
1663
14
PHP Tutorial
1266
29
C# Tutorial
1238
24
CNCF Arm64 Pilot: Impact and Insights CNCF Arm64 Pilot: Impact and Insights Apr 15, 2025 am 08:27 AM

This pilot program, a collaboration between the CNCF (Cloud Native Computing Foundation), Ampere Computing, Equinix Metal, and Actuated, streamlines arm64 CI/CD for CNCF GitHub projects. The initiative addresses security concerns and performance lim

Serverless Image Processing Pipeline with AWS ECS and Lambda Serverless Image Processing Pipeline with AWS ECS and Lambda Apr 18, 2025 am 08:28 AM

This tutorial guides you through building a serverless image processing pipeline using AWS services. We'll create a Next.js frontend deployed on an ECS Fargate cluster, interacting with an API Gateway, Lambda functions, S3 buckets, and DynamoDB. Th

Top 21 Developer Newsletters to Subscribe To in 2025 Top 21 Developer Newsletters to Subscribe To in 2025 Apr 24, 2025 am 08:28 AM

Stay informed about the latest tech trends with these top developer newsletters! This curated list offers something for everyone, from AI enthusiasts to seasoned backend and frontend developers. Choose your favorites and save time searching for rel

See all articles