How to retrieve role/status of authenticated user?

被刻印的时光 ゝ 提交于 2019-12-11 07:33:27

问题


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.

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');

回答1:


In LoginController, remove the following line.

protected $redirectTo = '/home'; 

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'; 
    }
}


来源:https://stackoverflow.com/questions/55442071/how-to-retrieve-role-status-of-authenticated-user

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!