import { verifyToken } from "../api/utils/jwt";
module.exports = (config, { strapi }) => {
return async (ctx, next) => {
const authHeader = ctx.request.header.authorization;
if (!authHeader || !authHeader.startsWith('Bearer ')) {
return ctx.unauthorized('Missing or invalid Authorization header');
}
const token = authHeader.split(' ')[1];
try {
const decoded = verifyToken(token);
ctx.state.user = decoded;
await next();
} catch (error) {
return ctx.unauthorized(error.message);
}
};
};