在Node.js中使用Passport和Google OAuth2.0时,代理的作用是什么?

3

我使用passport.js为web日志编写身份验证功能,发现了一个名为代理(proxy)的选项。我搜索了一下,但是没有找到好的答案,也不知道这个选项的作用。有人能为我解释一下吗?谢谢。

const GoogleStrategy = require('passport-google-oauth20').Strategy;
const user=require("./../models/users")
// const mongoose = require('mongoose');
// const keys = require('./keys');
// Load user model
//const User = mongoose.model('users');

 module.exports = function(passport){
  passport.use(
    new GoogleStrategy({
      clientID:'===================================================',
      clientSecret:'=================================================',
      callbackURL:'/auth/google/callback',
      proxy: true
    }, (accessToken, refreshToken, profile, done) => {
      // console.log(accessToken);
      // console.log(profile);

      const image = profile.photos[0].value.substring(0, profile.photos[0].value.indexOf('?'));
      
      const newUser = {
        googleID: profile.id,
        firstName: profile.name.givenName,
        lastName: profile.name.familyName,
        email: profile.emails[0].value,
        image: image
      }

      // Check for existing user
      User.findOne({
        googleID: profile.id
      }).then(user => {
        if(user){
          // Return user
          done(null, user);
        } else {
          // Create user
          new User(newUser)
            .save()
            .then(user => done(null, user));
        }
      })
    })
  );

  passport.serializeUser((user, done) => {
    done(null, user.id);
  });

  passport.deserializeUser((id, done) => {
    User.findById(id).then(user => done(null, user));
  });
}
1个回答

3
GoogleStrategy基于OAuthStrategy,OAuth协议基于将客户端重定向到身份提供者,并传递一个URL以便在重定向时返回重定向的URL,身份提供者可以使用该URL将用户返回给依赖方。为了确定该URL,该策略使用请求的主机字段。但是,如果应用程序在代理后面运行,则最终用户看到的域可能不同。如果将proxy选项设置为true,则使用X-Forwarded-Host头,以便为最终用户使用正确的域。
参考:reference

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接