Chapter 23 - How to use Firebase Social Login Authentication in Laravel Socialite Package

In this chapter, we will learn how to connect Laravel Socialite package to Firebase Authentication Social Login Provider. How to enable social option in Firebase Authentication Console and insert credentials of app and handling the callback from Social Login Page. 

Before we start, we assume you have all knowledge of setting up the Firebase Authentication and Laravel Socialite package integration. If not please read this Chapter 15 for Firebase Authentication and Laravel Socialite.

From Firebase Authentication Console.


To Laravel Login with Social Options


The very first step to enable all three social login providers from Firebase Authentication Panel. Simply click on sign in providers which you want, for example we have clicked on Facebook. Then click on enable.


Here we assume, you know all the steps of creating app in Facebook, Google, Twitter and obtain their app id and secret. Just enter app id and secret and click on save. So now your Facebook is enabled.

Now you have to integrate Socialite as usual you implement in a Normal Laravel project. After successfully installation, in LoginController, following is the handleProviderCallback function. Below is the function to give you a idea how it looks before we make changes of Firebase.

 public function handleProviderCallback(Request $request, $social)  
   {  
     if ($social == 'facebook' && (!$request->has('code') || $request->has('error'))) {  
       return redirect('/login');  
     } else if ($social == 'twitter' && $request->has('denied')) {  
       return redirect('/login');  
     }  
     $socialize_user = Socialite::driver($social)->user();  
     $social_user_id = $socialize_user->getId(); // unique facebook user id  
     switch ($social) {  
       case 'facebook':  
         $user = User::where('facebook_user_id', $social_user_id)->first();  
         break;  
       case 'google':  
         $user = User::where('google_user_id', $social_user_id)->first();  
         break;  
       case 'twitter':  
         $user = User::where('twitter_user_id', $social_user_id)->first();  
         break;  
       default:  
         # code...  
         break;  
     }  
     // register (if no user)  
     if (!$user) {  
       //Check if email exists in our system  
       $email = User::where('email', $socialize_user->getEmail())->first();  
       if(!$email) {  
         $user = new User;  
       } else {  
         $user = $email;  
       }    
       switch ($social) {  
         case 'facebook':  
           $user->facebook_user_id = $social_user_id;  
           break;  
         case 'google':  
           $user->google_user_id = $social_user_id;  
           break;  
         case 'twitter':  
           $user->twitter_user_id = $social_user_id;  
           break;  
         default:  
           # code...  
           break;  
       }  
     }  
     $user->email = $socialize_user->getEmail();  
     $user->name = $socialize_user->getName();  
     $user->password = md5(rand(1,10000));  
     $user->facebook_user_pic = $socialize_user->getAvatar();  
     $user->save();  
     // login  
     Auth::loginUsingId($user->id);  
     return redirect($this->redirectTo);  
   }  


Now the modification of handleProviderCallback function according to Firebase 

 public function handleProviderCallback(Request $request, $social)  
   {  
     if ($social == 'facebook' && (!$request->has('code') || $request->has('error'))) {  
       return redirect('/login');  
     } else if ($social == 'twitter' && $request->has('denied')) {  
       return redirect('/login');  
     }  
     $socialize_user = Socialite::driver($social)->user();  
     switch ($social) {  
       case 'facebook':  
         $idprovider = 'facebook.com';  
         break;  
       case 'google':  
         $idprovider = 'google.com';  
         break;  
       case 'twitter':  
         $idprovider = 'twitter.com';  
         break;  
       default:  
         # code...  
         break;  
     }  
     try {   
       $providerData = app('firebase.auth')->linkProviderThroughAccessToken($idprovider, $socialize_user->token);   
       //Credentials are correct   
       $logged_user = $providerData ->userRecord;  
     } catch (\Exception $ex) {   
       return back()->withErrors(['Credentials are incorrect']);   
     }   
   }  


Done. Then refer Chapter 15 to maintain a logged in session.

Here 'facebook.com' is provider identifier and below the list of providers of package

 const ANONYMOUS = 'anonymous';  
   const CUSTOM = 'custom';  
   const FACEBOOK = 'facebook.com';  
   const FIREBASE = 'firebase';  
   const GITHUB = 'github.com';  
   const GOOGLE = 'google.com';  
   const PASSWORD = 'password';  
   const PHONE = 'phone';  
   const TWITTER = 'twitter.com';  


And so rest of Google and twitter will be similar code. So hope you are clear regarding the topic. Thanks for reading. Please let us know your doubts in the comment section.

Post a Comment

1 Comments