Forget
Forget

Reputation: 152

request.auth.session.set(user_info) not working HapiJS

This is my strategy, which is defined on a server.register(). I'm basing my work off a tutorial and it is literally copied from it but it doesn't work.

    server.auth.strategy('standard', 'cookie', {
        password: 'somecrazycookiesecretthatcantbeguesseswouldgohere', // cookie secret
        cookie: 'app-cookie', // Cookie name
        isSecure: false, // required for non-https applications
        redirectTo: '/login',
        ttl: 24 * 60 * 60 * 1000 // Set session to 1 day
    });

    server.auth.default({
        strategy: 'standard',
        mode: 'required',
        scope: ['admin']
    });

This is my login route where the error occurs:

server.route({
    method: 'POST',
    path: '/login',
    config: {
        auth: false,
        validate: {
            payload: {
                email: Joi.string().email().required(),
                password: Joi.string().min(2).max(200).required()
            }
        },
        handler: function (request, reply) {

            getValidatedUser(request.payload.email, request.payload.password)
                    .then(function (user) {

                        if (user) {
                            //ERROR OCCURS HERE: IT SAYS SESSION IS UNDEFINED
                            request.auth.session.set(user);
                            return reply('Login Successful!');
                        } else {
                            return reply(Boom.unauthorized('Bad email or password'));
                        }

                    });
//                        .catch(function (err) {
//                            return reply(Boom.badImplementation());
//                        });

        }
    }
});

I've tried so many things but this part is crucial for this work and I can't find anyone with the same problem. Help please!

Upvotes: 1

Views: 1472

Answers (1)

mcmo
mcmo

Reputation: 86

hapi-auth-cookie has changed the way cookies are set and cleared. As of version 5.0.0, use request.cookieAuth.set() and request.cookieAuth.clear(). You are probably using a more recent version of the plugin than is used in the package.json of the tutorial.

source: https://github.com/hapijs/hapi-auth-cookie/commit/d233b2a3e4d0f03ef53b91b7929b8dbadbff624c

Upvotes: 7

Related Questions