如何在Laravel中对自定义模型使用身份验证 [英] How to use authentication on custom model in Laravel
问题描述
我正在为laravel中的用户创建新的自定义模型.我正在为将要使用的一种类型的用户使用默认的User
laravel模型,并为其他类型的用户使用新的Merchant
模型.
I'm making new custom model for user in laravel. I'm using the default User
laravel model for one type of users that I will have, and new Merchant
model for other type of users.
我在register
视图中做出选择选项,以选择要注册哪种类型的用户,以便在控制器中进行更好的控制.
I make select option in the register
view for chosing which type of user will be registered for better control in the controller.
<select id="user_type" name="user_type">
<option value="user">User</option>
<option value="merchant">Merchant</option>
</select>
这是我为两种类型的用户修改的默认RegisterController
:
This is my modified the default RegisterController
for both type of users:
<?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
中不是默认设置,所以我放置了该函数,因为我已经查看了该解决方案
To mention that register
function is not by default in register controller
, so I put that function because I already view that solution How to register more than one type of users and how to make multi auth in laravel 5.3 but I don't know how to modify that.
但是,现在的问题是我提交注册表后的身份验证.如果我注册了新的user
,它就可以正常工作,但是如果我注册了merchant
,它会将用户保存在数据库中,之后,如果users
中不存在任何user
,它就不会登录到商家.桌子.但是,如果我在注册merchant
后在users
中已经存在任何user
,它将登录在users
表中创建的最后一个user
中.
But the problem now is with the authentication after I submit the registration form. If I register new user
it's working fine, but if I register merchant
it saved the user in database and after that it doesn't log in the merchant if it doesn't exist any user
in the users
table. But if it already exist any user
in the users
after I register the merchant
it log in the last user
which is created in users
table.
所以我的问题是如何为商家用户修改身份验证.
So my question is how to modify the authentication for merchant user.
谢谢!
更新:
注册功能
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
Add a guard merchant
to this array with provider
value of 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,
],
],
使用Merchant
模型的model
值添加merchants
提供程序
Add merchants
provider with model
value of your Merchant
model
'providers' => [
'users' => [
'driver' => 'eloquent',
'model' => App\User::class,
],
'merchants' => [
'driver' => 'eloquent',
'model' => App\Merchant::class,
],
],
现在,您只需通过guard('web')
验证普通用户,并通过guard('merchant')
Now you can simply authenticate normal users via guard('web')
and merchant users via 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
For example there's an awesome package: https://github.com/Sarav-S/Laravel-Multiauth
它将帮助您处理所有困难的事情.
It will help you to handle all that hard stuff.
这篇关于如何在Laravel中对自定义模型使用身份验证的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!