@deekshith I am not sure why you are using manual checking all these stuff, but you can achieve these by using Laravel Socialite package.
You will get tons of tutorials on it. Just google it.
Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.
In one of the application i have used facebook JavaScript SDK for login purpose and also used google signin sdk too. i have used below approach when user first click Facebook login it will check for email exists or not if exists it will login if not then it will create new account and redirects.
If again user tries to login using google with same email address then i am allowing user to login to his previous account and i am just collecting the AUTH token and i am not comparing while logging in.
check the Facebook controller logic below,
///////////////////////////////////////
// prep Facebook verification
///////////////////////////////////////
// sanitize login data
$facebook_access_token = filter_var($accesstoken, FILTER_SANITIZE_STRING);
// set variables
$facebook_user_access_token = $facebook_access_token;
$my_facebook_app_id = config('constants.fb.facebook_app_id');
$my_facebook_app_secret = config('constants.fb.facebook_secret_key');
$facebook_application = 'My Portal'; // in my case 'domain.com', as set up in Facebook
///////////////////////////////////////
// get facebook access token
///////////////////////////////////////
$curl_facebook1 = curl_init(); // start curl
$url = "https://graph.facebook.com/oauth/access_token?client_id=".$my_facebook_app_id."&client_secret=".$my_facebook_app_secret."&grant_type=client_credentials"; // set url and parameters
curl_setopt($curl_facebook1, CURLOPT_URL, $url); // set the url variable to curl
curl_setopt($curl_facebook1, CURLOPT_RETURNTRANSFER, true); // return output as string
$output = curl_exec($curl_facebook1); // execute curl call
curl_close($curl_facebook1); // close curl
$decode_output = json_decode($output, true); // decode the response (without true this will crash)
// store access_token
$facebook_access_token = $decode_output['access_token'];
///////////////////////////////////////
// verify my access was legitimate
///////////////////////////////////////
$curl_facebook2 = curl_init(); // start curl
$url = "https://graph.facebook.com/debug_token?input_token=".$facebook_user_access_token."&access_token=".$facebook_access_token; // set url and parameters
curl_setopt($curl_facebook2, CURLOPT_URL, $url); // set the url variable to curl
curl_setopt($curl_facebook2, CURLOPT_RETURNTRANSFER, true); // return output as string
$output2 = curl_exec($curl_facebook2); // execute curl call
curl_close($curl_facebook2); // close curl
$decode_output2 = json_decode($output2, true); // decode the response (without true this will crash)
// test browser and Facebook variables match for security
if ($my_facebook_app_id == $decode_output2['data']['app_id'] && $decode_output2['data']['application'] == $facebook_application && $decode_output2['data']['is_valid'] == true) {
//success
$check_for_email = User::where('email',$email)->first();
//if email exists then login or create new account
} else {
//show email not found and error message
}
is this okay if i match only email and allow both google and facebook signin if they have same email address linked? i am using below condition if facebook/google login is success
$check_for_email = User::where('email',$email)->first();
Please or to participate in this conversation.