无法使用@hapi/cookie 19.x.x 验证用户
Unable to authenticate a user using @hapi/cookie 19.x.x
我最近将我的项目升级为使用 hapi 19.x.x,同时我更新了项目以使用 @hapi/cookie 而不是已弃用的 hap-auth-cookie 但是在成功验证后即使在使用 request.cookieAuth.set({ id : id})
设置会话 cookie 后,我的应用程序也会不断尝试重新验证
当应用程序使用 .auth.strategy('admin', 'cookie', {})
对象上的 redirectTo:
属性 重定向到 'restricted page' 时。
我注意到传入请求的状态 {}
是空的,而它不应该是
节点-v // 12.16.2
Google Chrome
版本 80.0.3987.163(正式版)(64 位)
package.json {
"dependencies": {
"@hapi/catbox-redis": "5.0.5",
"@hapi/cookie": "11.0.1",
"@hapi/h2o2": "9.0.1",
"@hapi/hapi": "19.1.1",
"@hapi/inert": "6.0.1",
"@hapi/joi": "17.1.1",
"@hapi/scooter": "6.0.0",
"@hapi/wreck": "17.0.0",
}
server.auth.strategy('admin', 'cookie', {
cookie: {
name: Server.cookieName,
password: auth_cookie_password,
isSecure: false,
ttl: Server.cacheCookieTtlMs
},
appendNext: true,
redirectTo: outboundUrl,
validateFunc: async (request: any, session: any) => {
// blah blah
}
{
method: ['GET', 'POST'],
path: '/login',
options: {
auth: false,
security: true
},
handler: async (request: any, h) => {
try {
const tokenSet = await authCallback();
const session = {
id: tokenSet.id,
}
request.cookieAuth.set(session);
const returnScript = `<script type="application/javascript" >(function() { setTimeout(function() {window.location = "http://localhost:3000"})})()</script>`;
return h.response(returnScript)
} catch (e) {
return h.response('Internal server error').code(500)
}
}
}
如有任何帮助,我们将不胜感激。
您必须将 cookie 路径设置为 /
只有当请求的 URL 以 cookie 路径的值开头时,cookie 才会发送到服务器。当您省略路径时,默认为收到响应 Set-Cookie header 的请求的 URL。因此,假设您省略了路径,并且您的 cookie 设置在 URL 上,例如 https://example.com/login (which is very common), then the cookie will only be sent on requests for subpaths like https://example.com/login/foo,这几乎不是您想要的。
我最近将我的项目升级为使用 hapi 19.x.x,同时我更新了项目以使用 @hapi/cookie 而不是已弃用的 hap-auth-cookie 但是在成功验证后即使在使用 request.cookieAuth.set({ id : id})
当应用程序使用 .auth.strategy('admin', 'cookie', {})
对象上的 redirectTo:
属性 重定向到 'restricted page' 时。
我注意到传入请求的状态 {}
是空的,而它不应该是
节点-v // 12.16.2
Google Chrome 版本 80.0.3987.163(正式版)(64 位)
package.json {
"dependencies": {
"@hapi/catbox-redis": "5.0.5",
"@hapi/cookie": "11.0.1",
"@hapi/h2o2": "9.0.1",
"@hapi/hapi": "19.1.1",
"@hapi/inert": "6.0.1",
"@hapi/joi": "17.1.1",
"@hapi/scooter": "6.0.0",
"@hapi/wreck": "17.0.0",
}
server.auth.strategy('admin', 'cookie', {
cookie: {
name: Server.cookieName,
password: auth_cookie_password,
isSecure: false,
ttl: Server.cacheCookieTtlMs
},
appendNext: true,
redirectTo: outboundUrl,
validateFunc: async (request: any, session: any) => {
// blah blah
}
{
method: ['GET', 'POST'],
path: '/login',
options: {
auth: false,
security: true
},
handler: async (request: any, h) => {
try {
const tokenSet = await authCallback();
const session = {
id: tokenSet.id,
}
request.cookieAuth.set(session);
const returnScript = `<script type="application/javascript" >(function() { setTimeout(function() {window.location = "http://localhost:3000"})})()</script>`;
return h.response(returnScript)
} catch (e) {
return h.response('Internal server error').code(500)
}
}
}
如有任何帮助,我们将不胜感激。
您必须将 cookie 路径设置为 /
只有当请求的 URL 以 cookie 路径的值开头时,cookie 才会发送到服务器。当您省略路径时,默认为收到响应 Set-Cookie header 的请求的 URL。因此,假设您省略了路径,并且您的 cookie 设置在 URL 上,例如 https://example.com/login (which is very common), then the cookie will only be sent on requests for subpaths like https://example.com/login/foo,这几乎不是您想要的。