Я пытаюсь создать страницу входа пользователя с флажком «запомнить меня». Звучит просто, правда?
Когда пользователь входит в систему без установленного флажка «запомнить меня», время жизни сеанса по умолчанию будет установлено на 10 секунд (10000 мс). Однако, если установлен флажок «запомнить меня», время жизни будет установлено на 100000 секунд (100000000 мс).
Пропустив всю проверку подлинности имени пользователя и пароля, я настроил эту небольшую демонстрацию, чтобы проиллюстрировать свои цели. К сожалению, кажется, что ttl сеанса ВСЕГДА составляет 10 секунд и никогда не составляет 100000 секунд.
Вот вставка для приведенного ниже кода: http://pastebin.com/45bRfxkn
var Hapi = require('hapi');
var AuthCookie = require('hapi-auth-cookie');
var server = new Hapi.Server('localhost',4000); // make a server
// this function is just for my example, I'll use an actual logger later...
function xhrlog(request){
var auth = request.auth.isAuthenticated ? "Authenticated" : "Not Authenticated";
console.log(request.method.toUpperCase()+" request to "+request.path+" is "+auth+".");
}
// this is the handler for the '/' route. You should start at this route first (it represents the login page)
function firstLoad(request, reply){
xhrlog(request);
request.auth.session.set({});
reply("<p style='color:blue'>click the button to test.</p><input type='button' id='foo' value='click me'></input><script>document.getElementById('foo').addEventListener('click', function(){ window.location = './newLocation'});</script> ");
}
// this is the page that i would expect to have created a session cookie with a ttl of 100000. But it doesnt.
function authorized(request,reply){
xhrlog(request);
reply("<p style='width: 300px;'>This is the authorized page. I would expect this page to have a session timeout of 100000 seconds. But it doesnt, it only has 10 seconds. Keep refreshing to see if you are still alive!</p>");
}
// set up the unauthenticated route here. this is the "login" page.
server.route({
method:'GET',
path:'/',
config: {
handler: firstLoad
}
});
server.pack.register(AuthCookie, function(err){
// set up strategy for the session cookie. It defaults to 10000 ms
server.auth.strategy('session', 'cookie', {
password: 'secret',
cookie: 'iDontKnowWhatThisIsFor',
redirectTo: '/',
isSecure: false,
ttl: 10000
});
// set up the route for the 'remember me' page. It should have a ttl of 100000000 ms.
server.route({
method: 'GET',
path: '/newLocation',
config: {
handler: authorized,
auth: {
mode: 'try',
strategy: 'session'
},
plugins: { 'hapi-auth-cookie' : { ttl: 100000000 }}
}
});
});