Protect your API on Node(Express)
Extract the Bearer Token from the request header
// auth_middleware.ts
import { IncomingHttpHeaders } from 'http';
const extractBearerTokenFromHeaders = ({ authorization }: IncomingHttpHeaders) => {
if (!authorization) {
throw new Error({ code: 'auth.authorization_header_missing', status: 401 });
}
if (!authorization.startsWith('Bearer')) {
throw new Error({ code: 'auth.authorization_token_type_not_supported', status: 401 });
}
return authorization.slice(bearerTokenIdentifier.length + 1);
};
Token validation
Install jose as your dependency
Retrieve Auth’s OIDC configurations
Add auth middleware
Apply middleware to your API
Last updated