Login to Passport.js Ajax?

Is it possible to login through ajax with a .js passport?

The thing is, I create the user through ajax, and I want it to be registered automatically (everything with json in a calm style), but req.login () does some things that I don’t know, and that apparently sends its status, headers and even redirects it home, but I need to create my own json response.

The code in which I create the user:

signup_facebook: function (req, res) {

    var restponse = new Restponse();

    var body = req.body;

    var obj = {
        display_name: body.first_name,
        name: body.first_name,
        surname: body.last_name,
        photos: ['http://graph.facebook.com/'+ body.id+ '/picture?type=normal'],
        gender: body.gender,
        facebook: {
            userID: body.id,
            displayName: body.display_name
        }
    }

    User.facebookSignUp(obj, function(user){

        if(user !== false){
            user = obj;
            restponse.location = '/';
            restponse.status = HTTPStatus.REST.C201_OK;
        }else{
            restponse.location = '/';
            restponse.status = HTTPStatus.REST.C302_FOUND;
        }

        restponse.body = user;

        req.login(user, {}, function(err) {
            APIheart.respondJson(res, restponse);
        });
    })

Thank you for your time!

+4
source share
1 answer

I found the answer in this post:

http://toon.io/on-passportjs-specific-use-cases/

// Manually establish the session...
req.login(user, function(err) {
    if (err) return next(err);
    return res.json({
        message: 'user authenticated',
    });
});
0
source