【问题标题】:Redirecting users if they aren't using specific domain如果用户不使用特定域,则重定向用户
【发布时间】:2019-03-26 11:02:24
【问题描述】:

我正在尝试使用 res.redirect 将用户重定向到路由,或者如果他们尝试使用指定域以外的域登录,则使用 res.send 发送文件。条件检查正在工作,但我正在尝试使用 res.sendFile/res.redirect 但它似乎不在此函数的范围内工作。很明显,这个函数中没有 res,但这就是我想出的全部。在网上搜索得很好,但我还没有解决这个问题。

感谢任何帮助。

passport.use(
  new GoogleStrategy({
    callbackURL: '/google/redirect',
    clientID: keys.google.clientID,
    clientSecret: keys.google.clientSecret
}, function (accessToken, refreshToken, profile, done){
  if (profile._json.hd === "HIDDEN-DOMAIN.COM") {
    User.findOne({googleId : profile.id})
  .then(function(currentUser){
    if(currentUser){
      console.log('User with ID' + currentUser.googleId +' already exists. No new entry was made');
      done(null, currentUser);
    } else {
      new User({
        username: profile.displayName,
        googleId: profile.id
      })
      .save()
      .then(function(newUser){
        console.log('New user created: ' + newUser);
        done(null, newUser);
      });
    }
  })
} else {
  console.log(__dirname);
  res.sendFile('../login.html');
};
}));

【问题讨论】:

    标签: javascript express passport.js google-oauth


    【解决方案1】:

    使用中间件进行检查,如果通过则next()。 结帐:https://expressjs.com/en/guide/using-middleware.html

    这个例子展示了一个挂载在 / 路径上的中间件函数。该函数针对 / 路径上的任何类型的 HTTP 请求执行。

    这个例子展示了一个路由和它的处理函数(中间件系统)。该函数处理 GET 请求。

    app.use('/', function (req, res, next) {
      // Check 1
      console.log('Request URL:', req.originalUrl)
      next()
    }, function (req, res, next) {
      // Check 2: Pass first check
      console.log('Request Type:', req.method)
      next()
    })
    app.get('/', (req, res) => {
       // Final Route
    });
    

    例子:

    app.use('/first', function (req, res, next) {
        passport.use(
            new GoogleStrategy({
                callbackURL: '/google/redirect',
                clientID: keys.google.clientID,
                clientSecret: keys.google.clientSecret
            }, function (accessToken, refreshToken, profile, done){
                if (profile._json.hd === "HIDDEN-DOMAIN.COM") {
                    User.findOne({googleId : profile.id})
                        .then(function(currentUser){
                            if(currentUser){
                                console.log('User with ID' + currentUser.googleId +' already exists. No new entry was made');
                                done(null, currentUser);
                            } else {
                                new User({
                                    username: profile.displayName,
                                    googleId: profile.id
                                })
                                    .save()
                                    .then(function(newUser){
                                        console.log('New user created: ' + newUser);
                                        done(null, newUser);
                                        next(); // next();
                                    });
                            }
                        })
                } else {
                    console.log(__dirname);
                    next(); // next();
                }
            }));
    
    }, function (req, res, next) {
        // More checks
        next()
    });
    
    app('/', (req, res) => {
        // final route here
        res.sendFile('../login.html');
    })
    

    【讨论】:

    • 感谢您的回复。我稍后会试试这个并回复你!
    • @ShadowCoder 告诉我。
    • 这成功了!抱歉延迟尝试-感谢您的帮助:)
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-06-10
    • 2013-04-07
    • 1970-01-01
    • 2013-06-03
    • 2012-05-16
    • 1970-01-01
    相关资源
    最近更新 更多