express-template/middlewares/page_auth.js

27 lines
563 B
JavaScript

const AuthUtils = require('../utils/auth')
const { Account } = require('../entity/account')
const { getManager } = require('typeorm')
module.exports = async function (req,res,next) {
const session = req.cookies.IM_SESS
if(!session) return res.redirect('/oauth/login')
try{
var payload = AuthUtils.decode(session)
}catch(err) {
return res.redirect('/oauth/login')
}
req.auth_session = payload
const manager = getManager()
const detail = await manager.findOne(Account,{ aid:payload.aid })
req.account_detail = detail
next()
}