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

Hapi.js Bell/Auth-Cookie Redirect Loop

我正在尝试为一个 Hapi.js 项目设置 google 身份验证,但我不明白为什么我在登录后将重定向循环返回到我的登录路径。

只有当用户尚未登录其 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');
    }
});

请注意,正在设置身份验证 cookie,一旦退出重定向循环,我就可以手动导航到“/”路由,没问题。

非常感谢任何帮助

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

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

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

这解决了问题:

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