これに対する可能なアプローチの簡単なスケッチは次のとおりです。
findOrCreateUser
Passportはメソッドを提供しないことに注意してください。すべてのデータベース管理とレコード作成は、アプリケーションによって定義されます(必要に応じて)。Passportは、認証のための機能を提供するだけです。
このアプローチの鍵は、Twitterから提供されたプロファイルデータから、データベースに「不完全な」ユーザーレコードを作成することです。次に、アプリケーションのルートで、必要な条件が満たされているかどうかを確認できます。そうでない場合は、不足している詳細を入力するように求められるフォームにユーザーをリダイレクトします。
passport.use(new TwitterStrategy({
consumerKey: TWITTER_CONSUMER_KEY,
consumerSecret: TWITTER_CONSUMER_SECRET,
callbackURL: "http://127.0.0.1:3000/auth/twitter/callback"
},
function(token, tokenSecret, profile, done) {
// Create a user object in your database, using the profile data given by
// Twitter. It may not yet be a "complete" profile, but that will be handled
// later.
return done(null, user);
}
));
app.get('/auth/twitter',
passport.authenticate('twitter'));
app.get('/auth/twitter/callback',
passport.authenticate('twitter', { failureRedirect: '/login' }),
function(req, res) {
// The user has authenticated with Twitter. Now check to see if the profile
// is "complete". If not, send them down a flow to fill out more details.
if (req.user.isCompleteProfile()) {
res.redirect('/home');
} else {
res.redirect('/complete-profile');
}
});
app.get('/complete-profile', function(req, res) {
res.render('profile-form', { user: req.user });
});
app.post('/update-profile', function(req, res) {
// Grab the missing information from the form and update the profile.
res.redirect('/home');
});