nodejsはsessionを通じてアイデンティティ検証を実現する.
1)モジュールの導入
var session = require('express-session');
var cookieParser = require('cookie-parser');
2)cookie及びセッションの適用app.use(cookieParser());
app.use(session({
resave: true, // don't save session if unmodified
saveUninitialized: false, // don't create session until something stored
secret: 'love'
}));
3)要求時、アプリケーションID認証app.use(function(req,res,next){
if (!req.session.user) {
if(req.url=="/login"){
next();// ,
}
else
{
res.redirect('/login');
}
} else if (req.session.user) {
next();
}
});
4)登録設計app.get('/login',function(req,res){
res.render("login");
});
app.post('/login',function(req,res){
if(req.body.username=="love" && req.body.password=="love"){
var user = {'username':'love'};
req.session.user = user;
res.redirect('/admin/app/list');
}
else
{
res.redirect('/login');
}
});
app.get('/logout',function(req,res){
req.session.user = null;
res.redirect('/login');
});