I am creating a multi vendor web application in which I need to implement login with Facebook
for customers. vendor can have their own domain or sub-domain and also own customers. In the website, I have implemented Facebook login by using env variables. the code is below.
Routes for facebook login
Route::get('auth/facebook', 'FacebookController@redirectToFacebook');
Route::get('auth/facebook/callback', 'FacebookController@handleFacebookCallback');
In config/services.php
'facebook' => [
'client_id' => env('FACEBOOK_CLIENT_ID'),
'client_secret' => env('FACEBOOK_CLIENT_SECRET'),
'redirect' => env('FACEBOOK_CALLBACK_URL'),
],
Facebook Controller function
public function redirectToFacebook()
{
return Socialite::driver('facebook')->redirect();
}
public function handleFacebookCallback()
{
try {
$user = Socialite::driver('facebook')->user();
$user_name = $user->getName();
$user_email = $user->getEmail();
$facebook_auth_id = $user->getId();
/* other code */
} catch (Exception $e) {
return redirect()->route('login')->with(['error' => "facebook login failed."]);
}
}
but I want to use vendor's Facebook key and secret which stored in database. How can I use dynamic variables here to implement the functionality?
Make sure these packages should be included in your Controller
use Laravel\Socialite\SocialiteManager;
use Socialite;
use Laravel\Socialite\Two\FacebookProvider;
Use below code in your controller
public function redirectToFacebook(Request $request)
{
$fb = $this->configDriver($request);
return $fb->redirect();
}
private function configDriver(Request $request, $domain = '', $driver = 'facebook')
{
$config['client_id'] = 'your_client_id';
$config['client_secret'] = 'your_client_secret';
$config['redirect'] = 'your_redirect_url';
return Socialite::buildProvider(FacebookProvider::class, $config);
}
public function handleSocialCallback(Request $request, $domain = '', $driver = 'facebook')
{
$usr = $this->configDriver($request)->stateless()->user();
$name = $user->getName();
$email = $user->getEmail();
$facebook_id = $user->getId();
/*save data in db*/
}