budibase/packages/server/middleware/authenticated.js

22 lines
422 B
JavaScript
Raw Normal View History

2020-05-07 11:53:34 +02:00
const jwt = require("jsonwebtoken")
module.exports = async (ctx, next) => {
2020-05-07 11:53:34 +02:00
const token = ctx.cookies.get("budibase:token")
console.log("TOKEN", token)
2020-05-06 21:29:47 +02:00
if (!token) {
2020-05-04 18:13:57 +02:00
ctx.isAuthenticated = false
2020-05-07 11:53:34 +02:00
await next()
return
}
try {
2020-05-07 11:53:34 +02:00
ctx.jwtPayload = jwt.verify(token, ctx.config.jwtSecret)
ctx.isAuthenticated = true
} catch (err) {
2020-05-07 11:53:34 +02:00
ctx.throw(err.status || 403, err.text)
}
2020-05-07 11:53:34 +02:00
await next()
}