node.js - Sails.js API passport.js authentication -
i trying develop api backend in sails.js.
the basic thing require authentication.
with that, found sails-generate-auth
generator, have followed steps listed @ sails-generate-auth .
now, when access http://localhost:1337/register
, see simple registration form, same goes login, , after logging in, see cookie set in browser sails.sid
.
after inspecting authcontroller.js
see has been written server rendered views.
how should modify controller/sailsapp supports api based authentication.
i ideally have:
a register route accept username , password via post content type application/json.
login route accept username , password content-type application/json , return bearer token frontend app can add header next time makes request.
all other routes under auth acl check if bearer token present , verified.
in authcontroller callback function replace this:
res.redirect('/');
with this:
console.log(user); var userid = user.id; passport.find({user: userid}, function(err, items){ if(err) return err; console.log(items[0].accesstoken); // make sure dont give them sensetive data res.json({userdata: user, token: items[0].accesstoken}); }); // upon successful login, send user homepage req.user //res.redirect('/');
now when client sends login/register request server response json response. make sure request token on other sails app actions.
Comments
Post a Comment