import { expressAuthHelpers } from '@lit-protocol/vincent-sdk';
const { authenticatedRequestHandler, getAuthenticateUserExpressHandler } = expressAuthHelpers;
import type { ExpressAuthHelpers } from '@lit-protocol/vincent-sdk';
// Define an authenticated route handler
const getUserProfile = async (req: ExpressAuthHelpers['AuthenticatedRequest'], res: Response) => {
// Access authenticated user information
const { pkpAddress } = req.user;
// Fetch and return user data
const userData = await userRepository.findByAddress(pkpAddress);
res.json(userData);
};
// Use in Express route with authentication
app.get('/profile', authenticateUser, authenticatedRequestHandler(getUserProfile));
Higher-order helper function to enforce authentication on a request handler and assert the type of
Request
that is passed into your authenticated Express routes.This function takes an
AuthenticatedRequestHandler
and returns a new request handler that verifies that the request has a 'user' property with the correct shape on it before calling the original handler. If thereq.user
property isn't the correct shape, it sends a401 Unauthorized
response to the client.NOTE: This does not verify signatures or any other content -- use
getAuthenticateUserExpressHandler
to create a middleware that does those things and ensure that your routes use it.See express.js documentation for details on writing your route handler