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.
Install the package:
composer require wika-group/azureadb2c-spa-for-laravelPublish assets:
php artisan vendor:publish --tag=azureb2cspa-assetsPublish migration:
php artisan vendor:publish --tag=azureb2cspa-migrationsExtend .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<!-- 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;@auth
<button onClick="b2cLogout()">Logout</button>
@endauth
@guest
<button onClick="b2cPopupLogin()">Login with Azure B2C</button>
@endguestYou 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) {}You must add the URL of the SPA in the Azure Portal:
