hapijs/cookie

request.auth.credentials is null after setting request.cookieAuth.set({ id: users.id });

bradrar opened this issue · 3 comments

This is more of a question than an issue. I followed the README.md and copied the code given.
I have successfully set the request.cookieAuth.set and looked at the chrome browser dev tools application tab to make sure it is set. but when I looked at the request.auth.credentials, It is null. I have set my validateFunc to return an object {valid: true, credentials: account} where account is the user but when I try to console.log(request.auth.credentials) it returns null.

const users = [
    {
        username: 'john',
        password: '$2b$10$nrkw6Mco2j7YyBqZSWRAx.P3XEZsZg3MNfma2ECO8rGMUTcF9gHO.',   // 'secret'
        name: 'John Doe',
        id: '2133d32a'
    }
];


  //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) => {

         
                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("/")
             }
        })

I just solved this . Just curious what is wrong with my code here.

How did you solve it? When credentials were null, what as the auth status?

lock commented

This thread has been automatically locked due to inactivity. Please open a new issue for related bugs or questions following the new issue template instructions.