DEV Community

LinaAhmadGhojan
LinaAhmadGhojan

Posted on

Laravel Multi Auth using Guards with Example (Api)

Image description

  • install laravel global
    composer global require laravel/installer

  • creat project
    laravel new example-app

  • run project
    cd example-app
    php artisan serve

  • Database Configuration
    DB_CONNECTION=mysql
    DB_HOST=127.0.0.1
    DB_PORT=3306
    DB_DATABASE=database_name
    DB_USERNAME=database_username
    DB_PASSWORD=database_password

  • Create migration and model for admins
    To make the model and migration for admins table, run the following command:

php artisan make:model Admin -m

It will create migration and model both. From the database/migrations directory, open the admin's migrations file, and edit it as follows:
public function up()
{
Schema::create('admins', function (Blueprint $table) {
$table->increments('id');
$table->string('name');
$table->string('email')->unique();
$table->string('password');
$table->rememberToken();
$table->timestamps();
});
}

  • migrate database
    php artisan migrate

  • Open the Admin model in app/Models/Admin.php and add the following:
    use Illuminate\Notifications\Notifiable;
    use Illuminate\Foundation\Auth\User as Authenticatable;
    class Admin extends Authenticatable
    {
    use Notifiable;
    protected $guard = 'admin';
    protected $fillable = [
    'name', 'email', 'password',
    ];
    protected $hidden = [
    'password', 'remember_token',
    ];
    }

  • Define the guards , open config/auth.php file
    before adding a new guard, first, we need to add the provider for that because all authentication drivers have a user provider. Let’s add a user provider for admin users where the driver is eloquent and the model is an Admin model class. After adding the provider array will look like below:
    'providers' => [
    'users' => [
    'driver' => 'eloquent',
    'model' => App\Models\User::class,
    ],
    'admins' => [
    'driver' => 'eloquent',
    'model' => App\Models\Admin::class,
    ]
    ]

now we will create a new guard for admin users, where the driver will be session and the provider is admins. after adding that guard array will look like below:
'guards' => [
'web' => [
'driver' => 'session',
'provider' => 'users',
],
'admin' => [
'driver' => 'session',
'provider' => 'admins',
],
'api' => [
'driver' => 'token',
'provider' => 'users',
'hash' => false,
]
]

  • now got to (app\Providers\RouteServiceProvider.php)
    for make routes admin in file (admin.php)
    write :
    ` */
    public function boot(): void
    {
    $this->configureRateLimiting();

    $this->routes(function () {
        Route::middleware('api')
            ->prefix('api')
            ->group(base_path('routes/api.php'));
    
        Route::middleware('web')
            ->group(base_path('routes/web.php'));
    
                // Admin User
        Route::prefix('admin')->name('admin.')
        ->namespace($this->namespace)
        ->group(base_path('routes/admin.php'));
    
    });
    

    }
    `

  • create file route in path (routes\admin.php)
    now write all routes for the

admin
in this file

Top comments (0)