DEV Community

Al-Amin Islam
Al-Amin Islam

Posted on • Edited on

What is the process to Sign In with Apple with laravel?

The Sign In with Apple Flow . How Sign In with Apple Works (Hint: it uses OAuth and OIDC) .
Today I will try to show how to login with apple.

No.1

Mandatory Check this url If you click on this URL, you can see how to create Apple's service.
Create Apple Service on Apple developer Account

No.2
You need to install laravel socialite package on your project



composer require laravel/socialite


Enter fullscreen mode Exit fullscreen mode

Then you will need to install apple socialiteproviders . Check this url Apple SocialiteProviders.

You need some configuration on your laravel projects

config/services.php
thats have provide . Apple SocialiteProviders.

Note
If it doesn't work you just need to add some others field which will get you from apple service developer account.



'apple' => [
    'client_id' => env('APPLE_CLIENT_ID'),
    'client_secret' => env('APPLE_CLIENT_SECRET'),
    'team_id' => env('APPLE_TEAM_ID'),
    'key_id' => env('APPLE_KEY_ID'),
    'private_key' => str_replace('\\n', "\n", env('APPLE_PRIVATE_KEY')),
    'redirect' => env('APPLE_REDIRECT_URL'),
],


Enter fullscreen mode Exit fullscreen mode

your laravel .env file looks like



APPLE_CLIENT_ID='xxxxxxxxx'
APPLE_PRIVATE_KEY="-----BEGIN PRIVATE KEY-----\n token \n-----END PRIVATE KEY-----"

APPLE_TEAM_ID="xxxxxx"
APPLE_KEY_ID="xxxxxxxx"
APPLE_CLIENT_SECRET="xxxxxxxxxx"

APPLE_REDIRECT_URL="http://127.0.0.1:8000/auth/login/apple/callback"
APPLE_REDIRECT_URI="http://127.0.0.1:8000/auth/login/apple/callback"


Enter fullscreen mode Exit fullscreen mode

..........

No. 3
Now you need to create a Apple secret which is very important for Apple login. For creating Apple secret you can following this website . Generate Apple token . For generate JWT token you will need to install this package .
composer require lcobucci/jwt

Now you are ready for creating route in socialite login.



Route::get('login/apple',[SocialController::class,'redirectToProvider'])->name('redirectToProvider');
Route::post('login/apple/callback',[SocialController::class,'handleProviderCallback'])->name('handleProviderCallback');


Enter fullscreen mode Exit fullscreen mode

Inside the controller



<?php

namespace App\Http\Controllers;

use Illuminate\Http\Request;
use Laravel\Socialite\Facades\Socialite;
use App\Models\User;
use Illuminate\Support\Facades\Log;


class SocialController extends Controller
{
  public function redirectToProvider()
  {
    Log::info("apple");
    return Socialite::driver('apple')->redirect();
  }

public function handleProviderCallback()
{
    try {
        $user = Socialite::driver('apple')->stateless()->user();
    } catch (\Exception $e) {
        return redirect()->route('login')->with('error', 'Failed to log in with Apple.');
    }

    $provider_id = $user->getId();
    $email = $user->getEmail() ?? 'no-email-provided';

    $existingUser = User::where('provider_id', $provider_id)->orWhere('email', $email)->first();

    if ($existingUser) {
        auth()->login($existingUser);
    } else {
        $newUser = User::create([
            'name' => $user->getName() ?? 'Apple User',
            'email' => $email,
            'provider_id' => $provider_id,
            'provider_name' => 'apple'
        ]);

        auth()->login($newUser);
    }

    // If it's a mobile app, generate and redirect with a token
    $token = auth()->user()->createToken('authToken')->accessToken;

    return $token;
 }

}


Enter fullscreen mode Exit fullscreen mode

I hope this will works . If you can face any problem on it feel free to knock me Email



alamincsetpi@gmail.com


Enter fullscreen mode Exit fullscreen mode

or you can comment in this below.

Thanks🤪
Happy coding❤️🤪

Top comments (0)

Some comments may only be visible to logged-in visitors. Sign in to view all comments.