Laravel Socialite for Mobile APPs using APIs

1.3k Views Asked by At

I'm using Laravel Socialite package for social media (Facebook, Gmail) authentication, creating account and login on website and work fine.

Now I'm in need of providing same feature on mobile devices but I'm not getting any helpful lead so far about how and what needs to be done in this case.

I've users table which store user's information and social_providers table which store user_id and provider_id returned by the socialite using below code.

public function handleProviderCallback(Request $request, $provider = null) {

    try {
        $socialUser = Socialite::driver($provider)->user();
    } catch (\Exception $e) {
        return redirect('/');
    }
    //check if we have logged provider
    $socialProvider = SocialProvider::where('provider_id', $socialUser->getId())->first();
    if (!$socialProvider) {
        $name = explode(' ', $socialUser->getName(), 2);
        $first_name = $name[0];
        $last_name = $name[1];
        //create a new user and provider
        $user = User::firstOrCreate(
                        ['email' => $socialUser->getEmail()], ['last_name' => $last_name,
                    'first_name' => $first_name,
                    'email_verified_at' => date('Y-m-d H:i:s')]
        );
        $user->socialProviders()->create(
                ['provider_id' => $socialUser->getId(), 'provider' => $provider]
        );
    } else {
        $user = $socialProvider->user;
    }

    auth()->login($user);

    return redirect('/');
}

Now in case of mobile, what needs to be done. Do I've to user android SDK on mobile and that will send user info and provider_id to backend? OR something needs to be done at backend Laravel side? Thanks if someone can guide me in basic way that what needs to be done and on which side (mobile or backend).

1

There are 1 best solutions below

0
On

for the mobile app, you would need to use the Mobile SDK to get the access token and in the backend, you would need to use the access token to get user info like the following

$provider_user = Socialite::driver($provider)->userFromToken($request->access_token);