node.js - passport node authenticate success -



node.js - passport node authenticate success -

i working on implementing passport authentication node application, , unable understand why there needs redirect before can access response (res) attribute?

app.get('/api/loginfailure', function(req, res) { res.status(401).json({message: 'login failed', success: true}); }); app.get('/api/loginsuccess', function(req, res) { res.status(200).json({message:'welcome!', success: true}); }); // process login form app.post('/api/login', passport.authenticate('local-login', { successredirect: '/api/loginsuccess', failureredirect: '/api/loginfailure'}));

as can see, utilize successredirect access different route in order send json response. not want node api redirecting actual application intention agnostic front end end.

the local login strategy follows. suspect difficulties may in how homecoming method;

passport.use('local-login', new localstrategy({ // default, local strategy uses username , password, override email usernamefield: 'email', passwordfield: 'password', passreqtocallback: true // allows pass entire request callback }, function(req, email, password, done) { // callback email , password our form // find user email same forms email // checking see if user trying login exists user.findone({ 'local.email': email }, function(err, user) { // if there errors, homecoming error before else if (err) homecoming done(err); // if no user found, homecoming message if (!user) { homecoming done(null, false, req.flash('loginmessage', 'no user found.')); // req.flash way set flashdata using connect-flash } // if user found password wrong if (!user.validpassword(password)) { homecoming done(null, false, req.flash('loginmessage', 'oops! wrong password.')); // create loginmessage , save session flashdata } // well, homecoming successful user homecoming done(null, user); }); }));

i intend remove of flashdata , not, beingness able collapse 2 additional api routes /api/login great.

i unable understand why there needs redirect before can access response (res) attribute?

if checked passport documentation, instead of copying code this guide, meant type of use, find out doesn't require redirects.

you can utilize in next way:

app.post('/login', passport.authenticate('local'), function(req, res) { // if function gets called, authentication successful. // `req.user` contains authenticated user. res.redirect('/users/' + req.user.username); } );

node.js passport.js

Comments

Popular posts from this blog

assembly - What is the addressing mode for ld, add, and rjmp instructions? -

vowpalwabbit - Interpreting Vowpal Wabbit results: Why are some lines appended by "h"? -

Is there a way to convert an HTML page styled with Bootstrap CSS into email-compatible html? -