wika-group / azureadb2c-spa-for-laravel
Use Azure AD B2C single sign-on for Laravel single-page applications
Package info
github.com/WIKA-Group/azureadb2c-spa-for-laravel
pkg:composer/wika-group/azureadb2c-spa-for-laravel
Requires
- php: ^8.2
- firebase/php-jwt: ^7.0
- spatie/laravel-package-tools: ^1.93
Requires (Dev)
None
Suggests
None
Provides
None
Conflicts
None
Replaces
None
README
This composer package provides the necessary logic to handle Azure B2C logins with MSAL in the front end (with a pop-up) and back-end validation. It requires Livewire.
If you want to use it with Wire Extender, use this package azureadb2c-spa-for-laravel-wire-extender.
Installation
Install the package:
composer require wika-group/azureadb2c-spa-for-laravel
Publish assets:
php artisan vendor:publish --tag=azureb2cspa-assets
Publish migration:
php artisan vendor:publish --tag=azureb2cspa-migrations
Extend .env and configure:
AADB2C_TENANT_ID= AADB2C_CLIENT_ID= AADB2C_DOMAIN= # {your_domain}.b2clogin.com AADB2C_CUSTOM_DOMAIN= # Optional: set to use custom domain e.g. login.contoso.com AADB2C_POLICY= # Optional - Default: 'B2C_1_sign-up_and_sign-in_policy' AADB2C_DEFAULT_ALGORITHM= # Optional: Decoding algorithm JWK key. Default: 'RS256' AADB2C_OAUTH_COLUMN= # Optional: Name of the OAuth ID column. Default 'oauth_id'
Optional: Publish config:
php artisan vendor:publish --tag=azureb2cspa-config
Usage
Add scripts to your views
<!-- In main livewire component --> <livewire:azureB2cSpaScripts/>
In order to trigger a Livewire re-render, an event listener is required.
Therefore, the provided trait can be used inside the main Livewire component.
use \WikaGroup\AzureAdB2cSpa\Traits\LoginLogoutEvents;
Add a button to trigger login or logout
@auth
<button onClick="b2cLogout()">Logout</button>
@endauth
@guest
<button onClick="b2cPopupLogin()">Login with Azure B2C</button>
@endguest
Hook into the events
You can add custom logic by using the emitted events for login and logout:
#[On('azureb2c-login-succeeded')] public function azureB2cLoginSucceeded() { $userData = session('azureb2c-user-data'); } #[On('azureb2c-login-failed')] public function azureB2cLoginFailed(string $msg) {} #[On('azureb2c-logout-succeeded')] public function azureB2cLogoutSucceeded() {} #[On('azureb2c-logout-failed')] public function azureB2cLogoutFailed(string $msg) {}
Configure Azure B2C
You must add the URL of the SPA in the Azure Portal:
