使用 Google oauth 进行访问时创建 vs 登录

Create vs Login when using Google oauth for access

我目前正在尝试设置我的服务器以允许用户使用 google oauth 2.0 登录。

我正在使用 passport 和 passport-google-oauth.

正常设置是这样的:

var GoogleStrategy = require('passport-google-oauth').OAuth2Strategy;

passport.use(new GoogleStrategy({
    clientID: GOOGLE_CLIENT_ID,
    clientSecret: GOOGLE_CLIENT_SECRET,
    callbackURL: "http://127.0.0.1:3000/auth/google/callback"
  },
  function(accessToken, refreshToken, profile, done) {
    User.findOrCreate({ googleId: profile.id }, function (err, user) {
      return done(err, user);
    });
  }
));

然而我真正想要的是在帐户被批准后仍然控制对我的服务器的访问。

意味着用户首先 'create' 和使用 google 的帐户,然后在帐户获得批准后才能登录。

我非常希望有一个注册路径和登录路径:

app.get('/auth/google/signup',
  passport.authenticate('google', { scope: ['profile', 'email'] }));

app.get('/auth/google',
  passport.authenticate('google', { scope: 'https://www.googleapis.com/auth/plus.login' }));

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

我的问题是,当我进入 GoogleStrategy 设置时,我真的不知道他们最初点击了哪条路线。 IE 如果他们点击了登录路径但还没有创建一个帐户我不想创建一个帐户我想警告他们他们还没有创建一个帐户。如果他们点击注册路线并且已经有一个帐户,我不想再创建一个帐户,我只会告诉他们他们已经有一个帐户。

GoogleStrategy 中是否有任何东西可以告诉我用户最初在我的服务器上点击了哪条路线?

在您的用户模型中创建 "approved" 字段,默认为 False(布尔值)

并且您可以在GoogleStrategy上勾选这个字段来限制访问。

如果您想将此应用于所有策略,您可以在 passport 中过滤序列化方法。

希望对您有所帮助。

您可以在初始请求中传递一个 'state' 查询参数,该参数将返回给您的回调。

记录于此: https://developers.google.com/identity/protocols/OAuth2WebServer

state Any string Provides any state that might be useful to your application upon receipt of the response. The Google Authorization Server roundtrips this parameter, so your application receives the same value it sent. To mitigate against cross-site request forgery (CSRF), it is strongly recommended to include an anti-forgery token in the state, and confirm it in the response. See OpenID Connect for an example of how to do this.