StackOverflow に似たログイン/サインアップ システムを実装しようとしています。つまり、次のようになります。
- サインアップとログインの両方のリンクが /users/login に移動します。
- ユーザーは、サインアップ中かログイン中かに関係なく、OAuth プロバイダー (Google など) をクリックします。
- OAuth コールバックは、アカウントがまだ存在しない場合 (アカウントの作成を確認するページ)、/users/authenticate に移動します。または、アカウントが既に存在する場合は / に移動します。
- (アカウントが新しい場合は、ここに管理者アカウントの確認手順を追加しますが、この質問にはあまり重要ではありません。)
私はこれについて正しく行っているかどうかわかりません。以下の関連コード。
プロファイルがデータベースに存在するかどうかを確認します。そうでない場合は、status = "new" のメモリ内プロファイルを返します。
passport.use(new GoogleStrategy({
clientID: config.google_client_id,
clientSecret: config.google_client_secret,
callbackURL: "/auth/google/callback"
},
function (accessToken, refreshToken, profile, done) {
// asynchronous verification, for effect...
process.nextTick(function () {
db.db.collection("users", function (err, collection) {
if (err) throw err;
collection.findOne({id: profile.id}, function (err, record) {
if (record) return done(null, record);
profile.status = "new";
done(null, profile);
});
});
});
})
);
ステータスに基づいて OAuth の後にリダイレクト ルートを選択します。
app.get('/auth/google/callback',
passport.authenticate('google', { failureRedirect: '/users/login' }),
function (req, res) {
switch (req.user.status) {
case "validated":
res.redirect('/'); break;
case "new":
res.redirect('/users/oauthconfirm'); break;
case "pending":
res.redirect('/users/login'); break;
}
}
);
そして最後に、新しいアカウントを確認するためのルート:
// app.js
app.get('/users/oauthconfirm', routes.users.oauthconfirm);
// routes/users.js
exports.oauthconfirm = function(req, res) {
db.db.collection("users", function (err, collection) {
if (err) throw err;
collection.insert(req.user, function (err, records) {
if (err) throw err;
res.render('login', {messages: [{status: "success", text:"Thank you. You will receive an e-mail when your account is validated."}]});
});
});
};
これを行う「正しい」方法は何ですか?私の検証コールバック コードが不適切であることは確かです。ありがとう-