public function deleteGoogleProfile($id) { $token = GoogleUser::getTokenById($id); GoogleUser::destroy($id); Tokens::revokeToken($token); return response()->json(['msg' => 'User disconnected'], 200); }
/** * 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'); }
/** * @param $url * @param null $user_id * @param null $token * @return json */ public static function GET($url, $user_id = null, $token = null) { try { $ch = curl_init(); if (FALSE === $ch) { throw new Exception('failed to initialize'); } curl_setopt($ch, CURLOPT_URL, $url); curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1); curl_setopt($ch, CURLOPT_IPRESOLVE, CURL_IPRESOLVE_V4); curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, 0); curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true); if ($token != null) { curl_setopt($ch, CURLOPT_HTTPHEADER, array('Content-Type: application/json', "Authorization: Bearer " . $token)); } $content = curl_exec($ch); if (FALSE === $content) { throw new Exception(curl_error($ch), curl_errno($ch)); } } catch (Exception $e) { trigger_error(sprintf('Curl failed with error #%d: %s', $e->getCode(), $e->getMessage()), E_USER_ERROR); } $json = json_decode($content); if (isset($json->error)) { if ($json->error->code == 401) { $new_access_token = Tokens::refreshToken($user_id); $query = parse_url($url, PHP_URL_QUERY); parse_str($query, $val); $val['access_token'] = $new_access_token; $fixed_query = http_build_query($val); $_url = parse_url($url); $_url['query'] = $fixed_query; $new_url = file_get_contents(Common::http_build_url($url, $_url)); $json = json_decode($new_url); } else { abort(500, $json->error->message); } } return $json; }