gpt4 book ai didi

php - 多个用户的 Laravel 身份验证

转载 作者:行者123 更新时间:2023-12-04 14:48:49 24 4
gpt4 key购买 nike

我的应用程序中有 4 种类型的用户,详细信息存储在 4 个不同的表中,那么如何实现 Laravel 的身份验证?

Route::post('/adlogin', 'mainController@adminlogin');
Route::get('/sellerlogin', function () {
return view('seller.pages.login');
});

Route::post('/sellerlog_in', 'mainController@sellerlogin');

最佳答案

使用它作为你的 Controller

<?php

namespace App\Http\Controllers\Auth;

use Illuminate\Http\Request;
use App\Http\Controllers\Controller;

//Use Dependencies
use Auth;

class AdminLoginController extends Controller
{

Middleware used here is admin which you have to create in config/auth.php


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

Create a view for your admin Login


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;

After Successfully login where you want to redirect like here I am redirecting toadmin.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');
}
}

After logout redirect back to login page

Make sure you are using right guard for right User. Create same functionality for more user types as you want create guards for them.



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',
],
],

adminadmin-api在此处为您的用户类型创建

Add providers


'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对于 auth.php 中的每个用户类型
  • 将您的案例添加到 RedirectIfAuthenticated
  • 关于php - 多个用户的 Laravel 身份验证,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49442305/

    24 4 0
    Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
    广告合作:1813099741@qq.com 6ren.com