2017-10-04 87 views
0

我是很新的Node.js,我正在學習用護照認證,在我的節點應用我有以下這與谷歌的用戶進行身份驗證和認證成功後,觸發回調線,如何通過護照發送成功重定向的用戶個人資料數據?

// send to google to do the authentication 
     app.get('/auth/google', passport.authenticate('google', { scope : ['profile', 'email'] })); 

     // the callback after google has authenticated the user 
     app.get('/auth/oauth/callback',passport.authenticate('google', {successRedirect : 'http://127.0.0.1:1234/testbot',failureRedirect : '/'})); 

我想要將用戶配置文件數據發送到使用successRedirect註冊的頁面,我該怎麼做? 我對與谷歌認證護照的策略是,像這樣

passport.use(new GoogleStrategy({ 
    clientID  : configAuth.googleAuth.clientID, 
    clientSecret : configAuth.googleAuth.clientSecret, 
    callbackURL  : configAuth.googleAuth.callbackURL, 
    passReqToCallback : true // allows us to pass in the req from our route (lets us check if a user is logged in or not) 

}, 
function(req, token, refreshToken, profile, done) { 
    console.log("Its here "); 
    // asynchronous 
    process.nextTick(function() { 

     // check if the user is already logged in 
     if (!req.user) { 

      User.findOne({ 'UserName' : req.body.email }, function(err, user) { 
       if (err) 
        return done(err); 

       if (user) { 

        // if there is a user id already but no token (user was linked at one point and then removed) 
        if (!user.google.token) { 
         user.google.token = token; 
         user.google.name = profile.displayName; 
         user.google.email = (profile.emails[0].value || '').toLowerCase(); // pull the first email 

         user.save(function(err) { 
          if (err) 
           return done(err); 

          return done(null, user); 
         }); 
        } 

        return done(null, user); 
       } else { 
        var newUser   = new User(); 
        newUser.UserName= (profile.emails[0].value || '').toLowerCase(); 
        newUser.google.id = profile.id; 
        newUser.google.token = token; 
        newUser.google.name = profile.displayName; 
        newUser.google.email = (profile.emails[0].value || '').toLowerCase(); // pull the first email 

        newUser.save(function(err) { 
         if (err) 
          return done(err); 

         return done(null, newUser); 
        }); 
       } 
      }); 

     } else { 
      // user already exists and is logged in, we have to link accounts 
      var user    = req.user; // pull the user out of the session 

      user.google.id = profile.id; 
      user.google.token = token; 
      user.google.name = profile.displayName; 
      user.google.email = (profile.emails[0].value || '').toLowerCase(); // pull the first email 

      user.save(function(err) { 
       if (err) 
        return done(err); 

       return done(null, user); 
      }); 

     } 

    }); 

})); 

回答

0

林不知道你使用前結束,但如果它與終極版反應,如果你有SSR可以通過射擊動作與req.user信息preloadState從你的服務器。 Here's documentation on that.

或者如果您有會話的設置,您可以在您的前端加載並響應req.user信息時發出獲取請求。

相關問題