如何获取已认证用户的角色/状态? [英] How to retrieve role/status of authenticated user?
问题描述
我是Laravel的新手.我使用下面代码部分给出的代码行在用户表中添加了一个名为status的附加字段.我正在尝试学习如何根据用户的角色来重定向用户.
I am new to Laravel. I have included an additional field in the user's table named status using the line of code given in the code part below. I am trying to learn how to redirect the user based on their roles.
我知道可以使用$user = Auth::user();
来检索经过身份验证的用户.我的问题是,如何获取要在打算编写的redirectTo
函数中使用的经过身份验证的用户的状态.
I understand that to retrieve an authenticated user I can use $user = Auth::user();
My question is how I can get the status of the authenticated user to be used in a redirectTo
function that I plan to write.
// This is how I have added the user status field:
$table->enum('status',['user', 'admin', 'disabled'])->default('user');
推荐答案
在LoginController中,删除以下行.
In LoginController, remove the following line.
protected $redirectTo = '/home';
然后将新方法redirectTo()
添加到LoginController. redirectTo()
方法优先于redirectTo
属性.
Then add a new method redirectTo()
to the LoginController. The redirectTo()
method takes precedence over the redirectTo
property.
public function redirectTo() {
$role = auth()->user()->status;
// Check user status/role
switch ($role) {
case 'admin':
return '/admin';
case 'user':
return '/user';
default:
return '/login';
}
}
这篇关于如何获取已认证用户的角色/状态?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!