2016-06-22 19 views
1

私はNodeJSで講座やチュートリアルをしばらく視聴しており、アプリでうまく利用することに決めました。NodeJS Passport Facebook OAuth

このプロジェクトでは、アクティビティをデータベースに保存するには、サインインしてログインする必要があります。私はこのプロセスを行うためにパスポートを使用し、プロジェクトのこのセクションのコードはこれです:

/****** Passport functions ******/ 
passport.serializeUser(function (user, done) { 
    done(null, user.id); 
}); 

passport.deserializeUser(function (id, done) { 
    db.user.findOne({ where : { idUser : id } }).then(function (err, user) { 
     done(err, user); 
    }); 
}); 

//Facebook 
passport.use(new FacebookStrategy({ 
    //Information stored on config/auth.js 
    clientID: configAuth.facebookAuth.clientID, 
    clientSecret: configAuth.facebookAuth.clientSecret, 
    callbackURL: configAuth.facebookAuth.callbackURL, 
    profileFields: ['id', 'emails', 'displayName', 'name', 'gender'] 

}, function (accessToken, refreshToken, profile, done) { 
    //Using next tick to take advantage of async properties 
    process.nextTick(function() { 
     db.user.findOne({ where : { idUser : profile.id } }).then(function (err, user) { 
      if(err) { 
       return done(err); 
      } 
      if(user) { 
       return done(null, user); 
      } else { 
       db.user.create({ 
        idUser : profile.id, 
        token : accessToken, 
        nameUser : profile.displayName, 
        email : profile.emails[0].value, 
        sex : profile.gender 
       }); 
       return done(null); 
      } 
     }); 
    }); 
})); 

app.use(express.static(__dirname + '/public/')); 

/* FACEBOOK STRATEGY */ 
// Redirect the user to Facebook for authentication. When complete, 
// Facebook will redirect the user back to the application at 
//  /auth/facebook/callback// 
app.get('/auth/facebook', passport.authenticate('facebook', { scope : ['email']})); 
/* FACEBOOK STRATEGY */ 
// Facebook will redirect the user to this URL after approval. Finish the 
// authentication process by attempting to obtain an access token. If 
// access was granted, the user will be logged in. Otherwise, 
// authentication has failed. 
app.get('/auth/facebook/callback', 
    passport.authenticate('facebook', { successRedirect: '/app', 
             failureRedirect: '/' })); 

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

app.get('/app', isLoggedIn, function (req, res) { 
    res.sendFile('app.html'); 
}); 

function isLoggedIn(req, res, next) { 
    if(req.isAuthenticated()) { 
     return next(); 
    } else { 
     res.redirect('/'); 
    } 
} 

私はパスポートを使用してFacebookの認証に続くチュートリアルはほとんど同じコードを使用し、私はチュートリアルので、Userモデルを変更しましたモンゴースを使用して私は Sequelizeを使用していますが、この面はうまくいきます.FBを使って申し込んでFBを登録したり、ログインしたりすると、クエリが機能します。

ただし、動作していないのはリダイレクトです。私がFacebookに登録すると、スタックされて何もロードされません(FBボタンがあるindex.htmlにホイールが回転して何もロードされません)。私はFacebookの使用してログインすると、それだけで画面にこれを表示します。

[オブジェクトSequelizeInstance:ユーザー]

をチュートリアルでは、講師はテンプレート言語としてEJSを使用し、しかし、私はすでに95を構築しましたHTML、CSS、jQueryを使用したプロジェクトのフロントエンドの%(ReactまたはAngularを使用していたはずですが、時間は敏感で、既にNodeを学習していました)。私はこれが起こっている理由の一つだと信じていますが、私はここで何が起こっているのか、どうして私がエラーを受けているのか、どうやって回避するのかについて100%確信していません。

さらに詳しい情報やコードが必要な場合は、私に教えてください。

+0

これはあなたのapp.jsコードですか? – Nivesh

+0

'app.use(passport.initialize());'と 'app.use(passport.session());'も設定しました – Nivesh

+0

これは私のサーバーです。jsファイル(すべてではなく、パスポート部分のみ)とはい、私は両方をしました – leofontes

答えて

3

デバッグに時間がかかり、助けを借りて、問題の原因を突き止めたところ、実際には3つのエラーがありました。すべての

まず、Facebookの戦略では、これは私がそれを作っていた方法を示します。

passport.use(new FacebookStrategy({ 
    //Information stored on config/auth.js 
    clientID: configAuth.facebookAuth.clientID, 
    clientSecret: configAuth.facebookAuth.clientSecret, 
    callbackURL: configAuth.facebookAuth.callbackURL, 
    profileFields: ['id', 'emails', 'displayName', 'name', 'gender'] 

}, function (accessToken, refreshToken, profile, done) { 
    //Using next tick to take advantage of async properties 
    process.nextTick(function() { 
     db.user.findOne({ where : { idUser : profile.id } }).then(function (user, err) { 
      if(err) { 
       return done(err); 
      } 
      if(user) { 
       return done(null, user); 
      } else { 
       //Create the user 
       db.user.create({ 
        idUser : profile.id, 
        token : accessToken, 
        nameUser : profile.displayName, 
        email : profile.emails[0].value, 
        sex : profile.gender 
       }); 

       //Find the user (therefore checking if it was indeed created) and return it 
       db.user.findOne({ where : { idUser : profile.id } }).then(function (user, err) { 
        if(user) { 
         return done(null, user); 
        } else { 
         return done(err); 
        } 
       }); 
      } 
     }); 
    }); 
})); 

db.user.findOne後のコールバックは、パラメータを切り替えていたので、それは毎回私にエラーを与えていましたそれを持っていなかったにもかかわらず、私はそれらを切り替えて、それを返すことができるように作成した後にDBのユーザーを探すためのクエリを追加しました。第二のFacebookのルート上に

、これは私がそれを建てた方法です:

app.get('/auth/facebook/callback', 
    passport.authenticate('facebook', { failureRedirect: '/' }), 
    function(req, res) { 
     // Successful authentication, redirect home. 
     res.redirect('../../app.html'); 
    }); 

これは(私はおそらく後に、より良いビューを使用するように書き換えます)私はHTMLを使用して継続させ、そしてテストにreq.userから情報を取得することができました。

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

はちょうど私が使用される命名規則を維持するためにuser.idUserにuser.idから変更:

最後に、私はパスポートのserializeUser上の軽微な命名エラーが発生しました。

これは、他の人がPasselizeでSequelizeを使用するのに役立ちます。