Authenticate users from more than two tables in laravel 5(在laravel 5 中对来自两个以上表的用户进行身份验证)
问题描述
据我所知,Auth::attempt
用于对 users
表中的用户进行身份验证,但我想对 managers
表中的其他用户进行身份验证和 admins
表中的 admin.我知道已经存在 laravel-multiauth
插件.但是我们可以创建自己的 AuthServiceProvider
来对来自多个表的用户进行身份验证吗?
As I know Auth::attempt
is used to authenticate users from users
table, but i want to authenticate another users from managers
table and admin from admins
table. I know there are laravel-multiauth
plugin already exist. But can we create our own AuthServiceProvider
for authenticating users from multiple tables..?
推荐答案
首先在 IlluminateFoundationAuth
中创建 Admin Authenticatable 就像
First create Admin Authenticatable in IlluminateFoundationAuth
like
<?php
namespace IlluminateFoundationAuth;
use IlluminateAuthAuthenticatable;
use IlluminateDatabaseEloquentModel;
use IlluminateAuthPasswordsCanResetPassword;
use IlluminateFoundationAuthAccessAuthorizable;
use IlluminateContractsAuthAuthenticatable as AuthenticatableContract;
use IlluminateContractsAuthAccessAuthorizable as AuthorizableContract;
use IlluminateContractsAuthCanResetPassword as CanResetPasswordContract;
class Admin extends Model implements
AuthenticatableContract,
AuthorizableContract,
CanResetPasswordContract
{
use Authenticatable, Authorizable, CanResetPassword;
}
然后通过扩展Authenticatable
管理模型来创建管理模型:-
Then create Admin Model by extending Authenticatable
Admin Model :-
<?php
namespace App;
use IlluminateFoundationAuthAdmin as Authenticatable;
class Admin extends Authenticatable
{
/**
* The attributes that are mass assignable.
*
* @var array
*/
protected $fillable = [
'name', 'email', 'password',
];
/**
* The attributes that should be hidden for arrays.
*
* @var array
*/
protected $hidden = [
'password', 'remember_token',
];
}
之后你需要像下面这样修改config/auth.php
添加提供者数组
After that you need to modify config/auth.php
like below
Add in providers array
'admins' => [
'driver' => 'eloquent',
'model' => AppAdmin::class,
],
并加入guards数组.
'user' => [
'driver' => 'session',
'provider' => 'users',
],
'admin' => [
'driver' => 'session',
'provider' => 'admins',
],
现在从用户表进行身份验证
if (Auth::guard('user')->attempt(['email' => $email, 'password' => $password])) {
$details = Auth::guard('user')->user();
$user = $details['original'];
return $user;
} else {
return 'auth fail';
}
并从Admin表进行身份验证
if (Auth::guard('admin')->attempt(['email' => $email, 'password' => $password])) {
$details = Auth::guard('admin')->user();
$user = $details['original'];
return $user;
} else {
return 'auth fail';
}
这篇关于在laravel 5 中对来自两个以上表的用户进行身份验证的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:在laravel 5 中对来自两个以上表的用户进行身份验
- 如何在 Symfony2 中正确使用 webSockets 2021-01-01
- 如何从数据库中获取数据以在 laravel 中查看页面? 2022-01-01
- openssl_digest vs hash vs hash_hmac?盐与盐的区别HMAC? 2022-01-01
- PHP - if 语句中的倒序 2021-01-01
- 使用 GD 和 libjpeg 支持编译 PHP 2022-01-01
- PHP foreach() 与数组中的数组? 2022-01-01
- Laravel 5:Model.php 中的 MassAssignmentException 2021-01-01
- 如何使用 Google API 在团队云端硬盘中创建文件夹? 2022-01-01
- Oracle 即时客户端 DYLD_LIBRARY_PATH 错误 2022-01-01
- 覆盖 Magento 社区模块控制器的问题 2022-01-01