我正在使用SailsJS中的Webpack构建一个react(ES6)应用程序。
我遇到的问题是会话未正确处理或存储在本地存储空间 IF 从前端触发操作
// TestAPIController:
module.exports = {
create: function(req, res){
// TODO: Add user authentication piece
var user = {name: 'test user'};
req.session.user = user.name;
req.session.authenticated = true;
console.log('session:', req.session);
// req.session.save();
res.json(req.session);
},
echo: function(req, res){
console.log('session:', req.session);
return res.json({message:'got it'});
}
};
政策:
TestAPIController: {
echo : 'sessionAuth'
}
路线:
'GET /TestAPI/create' : {
controller: 'TestAPIController',
action:'create'
},
'GET /TestAPI/echo' : {
controller: 'TestAPIController',
action:'echo'
},
前端
onClickCreate(e){
fetch('/TestAPI/create').then( res => res.json());
}
onClickEcho(e){
fetch('/TestAPI/echo').then(res => res.json());
}
标记
<li><a onClick={_this.onClickCreate}>Create Test</a></li>
<li><a onClick={_this.onClickEcho}>Echo Test</a></li>
点击创建控制台日志:[正确!]
Requested :: GET /TestAPI/create
session: Session {
cookie:
{ path: '/',
_expires: 2017-01-18T20:21:01.069Z,
originalMaxAge: 86400000,
httpOnly: true },
user: 'test user',
authenticated: true }
然后点击Echo,Console Log:[错误!]
Requested :: GET /TestAPI/echo
verbose: Sending 403 ("Forbidden") response:
You are not permitted to perform this action.
现在转到Chrome浏览器网址中的http://localhost:3000/TestAPI/create
浏览器打印来自通话的回报。和日志:
Requested :: GET /TestAPI/create
session: Session {
cookie:
{ path: '/',
_expires: 2017-01-18T20:21:13.645Z,
originalMaxAge: 86400000,
httpOnly: true },
user: 'test user',
authenticated: true }
然后在网址
中输入http://localhost:3000/TestAPI/echo
浏览器打印来自[正确!]
的回复Requested :: GET /TestAPI/echo
session: Session {
cookie:
{ path: '/',
_expires: 2017-01-18T20:25:24.019Z,
originalMaxAge: 86400000,
httpOnly: true },
user: 'test user',
authenticated: true }
也许我没有遵循一些基本原则? 或者也许有更好的方法来控制会话?
任何帮助将不胜感激。