1. Go to this page and download the library: Download lamy/laravel-sign-in-apple library. Choose the download type require.
2. Extract the ZIP file and open the index.php.
3. Add this code to the index.php.
<?php
require_once('vendor/autoload.php');
/* Start to develop here. Best regards https://php-download.com/ */
lamy / laravel-sign-in-apple example snippets
bash
php artisan laravel-sign-in-apple:install
bash
php artisan tinker
bash
namespace App\Http\Controllers\Auth;
use App\Models\User;
use Illuminate\Http\Request;
use App\Http\Controllers\Controller;
use Illuminate\Support\Facades\Auth;
use Laravel\Socialite\Facades\Socialite;
use Lamy\LaravelSignInApple\Facades\LaravelSignInApple;
class SocialAuthenticationController extends Controller
{
public function socialRedirect()
{
return Socialite::driver('apple')->scopes(['name', 'email'])->redirect();
}
public function appleCallback(LaravelSignInApple $signInApple, Request $request)
{
$socialUser = $signInApple->decodeAppleToken($request);
if (property_exists($socialUser, 'success') && $socialUser->success === false) {
return redirect()->intended('/')->with('error', $socialUser->message);
}
return $this->authenticate($socialUser, $request);
}
public function authenticate(object $socialUser, Request $request)
{
$userExist = User::where("apple_id", $socialUser->getId())->first();
$redirectTo = '/';
if ($userExist) {
Auth::login($userExist);
$redirectTo = '/home';
} elseif ($socialUser->getEmail()) {
$user = User::updateOrCreate(
['email' => $socialUser->getEmail()],
[
'firstname' => $socialUser->getName()['firstName'] ?? null,
'lastname' => $socialUser->getName()['lastName'] ?? null,
'email' => $socialUser->getEmail(),
'apple_id' => $socialUser->getId(),
'apple_token' => $socialUser->token,
'apple_refresh_token' => $socialUser->refreshToken,
]
);
Auth::login($user);
$redirectTo = '/home';
}
return redirect($redirectTo);
}
}