Laravel provides a simple and effective way to implement authentication in web applications, but what if you need to authenticate users against different tables? Laravel 9 provides an easy-to-use feature called “Multi-Auth” that allows you to authenticate users against multiple tables.
In this step-by-step guide, we will implement multi-auth in Laravel 9 using different tables for authentication.
Step 1: Create Authentication Tables
First, we need to create the tables for authentication. In this example, we will create two tables: users
and admins
. The users
table will be used for regular user authentication, and the admins
table will be used for admin authentication.
To create these tables, we can use Laravel’s migration feature. Run the following command in the terminal:
php artisan make:migration create_users_table --create=usersphp artisan make:migration create_admins_table --create=admins
This command will create two migration files in the database/migrations
directory: create_users_table.php
and create_admins_table.php
. We can edit these files to define the columns of the tables. For example, the create_users_table.php
migration file can look like this:
Schema::create('users', function (Blueprint $table) { $table->id(); $table->string('name'); $table->string('email')->unique(); $table->timestamp('email_verified_at')->nullable(); $table->string('password'); $table->rememberToken(); $table->timestamps();});
Similarly, we can define the columns of the admins
table in the create_admins_table.php
migration file.
After defining the columns, we can run the following command in the terminal to migrate the tables to the database:
php artisan migrate
Step 2: Create Authentication Guards
Next, we need to create authentication guards for both the users
and admins
tables. Authentication guards are used to specify which table to use for authentication.
To create authentication guards, we need to edit the config/auth.php
file. Add the following code to the guards
array:
'web' => [ 'driver' => 'session', 'provider' => 'users',],'user' => [ 'driver' => 'session', 'provider' => 'users',],'admin' => [ 'driver' => 'session', 'provider' => 'admins',],
In this code, we have created three guards: web
, user
, and admin
. The web
guard is the default guard that uses the users
table for authentication. The user
guard also uses the users
table for authentication, but it is used for regular user authentication. The admin
guard uses the admins
table for authentication and is used for admin authentication.
Step 3: Create Authentication Providers
Next, we need to create authentication providers for both the users
and admins
tables. Authentication providers are used to specify the database table and the model to use for authentication.
To create authentication providers, add the following code to the providers
array in the config/auth.php
file:
'users' => [ 'driver' => 'eloquent', 'model' => App\Models\User::class,],'admins' => [ 'driver' => 'eloquent', 'model' => App\Models\Admin::class,],
In this code, we have created two providers: users
and admins
. The users
provider uses the users
table and the User
model for authentication, while the admins
provider uses the admins
table and the Admin
model for authentication.
Step 4: Create Authentication Models
Next, we need to create the authentication models for both the users
and admins
tables. Authentication models are used to interact with the authentication tables.
To create the authentication models, we can use the make:model
command with the -a
option, which will create a model along with a migration, a factory, and a controller. Run the following commands in the terminal:
php artisan make:model User -aphp artisan make:model Admin -a
This will create four files for each model: a migration, a model, a factory, and a controller. We can edit the model files to specify the table name and the fillable columns. For example, the User
model can look like this:
class User extends Authenticatable{ use HasFactory, Notifiable; protected $fillable = [ 'name', 'email', 'password', ]; protected $hidden = [ 'password', 'remember_token', ]; protected $casts = [ 'email_verified_at' => 'datetime', ]; protected $table = 'users';}
Similarly, we can define the Admin
model.
Step 5: Create Authentication Routes
Next, we need to create authentication routes for both the users
and admins
tables. Authentication routes are used to specify the URLs for login, registration, and other authentication-related pages.
To create authentication routes, we can use the auth
middleware provided by Laravel. Add the following code to the routes/web.php
file:
Route::middleware(['auth:user'])->group(function () { // User routes});Route::middleware(['auth:admin'])->group(function () { // Admin routes});Auth::routes();
In this code, we have created two middleware groups: auth:user
and auth:admin
. The auth:user
middleware group is used for regular user authentication, and the auth:admin
middleware group is used for admin authentication. We can define the routes for each group in the corresponding closure.
The Auth::routes()
method will generate the authentication routes for the web
guard.
Step 6: Test the Authentication
Finally, we can test the authentication by creating login and registration forms for both the users
and admins
tables.
To create the forms, we can use Laravel’s built-in Auth
views. Run the following command in the terminal to publish the views:
php artisan vendor:publish --tag=laravel-auth
This command will publish the auth
views to the resources/views/auth
directory. We can edit these views to add custom styling and other features.
To test the authentication, we can navigate to the login and registration pages and enter valid credentials for each guard. Laravel will authenticate the users against the corresponding table and redirect them to the appropriate page.
Conclusion
In this step-by-step guide, we have learned how to implement multi-auth in Laravel 9 using different tables for authentication. By creating authentication guards, providers, models, routes, and views for each table, we can authenticate users against multiple tables in a Laravel application.
Comments
Post a Comment