Social login with google/facebook via Laravel API

Social login with google/facebook via Laravel API

public function handleProviderCallback(Request $request, $provider){
    $loginService = new LoginService(); // Don't forget to import the LoginService.php at top
     if($loginService->isLoginWithGoogle($provider)) {
           $response = Http::get("https://www.googleapis.com/oauth2/v3/tokeninfo?id_token={$accessToken}");

      if (!$response->successful()) {
             throw new \Exception($response->body(), requestResponse()::UNAUTHORIZED_ACTION);
                }

          $socialProvider = json_decode($response->body(), true);
               // Your Logic Here
      }

      // Facebook
      if($loginService->isLoginWithFacebook($provider)) {
          $response = Http::get("https://graph.facebook.com/v21.0/me?fields=id,name,email&access_token={$accessToken}");

          // When it's failed to successful
          if (!$response->successful()) {
               throw new \Exception($response->body(), requestResponse()::UNAUTHORIZED_ACTION);
           }

           $socialProvider = json_decode($response->body(), true);
           // Your Logic Here
     }
 } // handleProviderCallback method closing

  // LoginService.php
  public function isLoginWithGoogle($data){
     return $data == "google"; // returning true/false
  }

  public function isLoginWithFacebook($data){
     return $data == "facebook"; // returning true/false
  }

Important Note: $provider is a dynamic parameter which is receiving "facebook","google", "twitter","LinkedIn" etc from the URL.

Note: Always keep your eyes on official documentation.

Source: https://stackoverflow.com/a/79286767/13809654