2012-04-11 14 views
8

मैं अपने ऐप में पासपोर्ट मॉड्यूल (जीथब प्रमाणीकरण) का उपयोग कर रहा हूं और मैं कार्रवाई के आधार पर रीडायरेक्ट करना चाहता हूं ... मैं जांचता हूं कि यह सिर्फ एक सामान्य लॉगिन है या उपयोगकर्ता पहली बार लॉग इन करता है।पासपोर्ट: लॉगिन और खाता पंजीकरण के लिए अलग-अलग रीडायरेक्ट

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

    }); 
    } 
)); 
मेरी findOrCreateUser समारोह में

मैं जाँच अगर यह एक नया उपयोगकर्ता है और सभी db कार्रवाई करना ... मैं परीक्षण के लिए समारोह एक संदेश चर जो केवल एक स्ट्रिंग है कहते हैं, "के लिए लॉग इन" या "new_registration है लौट जाने "।

तो मेरा सवाल यह है कि मैं उस चर को "परिवहन" कैसे प्राप्त करूं जो मुझे खोजने के लिए मिलता है IrCreateUser ताकि पासपोर्ट ऑथ समाप्त होने के बाद मैं तदनुसार ("/ स्वागत" या "/ back_again") रीडायरेक्ट कर सकूं।

मेरे एप्लिकेशन में अन्य पासपोर्ट कोड:

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

उत्तर

9

अपने को सत्यापित कॉलबैक में, मैं बदल जाएगा चीजों को इतना findOrCreateUser समारोह कॉलबैक करने के लिए वास्तविक रिकॉर्ड की आपूर्ति, और उसके बाद से पारित है कि done() के माध्यम से है कि, इसलिए जैसे:

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

अब, जब प्रमाणीकरण के बाद कॉलबैक URL से निपटने, तो आप इस उपयोगकर्ता रिकॉर्ड की जांच कर सकते हैं और अगर यह नया था देख (मैं यह सोचते हैं रहा हूँ यह एक isNew संपत्ति यहाँ है):

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

धन्यवाद, अच्छा जवाब! – toxinlabs

+2

@ जेरेड हैंनसन, यह मेरे लिए भी काम करता था। क्या कोई साइट या दस्तावेज है जहां मैं इसे सीख सकता हूं? –

संबंधित मुद्दे