This application demonstrates how to implement passkey authentication using Express.js, integrating with a MongoDB database and utilizing the @simplewebauthn/server
library.
Make sure you have Node.js and npm installed on your machine.
Clone the repository and install dependencies:
git clone <repository-url>
cd <repository-directory>
npm install
Create a .env
file in the root directory with the following environment variables:
PORT=8080
MONGODB_URI=<your-mongodb-uri>
LOCAL_IP=localhost
Start the Express server:
npm start
The server will start running at http://localhost:8080
by default.
- Method: POST
- Description: Initiates the registration process for passkey authentication.
- Request Body:
{ "username": "user123" }
- Response:
{ "challenge": "<base64-encoded-challenge>", "rp": { "name": "CredMan App Test", "id": "<rpId>" }, "user": { "name": "user123", "displayName": "user123" }, "pubKeyCredParams": [ { "type": "public-key", "alg": -7 } ], "timeout": 60000, "attestationType": "none", "authenticatorSelection": { "authenticatorAttachment": "platform", "requireResidentKey": false, "userVerification": "required" }, "excludeCredentials": [] }
- Method: POST
- Description: Completes the registration process after user approval.
- Request Body:
{ "challenge": "<base64-encoded-challenge>", "username": "user123", "response": "<response-data>" }
- Response:
{ "message": "Registration successful", "data": { "userId": "<user-id>", "username": "user123" // other user data }, "token": "<jwt-token>" }
- Method: GET
- Description: Initiates the login process and retrieves authentication options.
- Response:
{ "challenge": "<base64-encoded-challenge>", "rpId": "blogs-deeplink-example.vercel.app", "allowCredentials": [ { "type": "public-key", "id": "<credential-id>", "transports": ["internal"] } ] }
- Method: POST
- Description: Completes the login process after user approval.
- Request Body:
{ "id": "<credential-id>", "challenge": "<base64-encoded-challenge>", "response": "<response-data>" }
- Response:
{ "message": "Login successful", "data": { "userId": "<user-id>", "username": "user123" // other user data }, "token": "<jwt-token>" }
- Method: GET
- Description: Protected route that requires a JWT token to access. Returns user information based on the token.
- Response:
{ "user": { "userId": "<user-id>", "username": "user123" // other user data } }
This project uses the @simplewebauthn/server
library for WebAuthn functionality. For more details, refer to their documentation.