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

6

9

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

Speciosity answered 24/6, 2014 at 8:55 Comment(0)
S
7

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' });
});
Speciosity answered 24/6, 2014 at 10:3 Comment(0)
C
8

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.

Clothier answered 30/1, 2016 at 19:41 Comment(1)
This is the cleanest solution here IMO.Eve
S
7

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' });
});
Speciosity answered 24/6, 2014 at 10:3 Comment(0)
F
4

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

Fluctuation answered 29/10, 2017 at 12:55 Comment(0)
N
3

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

Nieves answered 24/6, 2014 at 9:40 Comment(7)
check my below answerSpeciosity
If that's what you need and it works for you then it's alright ;)Nieves
yup i just want to send json after authenticationSpeciosity
is there a any other way to get that success/ failure authentication in the same post requestSpeciosity
I'm afraid I don't understand what you mean. Examples maybe?Nieves
how to add callback to the app.post('/signup',passport.authenticate('local-signup')); method to get authentication statusSpeciosity
Well, in the callblack you can check it with req.isAuthenticated(). If it returns true, than your authentication was successful.Nieves
E
1

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); }
    });
Earwax answered 12/1, 2016 at 2:44 Comment(0)
W
-3
// 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' });
});
Welloiled answered 2/8, 2017 at 10:30 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.