node.js - How to implement OAuth to my Nodejs/Sails.js app? -
i have sails.js app generates api client. in order secure api need implement oauth2.0 sails app. have started follow tutorial: https://www.npmjs.com/package/sails-generate-auth#requirements
but kinds of diffrent errors when every time when try lift server. dont understand i'm suppose send credentials server , access token. i'm new sails.js , got know oauth , can't find proper guide on how implement oauth.
how can implement oauth app? please have detailed answer can understand.
update:
ok instead started follow guide: https://www.bearfruit.org/2014/07/21/tutorial-easy-authentication-for-sails-js-apps/ , think got work should(?) when register account saves data in database should. login seems work didn't understood how can access actuall data username , email address after login redirects me homepage? i've tested login on postman , when log in cookie. suppose it?
the authcontroller generated sails-generate-auth doesn't add user details session default should add manually adding following line callback function in authcontroller.js
req.session.user = user;
this how callback looks line:
callback: function (req, res) { function tryagain (err) { // error messages returned via req.flash('error', someerror) // because shouldn't expose internal authorization errors user. // return generic error , original request body. var flasherror = req.flash('error')[0]; if (err && !flasherror ) { req.flash('error', 'error.passport.generic'); } else if (flasherror) { req.flash('error', flasherror); } req.flash('form', req.body); // if error thrown, redirect user // login, register or disconnect action initiator view. // these views should take care of rendering error messages. var action = req.param('action'); switch (action) { case 'register': res.redirect('/register'); break; case 'disconnect': res.redirect('back'); break; default: res.redirect('/login'); } } passport.callback(req, res, function (err, user, challenges, statuses) { if (err || !user) { return tryagain(challenges); } req.login(user, function (err) { if (err) { return tryagain(err); } // mark session authenticated work default sails sessionauth.js policy req.session.authenticated = true; req.session.user = user; // upon successful login, send user homepage req.user // available. res.redirect('/'); }); });
}
you can use user details in of controllers , views referring req.session.user
example twitter provides user name can use req.session.user.username
.
Comments
Post a Comment