Example #1
1
 public function connect_with_oauth2($provider_name)
 {
     if ($this->user_model->is_logged_in()) {
         redirect('user/login');
     }
     switch ($provider_name) {
         case 'facebook':
             $provider = new League\OAuth2\Client\Provider\Facebook(array('clientId' => '738761806197904', 'clientSecret' => '913fab2aabe36d3af31dc738e3964d69', 'redirectUri' => 'http://kuklos.vikom.io/user/connect/facebook', 'scopes' => array('email')));
             break;
         case 'github':
             $provider = new League\OAuth2\Client\Provider\Github(array('clientId' => 'e100df6b5305c00f58e3', 'clientSecret' => 'b27953bc5421ce1e109e2b49c7fbb170de51108c', 'redirectUri' => 'http://kuklos.vikom.io/user/connect/github', 'scopes' => array('email')));
             break;
         case 'google':
             $provider = new League\OAuth2\Client\Provider\Google(array('clientId' => '246372104087-gf5re27h5ds69p09ubs25qmlf4bh3oim.apps.googleusercontent.com', 'clientSecret' => '4ccpNZwabW817I6jhN1n8TdB', 'redirectUri' => 'http://kuklos.vikom.io/user/connect/google', 'scopes' => array('email')));
             break;
         default:
             exit('Unsupported OAuth2 Provider: ' . $provider_name);
     }
     if (!isset($_GET['code'])) {
         // If we don't have an authorization code then get one
         header('Location: ' . $provider->getAuthorizationUrl());
         exit;
     } else {
         // Try to get an access token (using the authorization code grant)
         $token = $provider->getAccessToken('Authorization_Code', ['code' => $_GET['code']]);
         // Get user email
         $email = '';
         try {
             // We got an access token, let's now get the user's details
             $userDetails = $provider->getUserDetails($token);
             // Use these details to create a new profile
             $email = $userDetails->email;
         } catch (Exception $e) {
             // Failed to get user details
             exit('Something went wrong. Contact Admin.');
         }
         // Create account if it does not exist
         if ($this->user_model->add_oauth_user($email)) {
             $this->send_welcome_email($email);
         }
         $this->set_login_userdata($email);
         redirect('user');
         // // Use this to interact with an API on the users behalf
         // echo $token->accessToken;
         // // Use this to get a new access token if the old one expires
         // echo $token->refreshToken;
         // // Number of seconds until the access token will expire, and need refreshing
         // echo $token->expires;
     }
 }