【问题标题】:Google OAuth Nodejs handle error using passport, passport-google-oauth20Google OAuth Nodejs 使用护照、passport-google-oauth20 处理错误
【发布时间】:2019-03-02 22:13:47
【问题描述】:

我正在使用 passportpassport-google-oauth20 编写身份验证 Nodejs API

一切正常,但问题是现在我想通过域验证用户的电子邮件。我的系统只允许@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('/');
    },
  );
};

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

任何想法将不胜感激,谢谢。

【问题讨论】:

    标签: node.js express authentication passport.js google-oauth


    【解决方案1】:

    首先,如果您只想在电子邮件具有特定域的情况下返回用户,您需要将您的域检查逻辑放在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);
    }
    

    根据passport js文档,http://www.passportjs.org/docs/configure/(检查验证回调部分)

    可以提供额外的信息消息来说明原因 失败。这对于显示 Flash 消息提示很有用 用户再试一次。

    所以如果域不匹配,你应该返回这样的错误

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

    【讨论】:

    • 感谢您的回复,那么如何获取该消息对象并将其显示给用户?
    • 更新了有关如何获取该消息并为用户显示的答案。
    猜你喜欢
    • 1970-01-01
    • 2021-03-27
    • 2018-07-04
    • 2021-03-27
    • 1970-01-01
    • 2018-12-02
    • 1970-01-01
    • 1970-01-01
    • 2022-10-24
    相关资源
    最近更新 更多