session登录态判断处理
使用session中间件
const session = require('koa-session-minimal')
const MysqlStore = require('koa-mysql-session')
const config = require('./../config')
const app = new Koa()
const sessionMysqlConfig= {
user: config.database.USERNAME,
password: config.database.PASSWORD,
database: config.database.DATABASE,
host: config.database.HOST,
}
app.use(session({
key: 'USER_SID',
store: new MysqlStore(sessionMysqlConfig)
}))
登录成功后设置session到MySQL和设置sessionId到cookie
let session = ctx.session
session.isLogin = true
session.userName = userResult.name
session.userId = userResult.id
需要判断登录态页面进行session判断
async indexPage ( ctx ) {
if ( ctx.session && ctx.session.isLogin && ctx.session.userName ) {
const title = 'work页面'
await ctx.render('work', {
title,
})
} else {
ctx.redirect('/error')
}
},