Skip to main content

Command Palette

Search for a command to run...

Social Login (Apple) | Laravel-11 (API ONLY)

Published
2 min read
Social Login (Apple) | Laravel-11 (API ONLY)
J

👨‍💻 Laravel Developer | PHP | MySQL | REST APIs Hi, I'm a Laravel developer with experience in building web applications using PHP and modern development practices. I work on backend systems, RESTful APIs, and database structures. I enjoy writing clean code, integrating third-party services, and improving workflows for better performance. 🔧 Tools I often use: Laravel, MySQL, Git, Composer, REST APIs 🚀 Focused on: Simplicity, performance, and maintainability

Laravel social Login . (Provider Apple)

Only for api or laravel backend authentication system using socialite package

Step 1 :

nstall laravel project and isntall socialite package..

composer create-project "laravel/laravel:^11.0" (project-name)
composer require laravel/socialite

Step: 2

//add this line on user table 
$table->string('apple_id')->nullable();


php artisan migrate
php artisan migrate:fresh --seed
le add google_id feild. After that run the migrations.

Step 3:

Setup service.php file and .env file for apple app credentials


    'apple' => [
        'client_id' => env('APPLE_CLIENT_ID'),
        'team_id' => env('APPLE_TEAM_ID'),
        'key_id' => env('APPLE_KEY_ID'),
        'key_path' => env('APPLE_PRIVATE_KEY'),
        'redirect' => env('APPLE_REDIRECT_URI'),
        // 'client_secret' => Helper::generateAppleClientSecret(),
    ],

APPLE_CLIENT_ID=Your client ID here
APPLE_TEAM_ID=Your Team ID here
APPLE_KEY_ID=Your Key here
APPLE_PRIVATE_KEY=Your private key here

Step:4

Set up route on api.php file

Route::post('/apple/signin', [YourControllerName::class, 'applesignin']);

Step: 5

Controller logic. If the user name will be f_name and l_name the follow the first one

    protected $client_id;
    protected $key_id;
    protected $team_id;
    protected $private_key;
    protected $redirect_url;

   public function __construct()
    {
        $this->client_id = config('services.apple.client_id');
        $this->key_id = config('services.apple.key_id');
        $this->team_id = config('services.apple.team_id');
        $this->private_key = config('services.apple.private_key');
        $this->redirect_url = config('services.apple.redirect');

        // dd($this->client_id, $this->key_id, $this->team_id, $this->private_key, $this->redirect_url);

    }

    public function appleAuthentication(Request $request)
    {

        // dd(config('services.google.client_secret'));

        try {
            $token = $request->input('token');
            if (!$token) {
                return response()->json(['status' => 'error', 'message' => 'Token is required'], 422);
            }

            $socialUser = Socialite::driver('apple')->stateless()->userFromToken($token);


            $userData = $this->extractUserData($socialUser);


            $user = User::updateOrCreate(
                ['email' => $userData['email']],
                [
                    'name' => $userData['name'],
                    'avatar' => $socialUser->getAvatar(),
                    'password' => bcrypt($socialUser->getId()),
                    'apple_id' => $socialUser->getId(),
                    'is_otp_verified' => 1,
                    'is_apple_signin' => true,
                ]
            );


            $user->update([
                'is_social_logged' => true,
            ]);


            Auth::login($user);
            $token = auth('api')->login($user);


            $response = [
                'id' => $user->id,
                'name' => $user->name,
                'email' => $user->email,
                'phone' => $user->phone ?? null,
                'avatar' => $user->avatar,
                'role' => $user->role,
                'token' => $token,
            ];

            return $this->success($response, 'Successfully Logged In', 200);
        } catch (\Exception $e) {

            Log::error("Apple Login Error: " . $e->getMessage());
            return $this->error([], $e->getMessage(), 500);
        }
    }

Step:6

Check from postman tool for using apple token..


💡
Thank You.