/**
  * Login with Google.
  */
 public function google(Request $request)
 {
     $accessTokenUrl = 'https://accounts.google.com/o/oauth2/token';
     $peopleApiUrl = 'https://www.googleapis.com/plus/v1/people/me/openIdConnect';
     $params = ['code' => $request->input('code'), 'client_id' => $request->input('clientId'), 'client_secret' => Config::get('auth.google.client_secret'), 'redirect_uri' => $request->input('redirectUri'), 'grant_type' => 'authorization_code'];
     $client = new GuzzleHttp\Client();
     // Step 1. Exchange authorization code for access token.
     $accessTokenResponse = $client->post($accessTokenUrl, ['form_params' => $params]);
     $accessToken = json_decode($accessTokenResponse->getBody())->access_token;
     $headers = array('Authorization' => 'Bearer ' . $accessToken);
     // Step 2. Retrieve profile information about the current user.
     $profileResponse = $client->get($peopleApiUrl, ['headers' => $headers]);
     $profile = json_decode($profileResponse->getBody());
     // Step 3a. If user is already signed in then link accounts.
     if ($request->header('Authorization')) {
         $user = User::where('google', '=', $profile->sub)->where('email', '=', $profile->email);
         if ($user->first()) {
             return response()->json(['message' => 'There is already a Google account that belongs to you'], 409);
         }
         $token = explode(' ', $request->header('Authorization'))[1];
         $payload = (array) JWT::decode($token, Config::get('app.token_secret'), array('HS256'));
         $user = User::find($payload['sub']);
         $user->google = $profile->sub;
         $user->name = $user->name ?: $profile->name;
         $user->email = $profile->email;
         $user->save();
         return response()->json(['token' => $this->createToken($user), 'user' => $user->toArray()]);
     } else {
         $user = User::where('google', '=', $profile->sub)->where('email', '=', $profile->email);
         if ($user->first()) {
             return response()->json(['token' => $this->createToken($user->first()), 'user' => $user->first()->toArray()]);
         }
         $user = new User();
         $user->google = $profile->sub;
         $user->name = $profile->name;
         $user->email = $profile->email;
         $user->save();
         return response()->json(['token' => $this->createToken($user), 'user' => $user->toArray()]);
     }
 }