wika-group / azureadb2c-spa-for-laravel
Use Azure AD B2C single sign-on for Laravel single-page applications
Installs: 158
Dependents: 0
Suggesters: 0
Security: 0
Stars: 2
Watchers: 2
Forks: 0
Open Issues: 0
pkg:composer/wika-group/azureadb2c-spa-for-laravel
Requires
- php: ^8.2
- firebase/php-jwt: ^6.11
- spatie/laravel-package-tools: ^1.16
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.
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;
If you use Wire Extender, you must Enable browser session support.
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(array $user) {} #[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:
