使用管理员角色进行laravel多重身份验证

时间:2019-02-11 11:17:03

标签: laravel

我的应用程序中有两种类型的用户。 “管理员”和“前端用户”。 “管理员”具有角色。我想根据其角色重定向管理员。我已经完成了multiauth系统,但不知道如何根据管理员的角色重定向。

这是管理员模型->

<?php

namespace App;

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

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',
    ];
    // public function post(){
    //     return $this->hasOne('App\Post');
    // }

    public function role()
    {
        return $this->belongsTo('App\Role');
    }
}

这是榜样->

<?php

namespace App;

use Illuminate\Database\Eloquent\Model;

class Role extends Model
{
    public function admins()
    {
        return $this->hasMany('App\Admin');
    }
}

这是管理员的登录控制器->

<?php

namespace App\Http\Controllers\Auth;

use Illuminate\Http\Request;
use App\Http\Controllers\Controller;
use Illuminate\Foundation\Auth\AuthenticatesUsers;

use Auth;

use App\Admin;
use App\Role;

class BackendLoginController extends Controller
{
    use AuthenticatesUsers;

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

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

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

        if(Auth::guard('admin')->attempt(['email' => $request->email, 'password' => $request->password], $request->remember))
        {
            return redirect()->intended(route('dashboard'));
        }

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

    public function logout()
    {
        Auth::guard('admin')->logout();
        return redirect('/');
    }
}

这是用户的登录控制器->

<?php

namespace App\Http\Controllers\Auth;

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

use Auth;

class LoginController extends Controller
{
    /*
    |--------------------------------------------------------------------------
    | Login Controller
    |--------------------------------------------------------------------------
    |
    | This controller handles authenticating users for the application and
    | redirecting them to your home screen. The controller uses a trait
    | to conveniently provide its functionality to your applications.
    |
    */

    use AuthenticatesUsers;

    /**
     * Where to redirect users after login.
     *
     * @var string
     */
    protected $redirectTo = '/home';

    /**
     * Create a new controller instance.
     *
     * @return void
     */
    public function __construct()
    {
        $this->middleware('guest', ['except' => ['logout', 'userLogout']]);
    }

    public function userLogout()
    {
        Auth::guard('web')->logout();
        return redirect('/');
    }
}

路线->

Route::get('/backend-login', 'Auth\BackendLoginController@showLoginForm')->name('backend.login');
Route::post('/backend-login', 'Auth\BackendLoginController@login')->name('backend.login.submit');
Route::get('/dashboard', 'BEController@index')->name('dashboard');
Route::get('/logout', 'Auth\BackendLoginController@logout')->name('backend.logout');

我已经做好了警卫并进行了多重身份验证,但无法本地设置角色基础系统

0 个答案:

没有答案