/** * Obtain the user information from Google. * * @param Request $request * @return Socialite ->user() */ public function handleGoogleProviderCallback(Request $request) { if ($request->get('error') == "access_denied") { return redirect('/'); } $authorizationCode = $request->get('code'); $googleTokens = Tokens::getGoogleTokens($authorizationCode); //return var_dump($googleTokens); $user = Socialite::driver('google')->getUserByToken($googleTokens->access_token); $email = $user['emails'][0]['value']; $name = $user['name']['givenName'] . " " . $user['name']['familyName']; $avatar = $user['image']['url']; if (!GoogleUser::existsByEmailAndId($email, Auth::user()->id) && isset($googleTokens->refresh_token)) { GoogleUser::create(['user_id' => Auth::user()->id, 'email' => $email, 'names' => $name, 'avatar' => $avatar, 'googleAccessToken' => $googleTokens->access_token, 'googleRefreshToken' => $googleTokens->refresh_token, 'uriCode' => $authorizationCode, 'expireValue' => $googleTokens->expires_in]); return redirect('/availability/google')->with(['message' => 'You have linked this profile (' . $email . ')']); } elseif (GoogleUser::existsByEmailAndId($email, Auth::user()->id) && isset($googleTokens->refresh_token)) { $id = GoogleUser::where('email', '=', $email)->where('user_id', '=', Auth::user()->id)->select('id')->first()->pluck('id')['id']; GoogleUser::updateTokens($id, ['googleAccessToken' => $googleTokens->access_token, 'googleRefreshToken' => $googleTokens->refresh_token, 'uriCode' => $authorizationCode, 'expireValue' => $googleTokens->expires_in]); return redirect('/availability/google')->with(['message' => 'You already have this profile (' . $email . '). Data is updated.']); } elseif (GoogleUser::existsByEmailAndId($email, Auth::user()->id) && !isset($googleTokens->refresh_token)) { return redirect('/availability/google')->with(['message' => 'You already have this profile (' . $email . ')']); } elseif (!GoogleUser::existsByEmailAndId($email, Auth::user()->id) && !isset($googleTokens->refresh_token)) { return redirect('/availability/google')->withErrors(['Missing refresh token. Contact the administrator.']); } else { return var_dump($authorizationCode, $googleTokens, $user); abort(500, "Auth error. Contact the admin"); } return redirect('/availability/google'); }