googleOAuth.js 1.1 KB

12345678910111213141516171819202122232425262728293031323334353637
  1. const GOOGLE_OAUTH_TOKEN_URI = "https://www.googleapis.com/oauth2/v4/token"
  2. ,GOOGLE_USERINFO_URI = "https://www.googleapis.com/oauth2/v1/userinfo";
  3. const googleConfig = require('../config.js').login.google
  4. ,httpsRequest = require('./httpsRequest.js').httpsRequest
  5. ,httpsPost = require('./httpsRequest.js').httpsPost
  6. ;
  7. function getUserId(code, cb) {
  8. httpsPost(GOOGLE_OAUTH_TOKEN_URI, {
  9. "client_id": googleConfig.clientId
  10. ,"client_secret": googleConfig.clientSecret
  11. ,"redirect_uri": googleConfig.redirect_uri
  12. ,"grant_type": "authorization_code"
  13. ,"code": code
  14. },
  15. (status, resp) => {
  16. if (status === 200 && resp && resp.access_token) {
  17. httpsRequest(GOOGLE_USERINFO_URI +"?access_token="+resp.access_token,
  18. (status, resp) => {
  19. if (status === 200 && resp && resp.id) {
  20. cb(resp.id);
  21. } else {
  22. cb(null);
  23. }
  24. });
  25. } else {
  26. cb(null);
  27. }
  28. });
  29. }
  30. module.exports.GoogleOAuth = {
  31. getUserId: getUserId
  32. };