How to create multi auth in laravel 7?

喜欢而已 提交于 2020-08-24 03:33:22

问题


I used to be for laravel 5.5 and earlier than https://github.com/Hesto/multi-auth .

But this repository don't update for laravel 7.0

How to create multi auth in Laravel 7.0 ?


回答1:


If you want to use a package the you can use this package laravel-multiauth

OR

if you want to create custom multi-auth based on a field in your users table for.e.g is_admin then follow the below steps:

Assuming you have installed Laravel and made a connection to database

Step1: Add new row is_admin in users table and model. then run the migration.

 public function up()
        {
            Schema::create('users', function (Blueprint $table) {
                $table->bigIncrements('id');
                $table->string('name');
                $table->string('email');
                $table->timestamp('email_verified_at')->nullable();
                $table->boolean('is_admin')->nullable(); // add this
                $table->string('password');
                $table->rememberToken();
                $table->timestamps();

        });

}

app/User.php

protected $fillable = [
    'name', 'email', 'password', 'is_admin' //add here
];

Then run the migration

   php artisan migrate

Step2: Create Auth using scaffold

Install laravel/ui package using below command

composer require laravel/ui 

Generate auth

php artisan ui bootstrap --auth 

npm install

npm run dev

Step3: Create IsAdmin Middleware will allows only admin access users to that routes

php artisan make:middleware IsAdmin

app/Http/middleware/IsAdmin.php

Add this in IsAdmin middleware

public function handle($request, Closure $next)
{
    if(auth()->user()->is_admin == 1){
       return $next($request);
    }
    return redirect(‘home’)->with(‘error’,"You don't have admin access.");
}

Register your IsAdmin middleware in app/Http/Kernel.php

protected $routeMiddleware = [
       'auth' => \App\Http\Middleware\Authenticate::class,
        'auth.basic' => \Illuminate\Auth\Middleware\AuthenticateWithBasicAuth::class,
        'bindings' => \Illuminate\Routing\Middleware\SubstituteBindings::class,
        'cache.headers' => \Illuminate\Http\Middleware\SetCacheHeaders::class,
        'can' => \Illuminate\Auth\Middleware\Authorize::class,
        'guest' => \App\Http\Middleware\RedirectIfAuthenticated::class,
       'signed' => \Illuminate\Routing\Middleware\ValidateSignature::class,
       'throttle' => \Illuminate\Routing\Middleware\ThrottleRequests::class,
   'verified' => \Illuminate\Auth\Middleware\EnsureEmailIsVerified::class,
    'is_admin' => \App\Http\Middleware\IsAdmin::class, // add this

];

Step4: Create your route for admin in routes/web.php

Route::get('admin/home', 'HomeController@adminHome')->name('admin.home')->middleware('is_admin');

Step5: Add adminHome() method for admin route in app/Http/Controllers/HomeController.php

public function adminHome()
 {
      return view('adminHome');
 }

Step6: Change LoginController, when user will login than we redirect according to user access. if normal user than we will redirect to home route and if admin user than we redirect to admin route in app/Http/Controllers/Auth/LoginController.php

public function login(Request $request)
{   
    $input = $request->all();
    $this->validate($request, [
        'email' => 'required|email',
        'password' => 'required',
    ]);

    if(auth()->attempt(array('email' => $input['email'], 'password' => $input['password'])))
    {
        if (auth()->user()->is_admin == 1) {
            return redirect()->route('admin.home');
        }else{
            return redirect()->route('home');
        }
    }else{
        return redirect()->route('login')
            ->with('error','Email-Address And Password Are Wrong.');
    }

}


来源:https://stackoverflow.com/questions/60762126/how-to-create-multi-auth-in-laravel-7

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!