You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

61 lines
2.3 KiB

7 years ago
  1. <?php
  2. defined('BASEPATH') OR exit('No direct script access allowed');
  3. require_once APPPATH . "libraries/Social_login.php";
  4. class Social_login_google extends Social_login {
  5. function __construct()
  6. {
  7. parent::__construct();
  8. $CI=&get_instance();
  9. $this->social_setting->client_id = $CI->site->config('social_google_clientid');
  10. $this->social_setting->client_secret = $CI->site->config('social_google_clientsecret');
  11. $this->social_setting->redirect_url = base_url("members/social-login/google");
  12. $this->social_setting->authorize_url = "https://accounts.google.com/o/oauth2/auth";
  13. $this->social_setting->token_url = "https://www.googleapis.com/oauth2/v4/token";
  14. $this->social_setting->info_url = "https://www.googleapis.com/plus/v1/people/me";
  15. $this->social_setting->token_request_post = TRUE;
  16. }
  17. protected function _get_authorize_param() {
  18. $scope_array = array(
  19. "https://www.googleapis.com/auth/plus.login",
  20. "https://www.googleapis.com/auth/userinfo.email",
  21. "https://www.googleapis.com/auth/userinfo.profile",
  22. "https://www.googleapis.com/auth/plus.me",
  23. "https://www.googleapis.com/auth/plus.me"
  24. );
  25. $param = parent::_get_authorize_param();
  26. $param['access_type'] = "offline";
  27. $param['scope'] = implode(" ", $scope_array);
  28. return $param;
  29. }
  30. protected function _get_info($access_token, $add_param="")
  31. {
  32. $result = json_decode(parent::_get_info($access_token, $add_param), TRUE);
  33. if( $result['id'] ) {
  34. return $result;
  35. }
  36. else {
  37. return NULL;
  38. }
  39. }
  40. protected function _generate_profile($profile)
  41. {
  42. $return['provider'] = "google";
  43. $return['id'] = $profile['id'];
  44. $return['name'] = $profile['displayName'];
  45. $return['profile'] = $profile['image']['url'];
  46. $return['email'] = $profile['emails'][0]['value'];
  47. $return['gender'] = $profile['gender'] == 'male' ? 'M' : ( $profile['gender'] == 'female' ? 'F' : 'U' );
  48. $return['extra'] = json_encode($profile, JSON_UNESCAPED_UNICODE|JSON_UNESCAPED_SLASHES);
  49. return $return;
  50. }
  51. }