簡體   English   中英

帶有passportjs + Vue的Google oAuth

[英]Google oAuth with passportjs + Vue

我目前堅持在連接到我自己的 node express api 服務器的 vue 應用程序中處理 google oAuth 登錄。

在express api服務器上,我使用passport作為中間件來處理google oauth,在通過google成功登錄后,我在后端的回調中生成了一個jwt。

passport.use(new GoogleStrategy({
    clientID: config.get('google.clientID'),
    clientSecret: config.get('google.clientSecret'),
    callbackURL: config.get('google.callbackUrl'),
  },
  function(accessToken, refreshToken, profile, done) {
    User.findOne(
      { socialID: profile.id },
      function (err, user) {
        if (err) {
            return done(err);
        }
        //No user was found... so create a new user with values from Facebook (all the profile. stuff)
        if (!user) {
          user = new User({
            name: profile.displayName,
            email: profile.emails[0].value,
            provider: profile.provider,
            socialID: profile.id,
          });
          user.save(function(err) {
            if (err) console.log(err);
          });
        }

        // the information which shall be inside the jsonwebtoken
        const payload = {
          user: {
            id: user.id
          }
        };

        // create jsonwebtoken and return it
        jwt.sign(
          payload,
          config.get('jwt.secret'), // get the secret from default.json to hash jsonwebtoken
          { expiresIn: config.get('jwt.lifetime') },
          (err, token) => {
            if(err) throw err; // if there is error, throw it and exit
            return done(JSON.stringify(token)); // return jwt token
          }
        );
      }
    );
  }
));

我的 api 服務器上有這些路由

// @route   GET api/auth/google
// @desc    Google auth route - get User From Google, store it if not exists yet
// @access  Public
router.get('/google',
  passport.authenticate('google', { scope: ['profile', 'email'], session: false })
);

// @route   GET api/auth/google/callback
// @desc    Google callback route
// @access  Public
router.get('/google/callback',
  passport.authenticate('google', { failureRedirect: '/', session: false }),
  function (req, res) {
    res.redirect('http://localhost:8080/?token=' + res);
  }
);

當我在 /auth/google 調用我的后端 api 路由時,我成功地重定向到了 google 登錄頁面。 但是通過我的方法,我嘗試使用 get 參數“token”從回調 url 重定向回我的 vue 應用程序,以在前端接收令牌。 我的后端回調路由中的重定向不起作用。 我如何將后端生成的令牌傳遞給我的前端?

我發現重定向不起作用,因為 return done() 函數需要兩個參數才能正常工作。

我在谷歌護照中間件內部改變了這樣的完成功能

jwt.sign(
 payload,
 config.get('jwt.secret'), // get the secret from default.json to hash jsonwebtoken
 { expiresIn: config.get('jwt.lifetime') },
  (err, token) => {
    if(err) throw err; // if there is error, throw it and exit
    return done(null, token); // return jwt token
  }
);

現在在我的路線中,我可以成功地重定向 + 添加令牌作為獲取參數 - 所以通過這個解決方法,我收到了我的 jwt,它是在我前端的后端生成的。

// @route   GET api/auth/google/callback
// @desc    Google callback route
// @access  Public
router.get('/google/callback',
  passport.authenticate('google', { failureRedirect: '/', session: false }),
  function (req, res) {
    let token = res.req.user;
    res.redirect('//localhost:8080/?token=' + token);
  }
);

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM