1
0
Fork 0
mirror of synced 2024-10-05 12:34:50 +13:00
budibase/packages/server/middleware/authenticated.js

21 lines
511 B
JavaScript
Raw Normal View History

const jwt = require("jsonwebtoken");
module.exports = async (ctx, next) => {
2020-05-05 04:13:57 +12:00
if (!ctx.headers.authorization) {
ctx.isAuthenticated = false
await next();
return;
};
// if (!ctx.headers.authorization) ctx.throw(403, "No token provided");
const [_, token] = ctx.headers.authorization.split(" ");
try {
ctx.request.jwtPayload = jwt.verify(token, ctx.config.jwtSecret);
2020-05-05 04:13:57 +12:00
ctx.isAuthenticated = true;
} catch (err) {
ctx.throw(err.status || 403, err.text);
}
await next();
};