This is an MIT-licensed open source project with its ongoing development made possible by the support of the community. If you'd like to support this, and our other packages, please consider sponsoring us via the button above.
We thank the following sponsors for their generosity, please take a moment to check them out:
Install the composer package:
composer require genealabs/laravel-sign-in-with-apple
We also recommend using geneaLabs/laravel-socialiter to automatically manage user resolution and persistence:
composer require genealabs/laravel-socialiter
Create an App ID
for your website (https://developer.apple.com/account/resources/identifiers/list/bundleId) with the following details:
Create a Service ID
for your website (https://developer.apple.com/account/resources/identifiers/list/serviceId) with the following details:
Create a Private Key
for your website (https://developer.apple.com/account/resources/authkeys/list) with the following details:
key.txt
Create your app's client secret:
Install the JWT Gem:
sudo gem install jwt
Create a file called client_secret.rb
to process the private key:
require 'jwt'
key_file = 'key.txt'
team_id = ''
client_id = ''
key_id = ''
ecdsa_key = OpenSSL::PKey::EC.new IO.read key_file
headers = {
'kid' => key_id
}
claims = {
'iss' => team_id,
'iat' => Time.now.to_i,
'exp' => Time.now.to_i + 86400*180,
'aud' => 'https://appleid.apple.com',
'sub' => client_id,
}
token = JWT.encode claims, ecdsa_key, 'ES256', headers
puts token
Fill in the following fields:
team_id
: This can be found on the top-right corner when logged into
your Apple Developer account, right under your name.client_id
: This is the identifier from the Service Id created in step
2 above, for example com.example.servicekey_id
: This is the identifier of the private key created in step 3
above.Save the file and run it from the terminal. It will spit out a JWT which is
your client secret, which you will need to add to your .env
file in the
next step.
ruby client_secret.rb
Set the necessary environment variables in your .env
file:
SIGN_IN_WITH_APPLE_LOGIN="/apple/login/controller/login/action"
SIGN_IN_WITH_APPLE_REDIRECT="/apple/login/controller/callback/action"
SIGN_IN_WITH_APPLE_CLIENT_ID="your app's service id as registered with Apple"
SIGN_IN_WITH_APPLE_CLIENT_SECRET="your app's client secret as calculated in step 4"
Add the following blade directive to your login page:
@signInWithApple($color, $hasBorder, $type, $borderRadius)
Parameter | Definition |
---|---|
$color | String, either "black" or "white. |
$hasBorder | Boolean, either true or false . |
$type | String, either "sign-in" or "continue" . |
$borderRadius | Integer, greater or equal to 0. |
This implementation uses Socialite to get the login credentials. The following is an example implementation of the controller:
<?php
namespace App\Http\Controllers\Auth;
use App\Http\Controllers\Controller;
use GeneaLabs\LaravelSocialiter\Facades\Socialiter;
use Laravel\Socialite\Facades\Socialite;
class AppleSigninController extends Controller
{
public function __construct()
{
$this->middleware('guest')->except('logout');
}
public function login()
{
return Socialite::driver("sign-in-with-apple")
->scopes(["name", "email"])
->redirect();
}
public function callback(Request $request)
{
// get abstract user object, not persisted
$user = Socialite::driver("sign-in-with-apple")
->user();
// or use Socialiter to automatically manage user resolution and persistence
$user = Socialiter::driver("sign-in-with-apple")
->login();
}
}
Note that when processing the returned $user
object, it is critical to know that the sub
element is the unique identifier for the user, NOT the email address. For more details, visit https://developer.apple.com/documentation/signinwithapplerestapi/authenticating_users_with_sign_in_with_apple.
During package development I try as best as possible to embrace good design and development practices, to help ensure that this package is as good as it can be. My checklist for package development includes:
Please observe and respect all aspects of the included Code of Conduct.
When reporting issues, please fill out the included template as completely as possible. Incomplete issues may be ignored or closed if there is not enough information included to be actionable.
Please review the Contribution Guidelines. Only PRs that meet all criterium will be accepted.
We have included the awesome symfony/thanks
composer package as a dev dependency. Let your OS package maintainers know you appreciate them by starring the packages you use. Simply run composer thanks
after installing this package. (And not to worry, since it's a dev-dependency it won't be installed in your live environment.)