UNPKG

@dbbs/strapi-stripe-payment

Version:
31 lines (30 loc) 1.21 kB
"use strict"; var __importDefault = (this && this.__importDefault) || function (mod) { return (mod && mod.__esModule) ? mod : { "default": mod }; }; Object.defineProperty(exports, "__esModule", { value: true }); const jsonwebtoken_1 = __importDefault(require("jsonwebtoken")); const http_errors_1 = __importDefault(require("http-errors")); const auth0AuthMiddleware = async (ctx, next) => { const authHeader = ctx.request.header.authorization; if (!authHeader) { throw new http_errors_1.default.Unauthorized('No authorization header found'); } const token = authHeader.split(' ')[1]; if (!token) { throw new http_errors_1.default.Unauthorized('No token found in authorization header'); } try { const publicKey = strapi.config.get('server.auth0.publicKey'); const decodedToken = jsonwebtoken_1.default.verify(token, publicKey, { algorithms: ['RS256'] }); ctx.state.user = decodedToken; await next(); } catch (error) { console.error('Token verification failed:', error.message); throw new http_errors_1.default.Unauthorized('Invalid token'); } }; exports.default = auth0AuthMiddleware;