Auth Guard仅在laravel中默认工作

时间:2019-06-26 18:58:38

标签: laravel authentication guard

我是laravel的新手,所以请多多包涵。我正在尝试使用一种登录表单来实现多重身份验证,但是由于auth Guard仅适用于auth.php文件中的默认值,因此我遇到了一些麻烦。我在这里尝试了很多解决方案,但是它们似乎没有用。我能做什么?

我的auth.php文件

<?php

return [

'defaults' => [
    'guard' => 'web',
    'passwords' => 'users',
],

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

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

    'employee' => [
        'driver' => 'session',
        'provider' => 'employees',
    ],
],

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


    'employees' => [
        'driver' => 'eloquent',
        'model' => App\Employee::class,
    ],

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





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


'passwords' => [
    'users' => [
        'provider' => 'users',
        'table' => 'password_resets',
        'expire' => 60,
    ],
    'employees' => [
        'provider' => 'employees',
        'table' => 'password_resets',
        'expire' => 60,
    ],
 ],

]; 

我的LoginController

<?php

namespace App\Http\Controllers\Auth;

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

class LoginController extends Controller
{

use AuthenticatesUsers;


protected $redirectTo = '/dashboard';



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

AuthenticateUsers特性内的我的AttemptLogin函数

protected function attemptLogin(Request $request)
{

    $userAttempt = $this->guard()->attempt(
            $this->credentials($request), $request->filled('remember')
    );
    if(!$userAttempt){
        $this->checkUser = "employee";
        return $this->employeeGuard()->attempt(
            $this->credentials($request), $request->filled('remember')
    );

    }
    return $userAttempt;

}

我的模特     

namespace App;


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



class Employee extends Authenticatable
{
use Notifiable;

    protected $guard = "employees";


/**
 * The attributes that are mass assignable.
 *
 * @var array
 */
protected $fillable = [
    'firstname', 'surname', 'DOB', 'gender', 'mobile_number', 
'job_title','job_sector', 
    'highest_qualification', 'experience', 'short_description', 
'relocation_willingness', 
    'salary_range', 'city', 'cv', 'five_tag_summation', 'email', 
'password',
];


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



protected $casts = [
    'email_verified_at' => 'datetime',
];
}

我的路线

 Route::get('/', 'PagesController@index');

Route::get('/about', 'PagesController@about');

Route::get('/services', 'PagesController@services');

Route::get('/blog', 'PagesController@blog');

Route::resource('posts', 'PostsController');

Route::resource('jobs', 'JobsController');

Route::get('/users/{id}/{name}', function ($id, $name) {
    return "This is user ".$id." with the name ".$name;
});

Route::get('/employeeRegister', 'Auth\EmployeeRegController@showRegistration');


Route::post('/employeeRegister', 'Auth\EmployeeRegController@register')->name('employeeRegister');

Auth::routes();


Route::get('/dashboard', 'DashboardController@index');

0 个答案:

没有答案