为了账号安全,请及时绑定邮箱和手机立即绑定

Hapi身份验证Cookie设置,但request.auth.credentials为null

Hapi身份验证Cookie设置,但request.auth.credentials为null

长风秋雁 2021-03-28 12:15:52
我已阅读hapi-auth-cookie的文档以尝试在我的网站中进行验证。Cookie设置好了,我可以在其中看到它 chrome browser dev tools application tab但是,当我console.log时request.auth.credentials,结果为null我要完成的操作是将用户限制为http:// localhost:3000 / restricted,我认为缺少的链接是request.auth.credentials因为它为null这是我的代码const users = [    {        username: 'john',        password: '$2b$10$nrkw6Mco2j7YyBqZSWRAx.P3XEZsZg3MNfma2ECO8rGMUTcF9gHO.',   // 'secret'        name: 'John Doe',        id: '2133d32a'    }];await server.register(require('hapi-auth-cookie'));    //strategy    server.auth.strategy('session', 'cookie', {        cookie: {            name: 'sid-example',            password: '!wsYhFA*C2U6nz=Bu^%A@^F#SF3&kSR6',            isSecure: false        },        redirectTo: '/login',        validateFunc: async (request, session) => {            const account = await users.find(                (user) => (user.id === session.id)            );            if (!account) {                return { valid: false };            }            return { valid: true, credentials: account };        }    });    server.auth.default({strategy: 'session', mode: 'try'});   //login post    server.route({        method: 'POST',        path: '/login',        handler: async (request, h) => {           console.log(request.payload)            const { username, password } = request.payload;            const account = users.find(                (user) => user.username === username            );            if (!account || !(await Bcrypt.compare(password, users[0].password))) {                console.log('user or password incorrect')                return h.view('login');            }            request.cookieAuth.set({ id: users.id });            console.log('login successful')            return h.redirect().location("/")         }    })我尝试将中的options.auth.mode“尝试”设置为“ try”,restricted route然后尝试将其options完全删除。所有路由都工作正常,但是即使我登录也无法打开受限路由。
查看完整描述

1 回答

  • 1 回答
  • 0 关注
  • 245 浏览
慕课专栏
更多

添加回答

举报

0/150
提交
取消
意见反馈 帮助中心 APP下载
官方微信