我的应用程序中有4种类型的用户,详细信息存储在4个不同的表中,那么如何实现Laravel的身份验证?
Route::post('/adlogin', 'mainController@adminlogin');
Route::get('/sellerlogin', function () {
return view('seller.pages.login');
});
Route::post('/sellerlog_in', 'mainController@sellerlogin');
答案 0 :(得分:5)
<?php
namespace App\Http\Controllers\Auth;
use Illuminate\Http\Request;
use App\Http\Controllers\Controller;
//Use Dependencies
use Auth;
class AdminLoginController extends Controller
{
此处使用的中间件是
中创建admin
,您必须在config/auth.php
public function __construct()
{
$this->middleware('guest:admin', ['except'=>'logout']);
}
为您的管理员登录创建视图
public function showLoginForm()
{
return view('auth.admin_login');
}
public function login(Request $request)
{
//Validate the Form Data
$this->validate($request, [
'email'=>'required|email',
'password'=>'required|min:5'
]);
//Attempt to log the Admin In
$email= $request->email;
$password= $request->password;
$remember= $request->remember;
成功登录后,您想重定向到这里,我正在重定向 为
admin.dashboard
//If Successful redirect to intended location
if (Auth::guard('admin')->attempt(['email' => $email, 'password' => $password], $remember)) {
return redirect()->intended(route('admin.dashboard'));
}
//If Unsuccessful redirect back to login form with form data
return redirect()->back()->withInput($request->only('email', 'remember'));
}
/**
* Log the Admin out of the application.
*
* @param \Illuminate\Http\Request $request
* @return \Illuminate\Http\Response
*/
public function logout()
{
Auth::guard('admin')->logout();
return redirect()->route('admin.login');
}
}
注销后重定向回登录页面
确保您对正确的用户使用了正确的
guard
。为更多用户类型创建相同的功能,为他们创建guards
。
在config/app.php
'guards' => [
'web' => [
'driver' => 'session',
'provider' => 'users',
],
'api' => [
'driver' => 'token',
'provider' => 'users',
],
'admin' => [
'driver' => 'session',
'provider' => 'admins',
],
'admin-api' => [
'driver' => 'token',
'provider' => 'admins',
],
],
此处创建的admin
和admin-api
为您的用户类型
添加提供商
'providers' => [
'users' => [
'driver' => 'eloquent',
'model' => App\User::class,
],
'admins' => [
'driver' => 'eloquent',
'model' => App\Admin::class,
],
],
最后重置密码使用此功能。
'passwords' => [
'users' => [
'provider' => 'users',
'table' => 'password_resets',
'expire' => 60,
],
'admins' => [
'provider' => 'admins',
'table' => 'password_resets',
'expire' => 10,
],
],
在http/middleware/redirectIfAuthenticated.php
中将此添加到您的handle
功能
//Check for admin login guard
switch ($guard) {
case 'admin':
if (Auth::guard($guard)->check()) {
return redirect()->route('admin.dashboard');
}
break;
default:
if (Auth::guard($guard)->check()) {
return redirect('/dashboard');
}
break;
}
您可以为您的用户类型添加更多案例。
LoginController
或使用逻辑将其登录。guards
RedirectIfAuthenticated
答案 1 :(得分:0)
您可以使用laravel-permission包。此包允许您管理数据库中的用户权限和角色。