2012-11-03 20 views
6

में कस्टम रीडायरेक्ट्स मैं अपने पहले सार्वजनिक सामना नोड ऐप के लिए मूल प्रमाणीकरण स्थापित करने की कोशिश कर रहा हूं। मेरे पास दो पासपोर्ट रणनीतियों हैं: 1) फेसबुक के लिए और 2) ट्विटर के लिए। मैं कम से कम तब तक एक ईमेल/पासवर्ड सिस्टम शामिल करने की योजना नहीं बना रहा जब तक कि मैं सुरक्षा प्रभाव को समझ नहीं पाता। मैं उन्हें नए उपयोगकर्ताओं को बनाने के लिए बॉक्स के रास्ते में काम करने और मोंगोस में बांधने में सक्षम हूं।पासपोर्ट

अब मैं सामाजिक खाता उपयोगकर्ताओं को समर्पित करने पर काम करना चाहता हूं। इसलिए जब भी कोई नया उपयोगकर्ता ट्विटर लेखक के माध्यम से आता है तो मैं उन्हें ईमेल एकत्र करने वाले पृष्ठ पर रीडायरेक्ट करना चाहता हूं। मैं टोकन और प्रोफाइल ऑब्जेक्ट को सत्र चर के रूप में लौटा रहा हूं और फॉर्म को छिपे हुए फ़ील्ड के रूप में सबमिट करते समय उन्हें उस पृष्ठ पर पुन: उपयोग कर दूंगा।

