2012-04-11 6 views
8

Ich benutze das Pass-Modul (Github-Authentifizierung) in meiner App und ich möchte je nach Aktion umleiten ... Ich überprüfe, ob es nur eine normale Anmeldung oder wenn der Benutzer ist meldet sich zum ersten Mal an.Pass: verschiedene Umleitung für Login und Account-Registrierung

passport.use(new GitHubStrategy({ 
    clientID: conf.github.app_id, 
    clientSecret: conf.github.app_secret, 
    callbackURL: conf.github.callback_url 
    }, 
    function(accessToken, refreshToken, profile, done) { 
    // asynchronous verification, for effect... 
    process.nextTick(function() { 

     // To keep the example simple, the user's GitHub profile is returned to 
     // represent the logged-in user. In a typical application, you would want 
     // to associate the GitHub account with a user record in your database, 
     // and return that user instead. 

     Models_User.findOrCreateUser(profile, function(msg){ 
     console.log("auth type:" + msg); 
     }); 

     return done(null, profile); 

    }); 
    } 
)); 

in meiner findOrCreateUser Funktion i überprüfen, ob es ein neuer Benutzer ist und tut das alles Handeln db ... zum Testen lassen i die Funktion eine Variable msg zurück, die nur eine Zeichenfolge, die „Login“ oder „new_registration sagt ".

Also meine Frage ist, wie man die Variable, die ich von findOrCreateUser bekomme, "transportiere", so dass ich dementsprechend umleiten kann ("/ welcome" oder "/ back_again"), nachdem die Passauth beendet ist.

der andere Pass-Code in meiner App:

// GET /auth/github 
// Use passport.authenticate() as route middleware to authenticate the 
// request. The first step in GitHub authentication will involve redirecting 
// the user to github.com. After authorization, GitHubwill redirect the user 
// back to this application at /auth/github/callback 
app.get('/auth/github', 
    passport.authenticate('github'), 
    //passport.authenticate('github', { scope: ['user', 'public_repo', 'gist'] }), 
    function(req, res){ 
    // The request will be redirected to GitHub for authentication, so this 
    // function will not be called. 
    }); 

// GET /auth/github/callback 
// Use passport.authenticate() as route middleware to authenticate the 
// request. If authentication fails, the user will be redirected back to the 
// login page. Otherwise, the primary route function function will be called, 
// which, in this example, will redirect the user to the home page. 
app.get('/auth/github/callback', 
    passport.authenticate('github', { successRedirect: '/', failureRedirect: '/login' }), 
    function(req, res) { 
    res.redirect('/'); 
    }); 

Antwort

9

In Ihrem Rückruf überprüfen, würde ich die Dinge ändern, so dass die findOrCreateUser Funktion den aktuellen Datensatz an die Callback liefert, und dann, dass passieren zu done(), so wie:

Models_User.findOrCreateUser(profile, function(user){ 
    console.log("auth type:" + msg); 
    return done(null, user); 
}); 

// take this out, use the actual model above 
//return done(null, profile); 

Nun, wenn die Callback-URL nach der Authentifizierung der Handhabung, können Sie diesen Benutzerdatensatz überprüfen und sehen, ob es neu war (ich nehme an, es eine isNew Eigenschaft hat hier):

app.get('/auth/github/callback', 
    passport.authenticate('github', { failureRedirect: '/login' }), 
    function(req, res) { 
    // successful auth, user is set at req.user. redirect as necessary. 
    if (req.user.isNew) { return res.redirect('/back_again'); } 
    res.redirect('/welcome'); 
    }); 
+0

danke, gute antwort! – toxinlabs

+2

@ Jared Hanson, Das hat auch für mich funktioniert. Gibt es eine Website oder eine Dokumentation, wo ich das lernen kann? –

Verwandte Themen