To implement the multiple authentication in Laravel you can follow the following steps
1 Create model for Admin
2 Define your guard in config\auth.php
3 Create Route
4 Create View for registration and login
5
Redirect authenticated users
Password Reset
php artisan make:model Admin -m
This will create the migration file too
You can open the migration file which has the name end with create_admins_table and edit the file with following code
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(); }); }
Now you can run the migration command to create the admins
table in the database
php artisan migrate
Then you have to add some code to Admin.php
model. You can copy the code from User.php
model
namespace App; use Illuminate\Notifications\Notifiable; use Illuminate\Foundation\Auth\User as Authenticatable; class Admin extends Authenticatable { use Notifiable; /** * The attributes that are mass assignable. * * @var array */ protected $fillable = [ 'name', 'email', 'password', ]; /** * The attributes that should be hidden for arrays. * * @var array */ protected $hidden = [ 'password', 'remember_token', ]; }
2 Define the Guard
'guards' => [ 'web' => [ 'driver' => 'session', 'provider' => 'users', ], 'api' => [ 'driver' => 'token', 'provider' => 'users', ], 'admin' => [ 'driver' => 'session', 'provider' => 'users', ] ],
I have defined guard name admin in this code
Now you need to create provider for the admin guard
'providers' => [ 'users' => [ 'driver' => 'eloquent', 'model' => App\User::class, ], 'admins' => [ 'driver' => 'eloquent', 'model' => App\Admin::class, ], ],
I have defined the provider admins
here
Now you have to tell to Admin
model that I am going to use admin
guard.
In Admin.php model file you can add the following line protected $guard='admin'
class Admin extends Authenticatable { use Notifiable; protected $guard='admin' /** * The attributes that are mass assignable. * * @var array */ protected $fillable = [ 'name', 'email', 'password', ]; /** * The attributes that should be hidden for arrays. * * @var array */ protected $hidden = [ 'password', 'remember_token', ]; }
Now I am going to create route and add middle ware to route
I have created the group route to make it easy
Route::group(['prefix' => 'admin', 'namespace' => 'Admin'], function () { Route::get('/', 'AdminController@index')->name("admin.home"); Route::get('login', 'Auth\AdminLoginController@showLoginForm')->name("admin.login"); Route::post('login', 'Auth\AdminLoginController@login')->name('admin.login.submit'); Route::get('logout', 'Auth\AdminLoginController@logout')->name('admin.logout'); Route::get('password/reset', 'Auth\AdminForgotPasswordController@showResetPasswordForm')->name('admin.password.request'); Route::post('password/email', 'Auth\AdminForgotPasswordController@sendResetLinkEmail')->name('admin.password.email'); Route::get('password/reset/{token}', 'Auth\AdminResetPasswordController@showResetForm')->name('admin.password.reset.token'); Route::post('password/reset', 'Auth\AdminResetPasswordController@reset')->name('admin.password.reset.submit'); });
we have admin home page for logged admin users and we have login and logout pages too
When you open the login page once you are authenticated it should redirect to some page
You can change the handle
function of the in RedirectIfAuthenticated
class App\Http\Middleware
public function handle($request, Closure $next, $guard = null) { if (Auth::guard($guard)->check()) { if($guard=='admin') return redirect('/admin'); return redirect('/home'); } //return $next($request); }
In password reset process you have two views to deal with
1 email.blade.php
This view has the text field to enter the email address and a button to send password reset link to the given email
2 reset.blade.php
User will receive email to his inbox for password resetting. When user open the URL he can see this view to reset the password
You need to chene the action of the form to