pitu
pitu

Reputation: 820

how to send json as a response after passport authenticationin node.js

I am trying this git example.

Which works well when I integrated it with my project, but what I want to achieve is to send json as a response to the client/request, instead of successRedirect : '/profile' & failureRedirect : '/signup'.

Is it possible to send a json, or is there some other methods to get the same?

Any help will be appreciated,TU

Upvotes: 9

Views: 13360

Answers (6)

Arthur Ronconi
Arthur Ronconi

Reputation: 2428

There is an official Custom Callback documentation:

app.get('/login', function(req, res, next) {
  passport.authenticate('local', function(err, user, info) {
    if (err) { return next(err); }
    if (!user) { return res.redirect('/login'); }
    req.logIn(user, function(err) {
      if (err) { return next(err); }
      return res.redirect('/users/' + user.username);
    });
  })(req, res, next);
});

https://github.com/passport/www.passportjs.org/blob/master/views/docs/authenticate.md

Upvotes: 4

Arrow
Arrow

Reputation: 163

// process the signup form
app.post('/signup', passport.authenticate('local-signup', {
    successRedirect : '/successjson', // redirect to the secure profile section
    failureRedirect : '/failurejson', // redirect back to the signup page if there is an error
    failureFlash : true // allow flash messages
}));

app.get('/successjson', function(req, res) {
    res.sendfile('public/index.htm');
});

app.get('/failurejson', function(req, res) {
    res.json({ message: 'hello' });
});

Upvotes: -3

pitu
pitu

Reputation: 820

here I modified my code to send json as a response

// process the signup form
app.post('/signup', passport.authenticate('local-signup', {
    successRedirect : '/successjson', // redirect to the secure profile section
    failureRedirect : '/failurejson', // redirect back to the signup page if there is an error
    failureFlash : true // allow flash messages
}));

app.get('/successjson', function(req, res) {
    res.sendfile('public/index.htm');
});

app.get('/failurejson', function(req, res) {
    res.json({ message: 'hello' });
});

Upvotes: 7

Midhun Darvin
Midhun Darvin

Reputation: 1255

You can use passport's authenticate function as route middleware in your express application.

app.post('/login',
  passport.authenticate('local'),
  function(req, res) {
    // If this function gets called, authentication was successful.
    // `req.user` contains the authenticated user.
    // Then you can send your json as response.
    res.json({message:"Success", username: req.user.username});
  });

By default, if authentication fails, Passport will respond with a 401 Unauthorized status, and any additional route handlers will not be invoked. If authentication succeeds, the next handler will be invoked and the req.user property will be set to the authenticated user.

Upvotes: 8

user855035
user855035

Reputation: 21

Use passport as a middleware.

router.get('/auth/callback', passport.authenticate('facebook'), 
    function(req, res){
        if (req.user) { res.send(req.user); }
        else { res.send(401); }
    });

Upvotes: 1

thyforhtian
thyforhtian

Reputation: 366

Create new route, e.g.: /jsonSend with res.json in it and make successRedirect: '/jsonSend'. That should do it.

Upvotes: 3

Related Questions