leonickl / pxp-auth
Authentication for PXP
Requires
- lbuchs/webauthn: ^2.2
- leonickl/pxp-core: ^v5.13
- nesbot/carbon: ^3.11
- phpmailer/phpmailer: ^7.0
Requires (Dev)
- laravel/pint: ^1.24
- phpstan/phpstan: ^2.1
Suggests
None
Provides
None
Conflicts
None
Replaces
None
README
Authentication for https://github.com/leonickl/pxp.
Install and Register
Install it with composer require leonickl/pxp-auth and register the module in your config.php:
[
'modules' => [
'auth' => 'leonickl/pxp-auth',
],
]
Then, register a user model. It must extend PXP\Auth\Models\Identity. You can use PXP\Auth\Models\User or define your own model and register it in config.php as follows:
use App\Models\User; use PXP\Auth\Models\Identity; [ 'resolver' => [ Identity::class => User::class, ], ]
Setup name fields
By default, the users table has one single name field. If you want to customize this, adjust your config.php:
[
'auth' => [
'name-columns' => [
'first_name' => 'First Name',
'last_name' => 'Last Name',
],
],
]
Setup roles
By default, users have the two available roles "regular" (0) and "admin" (1), but you can configure own roles, their levels, and labels in config.php if you like. The larger the corresponsing role number, the role should be more powerful.
[
'auth' => [
'roles' => [
'levels' => [
'REGULAR' => 0,
'ORGA' => 1,
'ADMIN' => 2,
],
'labels' => [
'REGULAR' => 'User',
'ORGA' => 'Organiser',
'ADMIN' => 'Admin',
],
],
],
]
Migrate
Then, copy the package's migrations to your app's migrations directory with ./run auth:publish-migrations. Adjust the user definition to your model (e.g., differing name fields).
Add auth routes
use PXP\Auth\Middleware\InteractiveAuth; use PXP\Auth\Controllers\LoginController; use PXP\Auth\Controllers\RegisterController; use PXP\Auth\Controllers\VerificationController; // remove this group if you do not want anyone // to be able to register themself Route::group( Route::get('/auth/register')->do(RegisterController::class, 'form')->name('register'), Route::post('/auth/register')->do(RegisterController::class, 'register'), ); Route::get('/auth/verify')->do(VerificationController::class, 'verify')->name('verify'); Route::group( Route::get('/auth/login')->do(LoginController::class, 'form')->name('login'), Route::post('/auth/login')->do(LoginController::class, 'login'), ); Route::group( Route::get('/auth/logout')->do(LoginController::class, 'logout')->name('logout'), Route::post('/auth/logout')->do(LoginController::class, 'logout'), ) ->middleware(InteractiveAuth::class);
Protect routes
Protect your routes by adding middleware to them. You may also want to restrict access to users that have already verified their email address (VerifiedEmail) or allow access only for admins (RequireAdmin). You can also add middleware to a Route::group() block.
use App\Controllers\MyController; use PXP\Auth\Middleware\InteractiveAuth; use PXP\Auth\Middleware\VerifiedEmail; Route::get('/my')->do(MyController::class, 'index')->name('my') ->middleware(InteractiveAuth::class) ->middleware(VerifiedEmail::class);
Configure mail server
Configure the SMTP server in config.php, so that the app can send verification emails to the users.
[
'mail' => (object) [
'host' => env('MAIL_HOST'),
'user' => env('MAIL_USER'),
'pass' => env('MAIL_PASS'),
'port' => env('MAIL_PORT'),
],
]
Passkeys
You may add passkey support. Therefore, fill in the following config values:
[
'auth' => [
'relying-party' => [
'name' => 'Merch',
'id' => env('RELYING_PARTY', 'localhost'),
],
],
]
Then, add some additional routes to routes.php:
Route::get('/auth/register-passkey')->do(RegisterController::class, 'passkeyForm') ->name('register-passkey-form'); Route::post('/auth/register-passkey-args')->do(RegisterController::class, 'registerPasskeyArgs') ->name('register-passkey-args'); Route::post('/auth/register-passkey')->do(RegisterController::class, 'registerPasskey') ->name('register-passkey'); Route::post('/auth/validate-passkey-args')->do(LoginController::class, 'validatePasskeyArgs') ->name('validate-passkey-args'); Route::post('/auth/validate-passkey')->do(LoginController::class, 'validatePasskey') ->name('validate-passkey');
Finally, ensure you have a route to serve javascript files and register the passkey script in the config file:
use PXP\Http\Controllers\AssetController; Route::get('/js/{file}')->do(AssetController::class, 'js')->name('js');
[
'js' => [
'passkey',
],
]