2012-04-11 21 views
8

Uygulamamdaki pasaport modülünü (github kimlik doğrulaması) kullanıyorum ve eyleme bağlı olarak yeniden yönlendirmek istiyorum ... sadece normal bir giriş olup olmadığını kontrol ediyorum. ilk kez oturum açar. Benim findOrCreateUser işlevindepasaport: giriş ve hesap kaydı için farklı yönlendirme

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); 

    }); 
    } 
)); 

i test için ben işlevi yalnızca ya da "new_registration "login" diyen bir dize olan bir msg değişkeni geri dönelim ... Bu yeni bir kullanıcı olup olmadığını kontrol edin ve tüm db eylem yapmak ".

bu yüzden sorum, pasaport auth bittikten sonra buna göre ("/ welcome" veya "/ back_again") yeniden yönlendirebilmem için findOrCreateUser öğesinden aldığım değişkeni "aktarma" yöntemidir.

Uygulamamda diğer pasaport kodu: doğrulamak Geri arama durumunda

// 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('/'); 
    }); 

cevap

9

, ben değiştirecek şeyler kadar findOrCreateUser işlev geri arama için fiili kayıt besler ve sonra done() kadar o geçmesi, böylece şöyle: Artık

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); 

kimlik doğrulama sonra geri arama URL'sini işlerken, bu kullanıcı kaydını kontrol ederek eğer yeni ise (Buranın bir isNew özelliği vardır varsayarak) bakın:

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

Teşekkürler, iyi cevap! – toxinlabs

+2

@Jared Hanson, O da benim için çalıştı. Bunu öğrenebileceğim bir site veya doküman var mı? –

İlgili konular