akunbeben / fortify-role
Basic Multi Roles authentication package for Fortify.
Requires
- php: ^7.3|^8.0
Requires (Dev)
- mockery/mockery: ^1.4
- orchestra/testbench: ^6.17
- phpunit/phpunit: ^9.5
This package is auto-updated.
Last update: 2023-11-26 19:49:46 UTC
README
Fortify Role
Basic Multi Roles authentication package for Fortify.
Requirement
- Laravel ^8.x
- Laravel/Fortify ^1.x
Installation
I recommended you to install this package only on a fresh project.
Install the package using composer
composer require akunbeben/fortify-role
Usage
Before continuing the process, please make sure the FortifyServiceProvider.php
is registered in config/app.php
. If it's already registered, you can skip this.
'providers' => [ ... App\Providers\FortifyServiceProvider::class, ],
Then publish the package's vendor files. It's need --force
because it will replace the RedirectIfAuthenticated
middleware.
php artisan vendor:publish --provider="Akunbeben\FortifyRole\FortifyRoleServiceProvider" --force
And then, add HasRole
traits to your User
model.
<?php namespace App\Models; use Akunbeben\FortifyRole\Traits\HasRole; ... class User extends Authenticatable { use HasFactory, Notifiable, HasRole; ... }
Now you need to run the migration, I suggest to migrate with the seeder. Or if you want to modify the seeder you can find the seeder file on database/seeders/RoleSeeder.php
and database/seeders/UserSeeder.php
php artisan migrate --seed
Finally, you can use the role
middleware in your routes like this example below:
... Route::group(['middleware' => ['auth', 'role:admin']], function () { }); Route::group(['middleware' => ['auth', 'role:user']], function () { });
Notes
- On the roles table migration file, there is a default value to assign the role to registered user. You can adjust it to your need.