简体   繁体   中英

?What is the use of a proxy in passport google-oauth20 in nodejs?

i write a authentication for weblog with pasport.js and i see a option, name is proxy. i search about this but i cat find a good asnwer. and i dont know whats this option use for. can someone explain this option for me? thank you.

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));
  });
}

The GoogleStrategy is based on the OAuthStrategy, and the OAuth protocol is based on redirecting the client to the identity provider, passing an URL to redirect back with the redirect, which the identity provider can then use to return the user to the relying party. To determine that URL, the strategy uses the host field of the request. However if the application is running behind a proxy, the domain the end-user sees might be different. If the proxy option is set to true, the X-Forwarded-Host header is used instead, so that the correct domain is used for the enduser.

reference

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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