0

「remember me」チェックボックスがあるユーザーログインページを作成しようとしています。シンプルですね。

ユーザーが「remember me」チェックボックスを選択せず​​にログインすると、デフォルトのセッション ttl が 10 秒 (10000 ミリ秒) に設定されます。ただし、「remember me」チェックボックスが選択されている場合、ttl は 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 }}  
      }
   });
});
4

2 に答える 2

1

応答インターフェイスで異なる ttl 値を設定できます。この ttl 値は、デフォルトのセッション Cookie の ttl をオーバーライドします

reply("test").state("session", session, {ttl: 365 * 30 * 7 * 24 * 60 * 60 * 1000});
于 2014-08-04T11:33:07.660 に答える
0

誰かがここにいることに気付いた場合、現在のバージョンの hapi-auth-cookie では答えが異なるようです。受け入れられた回答を使用してみましたが、ルート固有の ttl 値は変更されません。

APIドキュメントによると:request.cookieAuth.ttl(milliseconds)デフォルトの戦略設定を上書きします。明確に文書化されていませんが、値nullを渡して、ブラウザーで Cookie を Session に設定することもできます。元のポスターと同様の状況で、ttl 設定を正常にオーバーライドできました。

ただし、最新のブラウザのほとんどは、ブラウザのタブなどを再度開くために保存するため、Cookie を保存することになります。Cookie の有効期限が切れることを確実にしたい場合は、ttl を 1 日未満に設定することをお勧めします。セッションの期待される動作と同等の完全なソリューションではありません

コード スニペットの例を参照してください。

if (response.login) {
var session = { sid: response.sid};
request.cookieAuth.set(session);
// check if login form had remember me checkbox selected
if(request.payload.remember) {
  return reply.redirect(request.query.next); //uses default strategy ttl setting for cookie
}else{
  request.cookieAuth.ttl(24*60*60*1000); // override ttl to 24 hours
  return reply.redirect(request.query.next);
}
}
于 2016-11-20T18:50:46.913 に答える