akunbeben/fortify-role

This package is abandoned and no longer maintained. No replacement package was suggested.

Basic Multi Roles authentication package for Fortify.

v1.0.1 2021-05-17 20:57 UTC

This package is auto-updated.

Last update: 2023-11-26 19:49:46 UTC


README

laravel-logolockup-cmyk-red.svg

Fortify Role

Basic Multi Roles authentication package for Fortify.

GitHub license GitHub Workflow Status GitHub release (latest SemVer)

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.