How to implement Apple Sign In (Flutter) with a REST API backend (Laravel)?

142 Views Asked by At

I'm trying to implement SIGN IN WITH APPLE in my application (flutter app), and I've already managed to get the Authorization Code and Identity Token, but I got stuck on it and I don't know how to proceed anymore. I have to send these codes to my server and get user data from it, right? How do I proceed here, does anyone know?

I have used the following tutorial to complete the flow Implementing Sign-in with Apple on Flutter (iOS and Android) and the Web using a Laravel backend

I have to use only the sign-in with Apple only for ios not for Android and web side.

What I have currently done is:

public function loginWithApple(Request $request, AppleToken $appleToken)
{
    $request->validate([
        'code' => ['required'],
        'first_name' => ['nullable', "string"],
        'last_name' => ['nullable', "string"],
        'use_bundle_id' => ['required'],
    ]);
 
    config()->set('services.apple.client_secret', $appleToken->generateClientSecret(useBundleId: true));
    config()->set('services.apple.client_id', env('APPLE_APP_CLIENT_ID'));
    config()->set('services.apple.redirect', env('APPLE_APP_REDIRECT'));


    /** @var \Laravel\Socialite\Two\User $socialiteUser */
    $socialiteUser = Socialite::driver("apple")->stateless()->user();

    $user = User::firstOrCreate([
        "provider" => "apple",
        "provider_id" => $socialiteUser->getId(),
    ], [

        'name' => $socialiteUser->getName() ?? "",
        'email' => $socialiteUser->getEmail(),
        'provider_id' => $socialiteUser->getId(),
        'provider' => "apple",
    ]);;

    $token = $user->createToken($request->input("device_name"));

    return [
        "token" => $token->plainTextToken,
    ];
}

but I am facing an error with the invalid client and I don't know why it is occurring.

I have tested that from the Apple side when the user presses log in with Apple my callback URL is called and this function runs. but it gives an error on the line:

/** @var \Laravel\Socialite\Two\User $socialiteUser */
$socialiteUser = Socialite::driver("apple")->stateless()->user();

So, someone can give me a clue about where I am doing wrong because I am generating the secret key on the fly on every new request?

I am using the following Laravel package for this:

  • Laravel\Socialite\Facades\Socialite
0

There are 0 best solutions below