代码之家  ›  专栏  ›  技术社区  ›  Thuan Nguyen

Google OAuth Nodejs使用passport处理错误,passport-Google-oauth20

  •  0
  • Thuan Nguyen  · 技术社区  · 7 年前

    我正在使用 passport , passport-google-oauth20

    一切都是工作,但问题是现在我想通过域验证用户的电子邮件。我的系统只允许使用domain@framgia.com的电子邮件登录。如果没有,则向用户发回一条消息。

    const passport = require('passport');
    const GoogleStrategy = require('passport-google-oauth20').Strategy;
    
    const mongoose = require('mongoose');
    const keys = require('../config/keys');
    
    const User = mongoose.model('users');
    
    passport.serializeUser((user, done) => {
      done(null, user.id);
    });
    
    passport.deserializeUser((id, done) => {
      User.findById(id).then(user => {
        done(null, user);
      })
    });
    
    passport.use(
      new GoogleStrategy(
        {
          clientID: keys.googleClientID,
          clientSecret: keys.googleClientSecret,
          callbackURL: '/auth/google/callback',
        },
        async (accessToken, refreshToken, profile, done) => {
    
          const existingUser = await User.findOne({ googleId: profile.id });
          if (existingUser) {
            return done(null, existingUser);
          }
    
          if (!profile._json.domain || profile._json.domain !== 'framgia.com') {
            return done(null, {error: 'Not allow access!'});
          }
    
          const user = await new User({
            googleId: profile.id,
            email: profile.emails[0].value,
            name: profile.displayName,
            avatar: profile.photos[0].value,
          }).save();
    
          done(null, user);
        },
      ),
    );
    

    我写的逻辑代码是这样的:

    if (!profile._json.domain || profile._json.domain !== 'framgia.com') {
        return done(null, {error: 'Not allow access!'});
    }
    

    但我认为它不会工作,但我不知道如何处理错误并将消息发送回用户。

    const passport = require('passport');
    
    module.exports = (app) => {
      app.get(
        '/auth/google',
        passport.authenticate('google', {
          scope: ['profile', 'email'],
        }),
      );
    
      app.get(
        '/auth/google/callback',
        passport.authenticate('google', { failureRedirect: '/login' }),
        (req, res) => {
          // Successful authentication, redirect home.
          res.redirect('/');
        },
      );
    };
    

    如何处理错误并用一些消息重定向到路由/错误?

    如有任何意见,将不胜感激,谢谢。

    1 回复  |  直到 7 年前
        1
  •  2
  •   zerosand1s    7 年前

    首先,如果您只想在电子邮件具有某个域时返回用户,则需要在返回之前设置域检查逻辑 findOne() . 按照目前的逻辑,如果你找到一个用户,它只会返回它而不检查电子邮件域

    //check email domain before finding the user
    
    if (!profile._json.domain || profile._json.domain !== 'framgia.com') {
      return done(null, {error: 'Not allow access!'});
    }
    
    const existingUser = await User.findOne({ googleId: profile.id });
    if (existingUser) {
      return done(null, existingUser);
    }
    

    http://www.passportjs.org/docs/configure/ (检查验证回调部分)

    失败。这对于显示flash消息提示很有用

    因此,如果域不匹配,应该返回如下错误

    return done(null, false, { message: 'Not allow access!' });