gpt4 book ai didi

laravel - 如何在Laravel中对自定义模型使用身份验证

转载 作者:行者123 更新时间:2023-11-30 23:53:36 25 4
gpt4 key购买 nike

我正在为laravel中的用户制作新的自定义模型。我正在为将要使用的一种类型的用户使用默认的User laravel模型,并为其他类型的用户使用新的Merchant模型。

我在register View 中进行选择,以选择要注册哪种类型的用户,以便在 Controller 中进行更好的控制。

<select id="user_type" name="user_type">
<option value="user">User</option>
<option value="merchant">Merchant</option>
</select>

这是我为两种类型的用户修改的默认 RegisterController:
<?php

namespace App\Http\Controllers\Auth;

use App\User;
use App\Merchant;
use Validator;
use App\Http\Controllers\Controller;
use Illuminate\Foundation\Auth\RegistersUsers;

use Illuminate\Http\Request;
use App\Http\Requests;
use Auth;

class RegisterController extends Controller
{
/*
|--------------------------------------------------------------------------
| Register Controller
|--------------------------------------------------------------------------
|
| This controller handles the registration of new users as well as their
| validation and creation. By default this controller uses a trait to
| provide this functionality without requiring any additional code.
|
*/

use RegistersUsers;

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

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

/**
* Get a validator for an incoming registration request.
*
* @param array $data
* @return \Illuminate\Contracts\Validation\Validator
*/
public function register(Request $request)
{
$validator = $this->validator($request->all());

if ($validator->fails()) {
$this->throwValidationException(
$request, $validator
);
}

//Add custom code here
$new_user = $this->create($request->all());
//Add custom code here

Auth::guard($this->getGuard())->login($new_user);

return redirect($this->redirectPath());
}

protected function validator(array $data)
{
if($data['user_type']=='user'){
return Validator::make($data, [
'name' => 'required|max:255',
'email' => 'required|email|max:255|unique:users',
'password' => 'required|min:6|confirmed',
'first_name' => 'required|max:255',
'last_name' => 'required|max:255',
'user_type' =>'required'
]);
}else{
return Validator::make($data, [
'name' => 'required|max:255',
'email' => 'required|email|max:255|unique:users',
'password' => 'required|min:6|confirmed',
'first_name' => 'required|max:255',
'last_name' => 'required|max:255',
'user_type' =>'required'
]);
}
}

/**
* Create a new user instance after a valid registration.
*
* @param array $data
* @return User
*/
protected function create(array $data)
{
if($data['user_type']=='user'){
return User::create([
'name' => $data['name'],
'email' => $data['email'],
'password' => bcrypt($data['password']),
'first_name' => $data['first_name'],
'last_name' => $data['last_name'],
]);
}else{
return Merchant::create([
'name' => $data['name'],
'email' => $data['email'],
'password' => bcrypt($data['password']),
'first_name' => $data['first_name'],
'last_name' => $data['last_name'],
]);
}

}

}

提到 register函数默认不是 register controller,因此我放了该函数,因为我已经查看了解决方案 How to register more than one type of users and how to make multi auth in laravel 5.3,但是我不知道如何修改它。

但是现在的问题是我提交注册表后的身份验证。如果我注册新的 user可以正常工作,但是如果我注册 merchant,它将用户保存在数据库中,此后如果 user表中不存在任何 users,它就不会登录商人。但是,如果在我注册了 userusers中已经存在任何 merchant,它将登录到在 user表中创建的最后一个 users中。

所以我的问题是如何为商家用户修改身份验证。

谢谢!

更新:

注册功能
public function register(Request $request)
{
if($request->user_type =='user'){
$auth = auth()->guard();
} else{
$auth = auth()->guard('merchant');
}

$user = $this->create($request->all());
auth()->login($user);

return redirect($this->redirectPath());


}

最佳答案

  • 转到/config/auth.php
  • 查找数组的guards元素
    'guards' => [
    'web' => [
    'driver' => 'session',
    'provider' => 'users',
    ],

    'api' => [
    'driver' => 'passport',
    'provider' => 'users',
    ],
    ],
  • 向此数组添加一个保护merchant,其provider值为 merchants
    'guards' => [
    'web' => [
    'driver' => 'session',
    'provider' => 'users',
    ],

    'api' => [
    'driver' => 'passport',
    'provider' => 'users',
    ],

    'merchant' => [
    'driver' => 'session',
    'provider' => 'merchants',
    ],
    ],
  • 现在找到此配置的providers元素
    'providers' => [
    'users' => [
    'driver' => 'eloquent',
    'model' => App\User::class,
    ],
    ],
  • 使用您的merchants模型的model值添加Merchant提供程序
    'providers' => [
    'users' => [
    'driver' => 'eloquent',
    'model' => App\User::class,
    ],

    'merchants' => [
    'driver' => 'eloquent',
    'model' => App\Merchant::class,
    ],
    ],
  • 现在您可以简单地通过guard('web')对普通用户进行身份验证,并通过guard('merchant')对商家用户进行身份验证

  • 为您注册函数
    public function register(Request $request)
    {
    $guard = $request->user_type == 'user' ? 'web' : 'merchant';
    $user = $this->create($request->all());

    auth()->guard($guard)->login($user);

    return redirect($this->redirectPath());
    }

    您也可以使用软件包...

    例如,有一个很棒的软件包: https://github.com/Sarav-S/Laravel-Multiauth

    它将帮助您处理所有困难的事情。

    关于laravel - 如何在Laravel中对自定义模型使用身份验证,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40993143/

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