私は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%確信していません。
さらに詳しい情報やコードが必要な場合は、私に教えてください。
これはあなたのapp.jsコードですか? – Nivesh
'app.use(passport.initialize());'と 'app.use(passport.session());'も設定しました – Nivesh
これは私のサーバーです。jsファイル(すべてではなく、パスポート部分のみ)とはい、私は両方をしました – leofontes