第一个身份验证系统运行良好,其代码在下面,需要使用laravel 5.5使用两个不同表的不同用户使用
<?php
namespace App\Http\Controllers;
use Illuminate\Http\Request;
use App\User;
use Illuminate\Support\Facades\Auth;
class StudentController extends Controller
{
public function Register(Request $request)
{
$firstname = $request['firstname'];
$othername = $request['othername'];
$email = $request['email'];
$password = $request['password'];
$user = new User();
$user->firstname = $firstname;
$user->othername = $othername;
$user->email = $email;
$user->password = $password;
$user->save();
Auth::login($user);
return redirect()->route('studentDashboard');
}
public function Login(Request $request)
{
if(Auth::attempt(['email'=> $request['email'], 'password'=>
$request['password']]))
{
return redirect()->route('studentDashboard');
}
return redirect()->back();
}
}
我重复了上述操作以为其他用户创建auth。可以进行注册,但是即使登录数据正确也无法登录,它会在if语句后返回重定向
<?php
namespace App\Http\Controllers;
use Illuminate\Http\Request;
use App\Employer;
use Illuminate\Support\Facades\Auth;
class EmployerController extends Controller
{
public function createEmployerAccount(Request $request)
{
$companyName = $request['companyname'];
$companyEmail = $request['email'];
$companyPasword = $request['password'];
$Employer = new Employer();
$Employer->companyname = $companyName;
$Employer->email = $companyEmail;
$Employer->password = $companyPasword;
$Employer->save();
Auth::login($Employer);
return redirect()->route('employersDashboard');
}
public function signInEmployer(Request $request)
{
if(Auth::attempt(['email'=>$request['email'],
'password'=>$request['password']]))
{
return redirect()->route('employersDashboard');
}
return redirect()->back();
}
}
当我尝试将“电子邮件”更改为“电子邮件”时,显示错误->选择查询来自用户表而不是我需要从中获取数据的雇主表,并且当我将“密码”更改为“密码”,显示错误“未定义的索引密码”
这是路由文件的内容
Route::get('/',function(){
return view('pages.index');
})->name('home');
Route::post('/signup',[
'uses'=>'StudentController@Register',
'as'=> 'signup'
]);
Route::post('/signin',[
'uses'=>'StudentController@Login',
'as'=>'signin'
]);
Route::get('/employers',[
'uses'=>'PageController@employersPage',
'as'=>'employers'
]);
Route::get('/studentDashboard',[
'uses'=>'PageController@getStudentDashboard',
'as'=> 'studentDashboard'
]);
Route::post('/createcompany',[
'uses'=>'EmployerController@createEmployerAccount',
'as'=>'createcompany'
]);
Route::post('/signInEmployer',[
'uses'=>'EmployerController@signInEmployer',
'as'=>'signInEmployer'
]);
Route::get('/employersDashboard',[
'uses'=>'PageController@getEmployersDashboard',
'as'=> 'employersDashboard',
'middleware'=>'auth:employer'
]);
Route::post('/createPost',[
'uses'=>'PostController@postCreatePost',
'as'=> 'createPost'
]);
答案 0 :(得分:1)
您需要告诉Auth
在登录Guard
时使用不同的Employer
进行身份验证。为Employer
定义config/auth.php
更改的保护措施。
在guards
中查找auth.php
部分,然后添加新的后卫
'guards' => [
'web' => [
'driver' => 'session',
'provider' => 'users',
],
'employer' => [
'driver' => 'session',
'provider' => 'employers',
],
'api' => [
'driver' => 'passport',
'provider' => 'users',
],
],
现在在同一文件中有一个providers
节。您需要添加employers
提供者
'providers' => [
'users' => [
'driver' => 'eloquent',
'model' => App\Models\User::class,
],
//Employer provider
'employers' => [
'driver' => 'eloquent',
'model' => App\Employer::class,
],
],
创建自定义身份验证中间件
namespace App\Http\Middleware;
use Closure;
use Auth;
class AuthenticateEmployer
{
public function handle($request, Closure $next)
{
//If request does not comes from logged in employer
//then he shall be redirected to employer Login page
if (!Auth::guard('employer')->check()) {
return redirect('/signInEmployer');
}
return $next($request);
}
}
在routeMiddleware
的Kernal.php中注册自定义身份验证中间件
'employerAuth' => \App\Http\Middleware\AuthenticateEmployer::class,
现在,我们已经设置了自定义guard
和自定义中间件employerAuth
EmployerController
class EmployerController extends Controller
{
//either you have to define this or you can use `Auth::guard('employer')->attempt($credentials)` in login
protected function guard()
{
return Auth::guard('employer');
}
public function signInEmployer(Request $request)
{
if(Auth::attempt(['email'=>$request['email'],
'password'=>$request['password']]))
{
return redirect()->route('employersDashboard');
}
return redirect()->back();
}
}
对于受Employer
auth保护的所有路由,您都需要像这样在路由中添加中间件employerAuth
或在每个控制器employerAuth
中添加construct
public function __construct()
{
$this->middleware('employerAuth');
}
希望对您有帮助。有关详细信息,您可以检查此https://laravel.com/docs/5.6/authentication#authenticating-users
检查这个不错的示例应用程序是否有多重身份验证应用程序https://github.com/yskoverride/Various2.0/tree/master/app