简体   繁体   English

使用Google oauth访问时创建与登录

[英]Create vs Login when using Google oauth for access

I am currently trying to setup my server to allow users to login with google oauth 2.0. 我目前正在尝试设置服务器,以允许用户使用google oauth 2.0登录。

I am using passport and passport-google-oauth. 我正在使用护照和passport-google-oauth。

Normal set up is something like: 正常设置类似于:

var GoogleStrategy = require('passport-google-oauth').OAuth2Strategy; 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);
    });
  }
));

However what I really want is to still control access to my server after accounts are approved. 但是,我真正想要的是在帐户获得批准后仍然控制对服务器的访问。

Meaning a user would first 'create' and account using google, then be able to signin once there account is approved. 这意味着用户将首先使用Google“创建”和帐户,然后一旦该帐户获得批准就可以登录。

I would really like there to be a signup route and login route: 我真的很希望有一个注册路线和登录路线:

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('/');
  });

My problem is that when I get to the GoogleStrategy setup I don't really know which route they initially hit. 我的问题是,当我进入GoogleStrategy设置时,并不真正知道他们最初选择的路线。 IE if they hit the login route but had not created an account I do not want to create an account I want to warn them that they did not yet create an account. IE浏览器,如果他们点击了登录路径,但是还没有创建一个帐户,我不想创建一个帐户。我想警告他们,他们还没有创建一个帐户。 Had they hit the signup route and already had an account I would not want to create another account I would just tell them they already have an account. 如果他们按了注册路线并且已经有一个帐户,我不想创建另一个帐户,我只是告诉他们他们已经有一个帐户。

Is there anyway in the GoogleStrategy that I can tell which route the user initially hit on my server? 无论如何,GoogleStrategy中是否可以告诉用户最初在我的服务器上命中的路由?

In your user model create the "approved" field, with default False (Boolean) 在您的用户模型中,创建“批准”字段,默认为False(布尔值)

And you can check this field on the GoogleStrategy to restrict the access. 并且您可以在GoogleStrategy上检查此字段以限制访问。

If you want to apply this on all Strategies you can filter on the serialization method in passport. 如果要将其应用于所有策略,则可以过滤护照中的序列化方法。

Hope it helps. 希望能帮助到你。

You can pass a 'state' query parameter in your initial request that will be round-tripped back to your callback. 您可以在初始请求中传递一个“状态”查询参数,该参数将返回给您的回调。

Documented here: https://developers.google.com/identity/protocols/OAuth2WebServer 此处记录: 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. state Any string在收到响应后,提供可能对您的应用程序有用的任何状态。 The Google Authorization Server roundtrips this parameter, so your application receives the same value it sent. Google授权服务器会往返传递此参数,因此您的应用程序会收到与发送的相同值。 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. 为了减轻跨站点请求伪造(CSRF),强烈建议在状态中包含一个防伪令牌,并在响应中进行确认。 See OpenID Connect for an example of how to do this. 有关如何执行此操作的示例,请参见OpenID Connect。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何将Json Web Token(JWT)和Google oauth结合使用passwordjs,passport-google-oauth和node来创建社交登录系统? - How to combine Json Web Token(JWT) and google oauth using passportjs, passport-google-oauth and node to create social login system? 部署到 Heroku 时无法使用 Google OAuth 登录 - Unable to login with Google OAuth when deployed to Heroku 使用Passport-google-oauth20时自动登录 - Auto login while using passport-google-oauth20 使用Node.js和Google&Javascript进行OAuth 2登录 - OAuth 2 Login using Node.js and Google & Javascript 如何使用谷歌 api、oauth 验证登录? - How can I verify login using google api, oauth? 添加Meteor登录google oauth - Add Meteor login google oauth Google登录-尝试访问userinfo时出现401错误“需要登录” - Google Login - 401 error “Login Required” when trying to access userinfo Google OAuth2 + react-google-login:尝试检索刷新令牌时出现“redirect_uri_mismatch” - Google OAuth2 + react-google-login: "redirect_uri_mismatch" when trying to retrieve a refresh token Google OAuth2:尝试访问 Google Drive API 时,“错误:没有访问、刷新令牌或 API 密钥已设置” - Google OAuth2: "Error: No access, refresh token or API key is set" when trying to access Google Drive API OAuth使用Google令牌访问Jira? - OAuth access Jira with google token?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM