在用eggjs写用户登录, 用户登录成功后,在session里放入user
async login() {
const ctx = this.ctx;
const { username, password } = ctx.request.body;
const user = await this.service.home.login(username, password);
ctx.session.user = user;
const { id, schemastr } = user || {};
console.log(ctx.session);
ctx.body = {
data: {
id,
schemastr,
username,
},
message: 'success',
};
}
问题是,在设置session完之后,其它controller函数的session都为空对象:
async asyncData() {
const ctx = this.ctx;
const { id, schemastr } = ctx.request.body;
this.service.home.asyncData(id, schemastr);
console.log(ctx.session); // => {}
ctx.body = {
data: 200,
message: 'success',
};
}
请问大家是怎么回事?
这是因为 没有序列化 或者序列化到session失败
若是在express中是:
若是在egg.js中 由于egg-passport对passport进行了包装
你需要在app.js中进行: