示例#1
0
 public function updateFromGoogle($id)
 {
     $user = User::find($id);
     if ($user) {
         if ($user->updateFromGoogleAccount() === false) {
             return Redirect::to(GoogleSessionController::generateGoogleLoginURL(['approval_promt' => 'force', 'state' => 'updated_profile']));
         }
     }
     return Redirect::back()->with(['status' => 'Profile updated from google']);
 }
示例#2
0
文件: User.php 项目: vanderlin/halp
 public function updateFromGoogleAccount()
 {
     $client = GoogleSessionController::getClient();
     $oauth2 = new \Google_Auth_OAuth2($client);
     if ($this->google_token && $oauth2->isAccessTokenExpired() == false) {
         $oauth2->refreshToken($this->google_token);
         $client->setAccessToken($oauth2->getAccessToken());
         $oauth2 = new \Google_Service_Oauth2($client);
         $google_user = $oauth2->userinfo->get();
         // update the photo
         GoogleSessionController::saveGoogleProfileImage($google_user, $this);
         // other things later..
         return true;
     }
     return false;
 }
 public function signin()
 {
     $wantsJson = Request::wantsJson();
     $creds = GoogleSessionController::getCreds();
     $client = GoogleSessionController::getClient();
     $code = Input::get('code');
     if ($code) {
         // Exchange the OAuth 2.0 authorization code for user credentials.
         $client->authenticate($code);
         $token = json_decode($client->getAccessToken());
         $attributes = $client->verifyIdToken($token->id_token, $creds->client_id)->getAttributes();
         $oauth2 = new \Google_Service_Oauth2($client);
         $google_user = $oauth2->userinfo->get();
         $email = $google_user->email;
         $username = explode("@", $email)[0];
         if ($google_user) {
             $u = GoogleSessionController::findUserFromGooglePerson($google_user);
             $refreshToken = $client->getRefreshToken();
             // we have a user with this google+ account
             if ($u != null) {
                 // if(Input::has('state') && Input::get('state') == 'updated_profile') {
                 $this->saveGoogleProfileImage($google_user, $u);
                 // }
                 if (empty($u->google_token) && isset($refreshToken)) {
                     $u->google_token = $refreshToken;
                     $u->save();
                 }
                 if (empty($u->google_id)) {
                     $u->google_id = $google_user->id;
                     $u->save();
                 }
                 if (empty($u->firstname) || empty($u->lastname)) {
                     $u->firstname = $google_user->givenName;
                     $u->lastname = $google_user->familyName;
                     $u->save();
                 }
                 Auth::login($u, true);
                 $resp = ['notice' => 'Welcome ' . $u->username];
                 return $wantsJson ? Response::json($resp) : Redirect::intended();
             } else {
                 if ($client->getRefreshToken() == NULL) {
                     $url = GoogleSessionController::generateGoogleLoginURL(['approval_prompt' => 'force', 'state' => 'refresh_token']);
                     return Redirect::to($url);
                 }
                 return $this->createAccountFromGoogleAccount($google_user, $client->getRefreshToken());
             }
             return $wantsJson ? Response::json(['error' => 'No user found with that id']) : Redirect::to('/')->with(['error' => 'No user found with that id']);
         }
         $errors = ['error' => $email . ' is not registered with ' . Config::get('config.site-name')];
         return $wantsJson ? Response::json($errors) : Redirect::to('/')->with($errors);
     }
     return $wantsJson ? Response::json(['error' => 'Missing OAuth Code']) : Redirect::to('login')->with(['error' => 'Missing OAuth Code']);
 }