简体   繁体   English

如何允许用户在NodeJS PassportJS失败后尝试不同的电子邮件?

[英]How to allow user to try a different email after failure in NodeJS PassportJS?

Currently I am checking whether or not a user has a certain domain name email address using the following code. 目前,我正在使用以下代码检查用户是否具有某个域名电子邮件地址。 Initially, when the google authentication window shows, there is no option for the user to change their email address if they are already logged into Chrome. 最初,当Google身份验证窗口显示时,如果用户已经登录Chrome,则无法更改其电子邮件地址。 When I log into similar sites with Google authentication, I seemingly recalled being allowed to add an email address, or something of that nature. 当我使用Google身份验证登录类似网站时,我似乎回忆起被允许添加电子邮件地址或类似的东西。

So, say that the user attempts to log on with a non-lsmsa.edu email address and it fails. 因此,假设用户尝试使用nonlsmsa.edu电子邮件地址登录并且失败。 Currently it displays a nasty error. 目前它显示一个令人讨厌的错误。 How would I make it such that the user is allowed to attempt to re-login with a different email address. 如何使用户可以尝试使用其他电子邮件地址重新登录。

if ( profile.emails[0].value.indexOf("lsmsa.edu") > -1 ) {

    var newUser = new User()

    newUser.google.id    = profile.id
    newUser.google.token = token
    newUser.google.name  = profile.displayName
    newUser.google.email = profile.emails[0].value

    newUser.save(function(err) {
        if (err) throw err
        return done(null, newUser)
    })
}
else {
    done(new Error("Invalid Domain. Must use LSMSA email address."))
}

Check out the hd parameter . 查看hd参数 It makes sure that the user can only sign in with a proper email. 它确保用户只能使用正确的电子邮件登录。

EDIT: This isn't a per-request option. 编辑:这不是每个请求选项。 If you want to use it with passport-google-oauth , edit your config to be like this: 如果您想将它与passport-google-oauth ,请将您的配置编辑为:

passport.use(new GoogleStrategy({
    returnURL: 'http://www.example.com/auth/google/return',
    realm: 'http://www.example.com/',
    // Add this
    hd: 'example.com'
  },
  function(identifier, profile, done) {
    // Blah Blah Blah, Blow up Pluto, Milk Cows, Eat Chocolate, Etc.
  }
));

EDIT: If for some reason you have to have them login again, instead of using hd , just destroy the session ( req.session.destroy(); ), then redirect them to your authentication url (ie. /auth/google ). 编辑:如果由于某种原因你必须再次登录,而不是使用hd ,只需销毁会话( req.session.destroy(); ),然后将它们重定向到您的身份验证网址(即req.session.destroy(); /auth/google )。 However, using hd will be a much nicer user experience. 但是,使用hd将是一个更好的用户体验。

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM