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.
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);
}
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']);
}
}
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.
1 Comments
Let us know your doubts here.
ReplyDelete