Basic Multi Roles authentication package for Fortify.
- Laravel ^8.x
- Laravel/Fortify ^1.x
I recommended you to install this package only on a fresh project.
Install the package using composer
composer require akunbeben/fortify-role
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 () {
});
- 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.