हालांकि मैं समझ नहीं पा रहा हूं कि पासपोर्ट का उपयोग करके इस पहलू को कैसे कार्यान्वित किया जाए। यहां पर कोड है जो मैंने अभी तक प्रत्येक भाग के साथ क्या करने की कोशिश कर रहा है, इस पर टिप्पणी के साथ किया है। अनिवार्य रूप से मैं जांच रहा हूं कि क्या ट्विटर उपयोगकर्ता एक पुराना उपयोगकर्ता है, यदि नहीं, तो मैं सत्र चर सेट कर रहा हूं, जिसे मैं उपयोगकर्ता ऑब्जेक्ट को प्रारंभ करने के लिए/addemail पेज पर पुन: उपयोग करूंगा (ताकि सीरियलइज़, Deserialize फ़ंक्शन में कुछ कार्य करने के लिए (सुनिश्चित नहीं है कि मैं समझता हूं वास्तव में क्या सीरियलाइजिंग/Deserializing करता है)। अब यदि उपयोगकर्ता एक नया है, तो सत्र परिवर्तनीय NewTwitterUser सत्य है और मैं इसे उचित पृष्ठ पर रीडायरेक्ट करने के लिए ऑथ/कॉलबैक यूआरएल में जांचता हूं। हालांकि यह काम नहीं करता है।

//basic modules and setup 
var express = require('express') 
    , passport = require('passport') 
    , mongoose = require('mongoose') 
    , http = require('http') 
    , util = require('util') 
    , TwitterStrategy = require('passport-twitter').Strategy 
    , FacebookStrategy = require('passport-facebook').Strategy 
    , path = require('path'); 

var app = express(); 


//Mongodb setup 
var Schema = mongoose.Schema; 
var ObjectId = Schema.ObjectId; 

var UserSchema = new Schema({ 
    provider: String, 
    uid: String, 
    fb_uid: String, 
    twitter_uid: String, 
    name: String, 
    first_name: String, 
    gender: String, 
    fb_username: String, 
    twitter_username: String, 
    profile_pic: String, 
    email: String, 
    location: String, 
    birthday: String, 
    created: {type: Date, default: Date.now} 
}); 


var User = mongoose.model('User', UserSchema); 
mongoose.connect('MongoHQ db connection here')' 


//User Authentication - Twitter 
passport.use(new TwitterStrategy({ 
    consumerKey: 'KEY', 
    consumerSecret : 'SECRET', 
    callbackURL: "CALLBACKURL", 
    passReqToCallback: true 
}, 
    function(req, token, tokenSecret, profile, done){ 
     User.findOne({twitter_uid: profile.id}, function(err, user){ 
      if (err) { 
      console.log('this is an error 1' + err); 
      return done(err);} 
      if(user){ 
       console.log('this user' + user); 
       done(null, user); 
      } else { 
       console.log('this is a new user'); 
       req.session.token = token; 
       req.session.tokenSecret = tokenSecret; 
       req.session.profile = profile; 
       req.session.newtwitteruser = true; 
       var user = new User(); 
       user.uid = profile.id; 
       done(null, user); 

       /* This part is commented and is the default code I had if I needed to simply create a Twitter User right here. 
       var user = new User(); 
       user.provider = profile.provider; 
       user.uid = profile.id; 
       user.twitter_uid = profile.id; 
       user.name = profile.displayName; 
       user.first_name = profile.displayName[0]; 
       user.twitter_username = profile._json.screen_name; 
       user.profile_pic = profile._json.profile_image_url; 
       user.location = profile._json.location; 
       user.save(function(err){ 
        if(err) {throw err;} 
        else {done(null, user);} 
       });*/ 
      } 
     }); 
    } 
)); 


//User Authentication - Facebook 
passport.use(new FacebookStrategy({ 
    clientID: 'ID', 
    clientSecret: 'SECRET', 
    callbackURL: "URL" 
}, 
    function(accessToken, refreshToken, profile, done){ 
     User.findOne({fb_uid: profile.id}, function(err, user){ 
      if (err) {return done(err);} 
      if(user){ 
       done(null, user); 
      } else { 
       var user = new User(); 
       user.provider = profile.provider; 
       user.uid = profile.id; 
       user.fb_uid = profile.id; 
       user.name = profile.displayName; 
       user.first_name = profile._json.first_name; 
       user.gender = profile._json.gender; 
       user.fb_username = profile._json.username; 
       user.profile_pic = 'https://graph.facebook.com/' + profile.id + '/picture'; 
       user.email = profile._json.email; 
       user.location = profile._json.location.name; 
       user.birthday = profile._json.birthday; 
       user.save(function(err){ 
        if(err) {throw err;} 
        else {done(null, user);} 
       }); 
      } 
     }) 
    } 
)); 


passport.serializeUser(function(user, done) { 
    done(null, user.uid); 
}); 

passport.deserializeUser(function(uid, done) { 
    User.findOne({uid: uid}, function (err, user) { 
    done(err, user); 
    }); 
}); 


//app configurations 
app.configure(function(){ 
    app.set('port', process.env.PORT || 3000); 
    app.set('views', __dirname + '/views'); 
    app.set('view engine', 'jade'); 
    app.use(express.bodyParser()); 
    app.use(express.methodOverride()); 
    app.use(express.cookieParser("freecookie")); 
    app.use(express.session({secret:"freecookie"})); 
    app.use(express.static(path.join(__dirname, 'public'))); 
    app.use(express.errorHandler()); 
    app.use(passport.initialize()); 
    app.use(passport.session()); 
    app.use(app.router); 
}); 


//Basic Routing 
app.get('/', function(req, res){ 
    res.render('home', {title: 'App Title', user: req.user}); 
}); 


app.get('/auth/twitter', passport.authenticate('twitter')); 

app.get('/auth/twitter/callback', 
    passport.authenticate('twitter', {failureRedirect: '/login' }), 
    function(req, res) { 
     if (req.session.newtwitteruser){ 
     res.redirect('/addemail');} 
     else {res.redirect('/');} 
    }); 

app.get('/addemail', function(req, res){ 
    if (req.session.newtwitteruser){ 
    res.render('email', {title: 'Add your Email'});} 
    else {res.redirect('/');} 
}); 


app.get('/auth/facebook', passport.authenticate('facebook', {scope: ['email', 'user_location', 'user_birthday'] })); 

app.get('/auth/facebook/callback', 
    passport.authenticate('facebook', { successRedirect: '/', failureRedirect: '/login' })); 


app.get('/logout', function(req, res){ 
    req.logout(); 
    res.redirect('/'); 
}); 



//create the server 
var server = http.createServer(app); 
server.listen(app.get('port')); 


//Checks if a request is authenticated 
function ensureAuthenticated(req, res, next) { 
    if (req.isAuthenticated()) { return next(); } 
    res.redirect('/login') 
} 

उत्तर

6

मैं पहली बार अपने चहचहाना मार्ग के अंदर अपने addemail कार्य करने के लिए सफल लॉगिन अनुप्रेषित होगा:

app.get('/auth/twitter', passport.authenticate('twitter')); 
    app.get('/auth/twitter/callback', 
    passport.authenticate('twitter', { successRedirect: '/addemail', 
             failureRedirect: '/' })); 

गु एन, आपके एडेमेल फ़ंक्शन के अंदर, मैं यह देखने के लिए जांच करूंगा कि उनके पास पहले से कोई ईमेल है या नहीं, जिसके बाद आप उन्हें उचित स्थान पर रीडायरेक्ट कर सकते हैं।

app.get('/addemail', function(req, res){ 
     if (req.user.email){ 
      res.render('email', {title: 'Add your Email'});} 
     else {res.redirect('/');} 
    }); 
संबंधित मुद्दे