Laravel 5.4 multi Authentication


first let take a look at file structur :

   as you can see we got Admin\Auth controller that we use for admin authentication and Auth for client authentication and yes we have view for both login . as you noticed both have completed set of auth that made by command php artisan make:auth and in middleware we create two middlewares like in picture CheckAdminAuthenticated.php and RedirectifAdminAuthenticated.php . let get fast just run command to create auth and copy to Admin folder and then change some code.

now start our step like alway i know you only on the step

step 0 : run command to create auth

php artisan make:auth

step 1 : open config/auth.php and add admins guard like below

'guards' => [
        'web' => [
            'driver' => 'session',
            'provider' => 'users',
        ],

        'api' => [
            'driver' => 'token',
            'provider' => 'users',
        ],

        // For admin
        'admins' => [
            'driver'   => 'session',
            'provider' => 'admins'
        ]
 ],

'providers' => [
        'users' => [
            'driver' => 'eloquent',
            'model' => App\User::class,
        ],

        // For admin
        'admins' => [
            'driver' => 'eloquent',
            'model' => App\Admin::class
        ]

        // 'users' => [
        //     'driver' => 'database',
        //     'table' => 'users',
        // ],
 ],

//this part is for admins forgetpassword

 'passwords' => [
        'users' => [
            'provider' => 'users',
            'email' => 'auth.emails.password',
            'table' => 'password_resets',
            'expire' => 60,
        ],

        'admins' => [
            'provider' => 'admins',
            'email' => 'auth.emails.password',
            'table' => 'password_resets',
            'expire' => 60,
        ],
    ],

let create migrate and model for admin


use Illuminate\Support\Facades\Schema;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Database\Migrations\Migration;

class CreateAdminsTable extends Migration
{
    /**
     * Run the migrations.
     *
     * @return void
     */
    public function up()
    {
        Schema::create('admins', function (Blueprint $table) {
            $table->increments('id');
            $table->string('name');
            $table->string('email','100')->unique();
            $table->string('password');
            $table->rememberToken();
            $table->timestamps();
        });
    }

    /**
     * Reverse the migrations.
     *
     * @return void
     */
    public function down()
    {
        Schema::dropIfExists('admins');
    }
}

now let run migrate to create your table

php artisan migrate

than we create model like below

php artisan make:model Admin

than fill code like below

//App/Admin

namespace App;

use Illuminate\Foundation\Auth\User as Authenticatable;
use Illuminate\Notifications\Notifiable;

class Admin extends Authenticatable
{

	use Notifiable;

	
	protected $guard = "admins";
    
    protected $fillable = [
        'name', 'email', 'password',
    ];

    protected $hidden = [
        'password', 'remember_token',
    ];

}

step 3: we create middleware as follow command

php artisan make:middleware CheckIfAdminAuthenticated

//and

php artisan make:middleware RedirectIfAdminAuthenticated

*** why we create 2 middleware ? one to check guest and logged in user because we create custom middleware not like intergrated user middleware

now let change some code in app/http/middleware/CheckIfAdminAuthenticated.php that we got after run command

namespace App\Http\Middleware;

use Closure;
use Illuminate\Support\Facades\Auth;

class CheckIfAdminAuthenticated
{
    public function handle($request, Closure $next, $guard = null)
    {
        
        if (!Auth::guard('admins')->check()) {
            return redirect('/admin');
        }

        return $next($request);
    }
}

and  app/http/middleware/RedirectIfAdminAuthenticated.php

<?php

namespace App\Http\Middleware;

use Closure;
use Illuminate\Support\Facades\Auth;

class RedirectIfAdminAuthenticated
{

    public function handle($request, Closure $next, $guard = null)
    {
        
        if (Auth::guard('admins')->check()) {
            return redirect('/admins/home');
        }

        return $next($request);
    }
}

then we add class to routemiddleware

protected $routeMiddleware = [
        .
        .
        .

        'admin.auth' => \App\Http\Middleware\CheckIfAdminAuthenticated::class,
        'admin.guest' => \App\Http\Middleware\RedirectIfAdminAuthenticated::class,
    ];

step 4 : create folder admin copy auth folder with HomeController to folder admin open LoginController.php and edit code like below

namespace App\Http\Controllers\Admin\Auth;

use App\Http\Controllers\Controller;
use Illuminate\Foundation\Auth\AuthenticatesUsers;
use Illuminate\Support\Facades\Auth;

class LoginController extends Controller
{
    
    use AuthenticatesUsers;

    protected $redirectTo = '/admins/home';

    protected $guard = ['admins']; // add in case error with function guard

    public function __construct()
    {
        $this->middleware('admin.guest', ['except' => 'logout']);
    }

    public function showLoginForm()
    {
        return view('admin.login');
    }

    //this function doesn't add on users loginController
    protected function guard()
    {
        return Auth::guard('admins');
    }

}

step 5 : let get create route

Route::group(['namespace' => 'Admin','prefix' => 'admins'], function () {
    Route::get('/', 'Auth\[email protected]');
    Route::post('login', 'Auth\[email protected]');
    Route::post('logout', 'Auth\[email protected]');
    Route::get('home', '[email protected]');
});

job done ! now you have admin auth and user auth !!!!!!!!!!

Share this Post:

Related Posts:

www.000webhost.com