我想制作多身份验证Laravel

时间:2019-12-11 18:12:37

标签: laravel authentication

我已经使用中间件完成了Laravel登录,它适用于管理员和简单用户,对于餐厅和送货用户,它重定向到简单用户页面和路由器显示错误404,如何解决,它将重定向到正确的用户? 我只需要修复如何将正确的用户重定向到正确的路由,因为在php artisan上显示make:list路由器时,它会向我显示错误404。

路线:

Route::get('/', function () {
    return view('welcome');
});


Route::get('/admin', 'HomeController@admin_dashboard')->name('admin.dashboard')->middleware(['auth', 'is_admin']);
Route::get('/restaurant', 'HomeController@restaurant_dashboard')->name('restaurant.dashboard')->middleware(['auth', 'is_restaurant']);
Route::get('/delivery', 'HomeController@delivery_dashboard')->name('delivery.dashboard')->middleware(['auth', 'is_delivery']);
Auth::routes();

Route::get('/home', 'HomeController@index')->name('home');

管理中间件:

<?php

namespace App\Http\Middleware;

use Closure;
use Auth;
class IsAdmin
{
    /**
     * Handle an incoming request.
     *
     * @param  \Illuminate\Http\Request  $request
     * @param  \Closure  $next
     * @return mixed
     */
    public function handle($request, Closure $next)
    {
        if (Auth::check() && (Auth::user()->user_type == 'admin')) {
            return $next($request);
        }
        else{
            abort(404);
        }
    }
}

交付中间件:

<?php

namespace App\Http\Middleware;

use Closure;
use Auth;
class IsDelivery
{
    /**
     * Handle an incoming request.
     *
     * @param  \Illuminate\Http\Request  $request
     * @param  \Closure  $next
     * @return mixed
     */
    public function handle($request, Closure $next)
    {
        if (Auth::check() && (Auth::user()->user_type == 'delivery')) {
            return $next($request);
        }
        else{
            abort(404);
        }
    }
}

登录代码:

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



    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()->user_type == 'admin') {
                return redirect()->route('admin.dashboard');
            } else
            if (auth()->user()->user_type == 'restaurant') {
                return redirect()->route('restaurant.dashboard');
            } 
            else
            if (auth()->user()->user_type == 'delivery') {
                return redirect()->route('delivery.dashboard');
            } else
            if (auth()->user()->user_type == 'user') {
                return redirect()->route('home');
            }
        }else{
            return redirect()->route('login')
                ->with('error','Email-Address And Password Are Wrong.');
        }

    }

家庭控制器:

public function __construct()
    {
        $this->middleware('auth');
    }

    /**
     * Show the application dashboard.
     *
     * @return \Illuminate\Contracts\Support\Renderable
     */
    public function admin_dashboard()
    {
        return view('cp/dashboard');
    }

    public function restaurant_dashboard()
    {
        return view('restaurant/dashboard');
    }

    public function delivery_dashboard()
    {
        return view('delivery/dashboard');
    }

    public function index()
    {
        return view('home');
    }

0 个答案:

没有答案