Passport.js, Express.js and Angular.js routing: how can they coexist?

I apologize, this question turned out to be a bit long, but I worked on it for some time and really needed an explanation of the whole story.

Background : The application is based on the MEAN stack, trying to authorize Facebook logins using Passport.js.

The following Passport.js guide I implemented something similar to:

// HTML
    <a href="/connect/facebook" target="_self">Add a Facebook login</a>
// send to facebook to do the authentication
app.get('/connect/facebook',isLoggedIn, passport.authorize('facebook', 
    { scope : 'email' })
);
// handle the callback after facebook has authorized the user
app.get('/connect/facebook/callback',
    passport.authorize('facebook', {
    successRedirect : '/profile',
    failureRedirect : '/profile'
    }));

Pay attention to target=_selfin html to skip Angular routing. Clearly, authorization is working fine. However, redirection does not work, as routing is done using Angular. After logging in, I never land on /profile(but by default Angular).

, Passport.js , json Angular, Angular . - :

// In the controller
$http.get("/connect/facebook").success(function(data){
    // here I wait for json data from the server and do the routing
});
// I call this route from Angular
app.get('/connect/facebook',isLoggedIn,passport.authorize('facebook', 
    { scope : 'email' })
);
// But Facebook lands here!
app.get('/connect/facebook/callback',function(req, res, next) {
    passport.authorize('facebook', function(err, user, info) {
        res.json({something:smtg});
        ...

, , Passport.js. ? /connect/facebook Angular, json /connect/facebook/callback.

Passport, - , /profile FB, , ? .

: Passport-Facebook GitHub. , .

+4
2

, , .

, Angular.js HTML-, , URL- . , Angular.js script , script. /# , , script. ( HTML). (, ), HTML 5 /# URL-, http://somesite.com/#/someroute http://somesite.com/someroute. , , /# . Angular.js HTML5 pushState (AKA HistoryAPI), .

, , Angular.js script, angular script . Angular.js . , . angular, /#/someroute . Angular.js, /# angular. , - , . , .

, successRedirect : '#/profile', , profile angular .

, .

+4

@Claies , , # = facebook.

0

All Articles