I need to add social login to a laravel API application project. I have successfully implemented the social login using socialite with the set up below
web.php
Auth::routes(['verify' => true]);
Route::get('/auth/{provider}', 'SocialAuthController@redirectToProvider');
Route::get('/auth/{provider}/callback', 'SocialAuthController@handleProviderCallback');
SocialauthController methods are
public function redirectToProvider($provider)
{
return Socialite::driver($provider)->redirect();
}
public function handleProviderCallback($provider)
{
// retrieve social user info
$socialUser = Socialite::driver($provider)->stateless()->user();
// check if social user provider record is stored
$userSocialAccount = SocialAccount::where('provider_id', $socialUser->id)->where('provider_name', $provider)->first();
if ($userSocialAccount) {
// retrieve the user from users store
$user = User::find($userSocialAccount->user_id);
// assign access token to user
$token = $user->createToken('******')->accessToken;
// return access token & user data
return response()->json([...]);
} else {
// store the new user record
$user = User::create([...]);
// store user social provider info
if ($user) {
SocialAccount::create([...]);
}
// assign passport token to user
$token = $user->createToken('******')->accessToken;
return response()->json([...]);
}
}
When the react app consuming the api makes a call to https://app-domain-name/auth/google the user is redirected to google and the user is successfully authenticated. But the response is being returned to the browser window where the user was redirected to from the app.
How do I redirect the user back to the app with the response? I need to do this so that the granted access token can be used to access app resources.