0

StackOverflow に似たログイン/サインアップ システムを実装しようとしています。つまり、次のようになります。

  1. サインアップとログインの両方のリンクが /users/login に移動します。
  2. ユーザーは、サインアップ中かログイン中かに関係なく、OAuth プロバイダー (Google など) をクリックします。
  3. OAuth コールバックは、アカウントがまだ存在しない場合 (アカウントの作成を確認するページ)、/users/authenticate に移動します。または、アカウントが既に存在する場合は / に移動します。
  4. (アカウントが新しい場合は、ここに管理者アカウントの確認手順を追加しますが、この質問にはあまり重要ではありません。)

私はこれについて正しく行っているかどうかわかりません。以下の関連コード。

プロファイルがデータベースに存在するかどうかを確認します。そうでない場合は、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."}]});
        });
    });
};

これを行う「正しい」方法は何ですか?私の検証コールバック コードが不適切であることは確かです。ありがとう-

4

1 に答える 1

0

OpenId を使用する方法passport-google: OpenId 構成戦略

Oauth の方法passport-google-oauth: OAuth 構成戦略

あなたのコードを読むことによって、あなたが適用しようとしているものを実際に知ることはできません。OAuth のようですが、不要なルートが表示さ/users/openidconfirmれます。

見つけた 2 つのリンクを紹介します。

  1. OpenID と OAuth
  2. OpenID と OAuth の違いは何ですか?

質問/トラブルを改善して、より良い回答を詳しく説明できるかもしれません。

これが役立つことを願っています。

于 2013-07-21T18:55:48.133 に答える