Laravel 自定义多重身份验证

Laravel Custom Multi Authentication

我正在创建一个具有自定义多重身份验证的 laravel 应用程序。我正在关注这篇文章进行多重身份验证。 https://pusher.com/tutorials/multiple-authentication-guards-laravel

我已经创建了登录和注册控制器定义的守卫和提供者,一切正常,我可以注册用户并登录他们。我做了一个页面(http://127.0.0.1:8000/admin)这个页面应该只有在管理员登录时才能访问,但是这个url是所有人都可以访问的。下面是我的代码,

登录控制器代码

public function __construct()
{
     $this->middleware('guest')->except('logout');
        $this->middleware('guest:admin')->except('logout');
        $this->middleware('guest:writer')->except('logout');
}


public function showAdminLoginForm()
{
    return view('auth.login', ['url' => 'admin']);
}

public function adminLogin(Request $request)
{
    $this->validate($request, [
        'email'   => 'required|email',
        'password' => 'required|min:6'
    ]);

    if (Auth::guard('admin')->attempt(['email' => $request->email, 'password' => $request>password], $request->get('remember'))) {

        return redirect()->intended('/admin');
    }
    return back()->withInput($request->only('email', 'remember'));
}

注册控制器代码

     public function __construct()
    {
        $this->middleware('guest');
            $this->middleware('guest:admin');
            $this->middleware('guest:writer');


    }

     public function showAdminRegisterForm()
    {
        return view('auth.register', ['url' => 'admin']);
    }
protected function createAdmin(Request $request)
    {
        $this->validator($request->all())->validate();
        $admin = Admin::create([
            'name' => $request['name'],
            'email' => $request['email'],
            'password' => Hash::make($request['password']),
        ]);
        return redirect()->intended('login/admin');
    }

管理员模型代码

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

use Illuminate\Database\Eloquent\Model;

class admin extends Authenticatable 
{


     use Notifiable;

        protected $guard = 'admin';

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

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

Handler.php

  protected function unauthenticated($request, AuthenticationException $exception)
    {
        if ($request->expectsJson()) {
            return response()->json(['error' => 'Unauthenticated.'], 401);
        }
        if ($request->is('admin') || $request->is('admin/*')) {
            return redirect()->guest('/login/admin');
        }
        if ($request->is('writer') || $request->is('writer/*')) {
            return redirect()->guest('/login/writer');
        }
        return redirect()->guest(route('login'));
    }

RedirectifAuthenticated.php

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

        return $next($request);
    }

Admin.blade.php 查看

   <html>
    <head>
        <title>admin page </title>
    </head>
    <body>

    <h1> hi bosss </h1>
    </body>
    </html>

//Auth.php

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

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


          'admin' => [
            'driver' => 'session',
            'provider' => 'admins',
        ],
        'writer' => [
            'driver' => 'session',
            'provider' => 'writers',
        ],
'providers' => [
    'users' => [
        'driver' => 'eloquent',
        'model' => App\User::class,
    ],

    'admins' => [
        'driver' => 'eloquent',
        'model' => App\admin::class,
    ],
    'writers' => [
        'driver' => 'eloquent',
        'model' => App\writer::class,
    ],

//web.php

  <?php
Route::view('/', 'welcome');
Auth::routes();

Route::get('/login/admin', 'Auth\LoginController@showAdminLoginForm');
Route::get('/login/writer', 'Auth\LoginController@showWriterLoginForm');
Route::get('/register/admin', 'Auth\RegisterController@showAdminRegisterForm');
Route::get('/register/writer', 'Auth\RegisterController@showWriterRegisterForm');

Route::post('/login/admin', 'Auth\LoginController@adminLogin');
Route::post('/login/writer', 'Auth\LoginController@writerLogin');
Route::post('/register/admin', 'Auth\RegisterController@createAdmin');
Route::post('/register/writer', 'Auth\RegisterController@createWriter');

Route::view('/home', 'home')->middleware('auth');
Route::view('/admin', 'admin');
Route::view('/writer', 'writer');

求推荐。

您应该将 auth:admin 中间件分配给您希望仅限管理员访问的路由:

Route::view('admin', 'admin')->middleware('auth:admin');

Laravel 6.x Docs - Authentication - Protecting Routes - Specifying A Guard

我认为不值得拥有两个单独的表useradmin
我找到的多重身份验证的最佳方法是:
https://www.itsolutionstuff.com/post/laravel-6-multi-auth-authentication-tutorialexample.html