UPDATED: Current solution below.
I'm adding facebook login into my website - something I was hoping to be simple but proving difficult.
(Built with: Node.js/Express API and Angular front end)
So my issues I kindly ask help with:
1 - Instead of successRedirect return a token
I'm using passport-facebook to help with the login process however in the callback instead of the successRedirect can i just say return some json containing a token. Note the token is a JWT one that i already use with standard email authentication and I currently create this in the passport.serializeUser:
passport.serializeUser(function(user, done) {
var token = ///implementation for token
done(null, user);
});
And then in the callback do something like:
router.get('/login/facebook/callback',
passport.authenticate('facebook', {
res.json({success: true, token:token)
})
);
2 - How do handle on the client side
Then the second bit i can't get my head around is if i return the token, on my client side I'll be 'picking' that up in a usual $http.get method. But how will the URL flow work - by this I mean i have a 'login with facebook' button, this then calls the server side router.get('/login/facebook' which redirects to the facebook login page so once thats done and a token is returned how will it know to go back to the home page.
Hope my questions above make sense - am new to Angular so please excuse me if not clear will to best i can to explain.
SOLUTION (please let me know your thoughts if there is something bad I'm doing) So after some more thought, I've solved this using the following:
1 - In the callback I made a new API get route. I then send the user ID in the URL to create my custom JWT token:
router.get('/login/facebook/callback',
passport.authenticate('facebook',
{ session: false, failureRedirect: "/processfblogin/false" }),
function(req, res) {
res.redirect("/api/processfblogin/" + req.user._id);
}
);
Then from here i make the token and send in the URL.
So this is where it got interesting. I ideally wanted to use a $http get however CORs kept giving me a 302. So eventually I attached the token as a URL parameter and send it as:
res.redirect('http://localhost:3000/#/fbToProcess/' + token);
I then made this single page with its own controller that simply takes the token, saves it and everything auth wise clicks on my page.
I'll continue looking for a way to send the token in a json response so the user doesnt need to go to this new empty page however until then it works :-)