Hapi.js Bell / Auth-Cookie重定向循环

时间:2016-09-28 13:16:49

标签: node.js hapijs

我正在尝试为Hapi.js项目设置google auth,我无法理解为什么登录后我会将重定向循环返回到我的登录路径。

仅当用户尚未登录其Google帐户或尚未授予该应用访问其信息的权限时,才会发生这种情况。

这是身份验证注册码

server.auth.strategy('session', 'cookie', true, {
   password: 'private_key',
   redirectTo: '/login/google'
});

server.auth.strategy('google', 'bell', {
    provider: 'google',
    password: 'private_key',
    clientId: 'client_id',
    clientSecret: 'client_secret',
    location: 'same_origin_registered_with_google'
});

这些是我的两条路线

server.route({
    method: ['GET', 'POST'],
    path: '/login/google',
    config: {
        auth: {
            strategy: 'google',
            mode: 'try'
        },
        plugins: { 'hapi-auth-cookie': { redirectTo: false } },
        handler: (request, reply) => {
            if (!request.auth.isAuthenticated) {
                return reply('Authentication failed due to: ' + request.auth.error.message);
            }

            let creds = request.auth.credentials;

            request.cookieAuth.set({
                token: creds.token,
                userId: creds.profile.id
            });

            return reply.redirect('/');
        }
    }
});

server.route({
    method: 'GET',
    path: '/',
    handler: (request, reply) => {
        return reply('hello');
    }
});

要注意,正在设置auth cookie,一旦重定向循环退出,我可以导航到" /"手动路线没问题。

非常感谢任何帮助

2 个答案:

答案 0 :(得分:4)

这是由于hapi-auth-cookie尚未处理hapi 15中的isSameSite cookie选项导致cookie无法发送回服务器。

您可以通过手动设置选项来解决此问题。

更多信息可以在hapi 15发行说明https://github.com/hapijs/hapi/issues/3323和PR hapi-auth-cookie https://github.com/hapijs/hapi-auth-cookie/pull/142

中找到

答案 1 :(得分:1)

这解决了问题:

server.auth.strategy('session', 'cookie', true, {
   password: 'private_key',
   redirectTo: '/login/google',
   isSameSite: 'Lax'
});
相关问题