Laravel: Using Different Table and Guard for Login

 

Laravel: Using Different Table and Guard for Login



If you have used Laravel for a while, you should have heard a lot about multiple authentications. You should have also heard “guards” a whole lot. But if you are fairly new to Laravel, multiple authentications makes it possible for you to have different classes of users access different/similar parts of the same application.

There are many reasons why you may want to use multiple authentications in your Laravel application. For example, you have a large application that runs an entire company. Customers also interact with the product and services of the company through the same application. The application also has a blog and there is a department in the company responsible for handling the blog.

Configuration

Open up config/auth.php and add new provider the following in the providers key:

‘employee’ => [
‘driver’ => ‘eloquent’,
‘model’ => App\Models\Employee::class,
],

This means, we are creating new employee provider which using eloquent driver and the model use for eloquent driver is App\Models\Employee::class.

Next add the employee guard in the guard key in config/auth.php:

‘employee’ => [
‘redirectTo’ => ‘employee.home’,
‘driver’ => ‘session’,
‘provider’ => ‘employee’,
],

This means, we are creating new `employee` guard which using `session` driver and using user provider called `employee`, which we created before.

Model & Migration

Now we going to create model and migration for employee

$ php artisan make:model Models/Employee -m 

Open up the model, and update the class as following:

<?phpnamespace App\Models;use Illuminate\Foundation\Auth\User as Model;class Employee extends Model
{
protected $guarded = ['id'];
protected $hidden = [
'employee_password', 'remember_token',
];
public function getAuthPassword()
{
return $this->employee_password;
}
}

Illuminate\Foundation\Auth\User is class that responsible to handle some part of Laravel authentication. So we just need to extend our Employee class to get all the auth related methods.

Then I do overwrite the method name getAuthPassword() by returning the hashed password in employees table, from employee_password field — this where you defined the custom password field you want to use.

The migration, is quite simple:

Schema::create('employees', function (Blueprint $table) {
$table->increments('id');
$table->string('employee_id')->unique();
$table->string('employee_password');
$table->rememberToken();
$table->timestamps();
});

Dummy Data for Employee

Create the factory the employee:

$ php artisan make:factory EmployeeFactory — models=Models/Employee

Open up your EmployeeFactory and add the following:

<?phpuse Faker\Generator as Faker;$factory->define(App\Models\Employee::class, function (Faker $faker) {
return [
'employee_id' => 'EMP' . $faker->randomNumber(5, true),
'employee_password' => '$2y$10$TKh8H1.PfQx37YgCzwiKb.KjNyWgaHb9cbcoQgdIVFlYg7B77UdFm', // secret
];
});

Then, run in terminal:

$ php artisan tinker
>>> factory(\App\Models\Employee::class, 10)->create();

This should give you 10 employees in your employees table.


Post a Comment

0 Comments