[英]How to Authenticate a user from a table rather than 'users' table in Laravel 5.1?
I am new to Laravel, and I am trying to authenticate a user from players table. 我是Laravel的新手,我正在尝试从玩家表中验证用户身份。
As we know Auth::attempt
is used to authenticate a user, and by default it works for users table, and for users table it is working perfect. 众所周知,
Auth::attempt
用于对用户进行身份验证,默认情况下,它适用于users表,对于users表则完美。 But now I want to authenticate another user (a player) from another table (players), but I am unable to find a solution. 但是现在我想从另一个表(玩家)对另一个用户(玩家)进行身份验证,但是我找不到解决方案。
I had the same requirement. 我有同样的要求。 I had two user tables
users
(for main website) and admins
(for admin panel). 我有两个用户表
users
(用于主网站)和admins
(用于管理面板)。 For that to happen, I must authenicate admin
user against admins
table. 为此,我必须针对
admins
表对admin
用户进行身份验证。 I wanted to use stock authentication library. 我想使用股票认证库。 So, I made following middleware.
因此,我制作了以下中间件。
Of course I had separate login form for admin panel 当然,我有单独的管理面板登录表单
<?php namespace App\Http\Middleware;
use Closure;
class ChangeUserToAdmin
{
/**
* Handle an incoming request.
*
* @param \Illuminate\Http\Request $request
* @param \Closure $next
* @return mixed
*/
public function handle($request, Closure $next)
{
\Config::set('auth.table', 'admins');
\Config::set('auth.model', 'App\DB\Admin\Admin');
\Config::set('session.cookie', 'admin_session');
\Config::set('session.path', '/admin/');
return $next($request);
}
}
All my routes within admin
route group (ie domain.com/admin/**
) were protected by this middleware. 我在
admin
路由组中的所有路由(即domain.com/admin/**
)都受到此中间件的保护。 So essentially I changed authentication model and table for admin
area. 因此,基本上我更改了
admin
区域的身份验证模型和表。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.