Laravel 5.3 登录重定向到多个用户的不同页面

2021-12-18 00:00:00 php laravel laravel-5.3

我的 Laravel 5.3 有三种不同类型的用户.我希望他们在登录后被重定向到不同的仪表板页面.例如:

I have Laravel 5.3 with three different types of users. I want them to be redirected to different dashboard pages after logging in. For example:

用户 ->登录 ->用户仪表板

user -> login -> user-dashboard

管理员 ->登录 ->管理仪表板

admin -> login -> admin-dashboard

我创建了一个名为 CheckRole 的中间件:

I have created a middleware called CheckRole:

public function handle($request, Closure $next)
{
    if($request->user() === null) {
    return response("Insufficient Permissions" , 401);
    }
    $actions = $request->route()->getAction();
    $roles = isset($actions['roles']) ? $actions['roles'] : null;

    if($request->user()->hasAnyRole($roles) || !$roles) {
            return $next($request);
        }
    return response("Insufficient Permissions" , 401);

}

路线

Route::group(['middleware' => ['auth','roles'], 'roles' => 'Admin'],  function () { 
    // Routes here
}

角色运行良好.

现在 redirectTo='';LoginContoller 中只指向一个视图.我检查了文档,我相信这与没有解释如何设置它的守卫有关.

Now redirectTo= ''; in the LoginContoller points to one view only. I have checked the documentation and I believe this has something to do with guards which have no explanation on how to set it up.

我也见过多重身份验证,但我认为为不同的用户创建不同的表并因此寻找替代答案是不明智的.

I have also seen multiauth, but I do not think it is wise to create different tables for different users and hence looking for an alternate answer.

任何建议将不胜感激.

我的表是这样的:

Table users

id | name | email
---------
1  | John | john@blah.com
2  | Michael | michael@blah.com

Table roles

id | name
---------
1  | Admin
2  | PrivilegedMember
3  | Subscriber

Table user_role

id | user_id | role_id
----------------------
1  |    1    |    1   
2  |    2    |    2

这可能与以下问题重复,但提供的答案没有解释多次重定向.

This might be a duplicate of the below question but the answer provided leaves without explaining multiple redirections.

Laravel 5.3 中的多重身份验证

推荐答案

在你的 LoginController 中实现一个 authenticated() 方法并在那里添加重定向逻辑:

Implement an authenticated() method in your LoginController and add the redirection logic there:

<?php

namespace AppHttpControllersAuth;

use AppHttpControllersController;
use IlluminateFoundationAuthAuthenticatesUsers;

class LoginController extends Controller
{
    use AuthenticatesUsers;

    // ...

    /**
     * The user has been authenticated.
     *
     * @param  IlluminateHttpRequest  $request
     * @param  mixed  $user
     *
     * @return mixed
     */
    protected function authenticated(Request $request, $user)
    {
        if($user->hasRole('Admin')) {
            return redirect()->intended('admin');
        } 

        if ($user->hasRole('PrivilegedMember')) {
            return redirect()->intended('PriviligedMember/index');
        }
    }

    // ...
}

该方法在用户通过身份验证后调用.查看sendLoginResponse的最后两行:

The method is called after the user is authenticated. See the last two lines of sendLoginResponse:

/**
 * Send the response after the user was authenticated.
 *
 * @param  IlluminateHttpRequest  $request
 *
 * @return IlluminateHttpResponse
 */
protected function sendLoginResponse(Request $request)
{
    $request->session()->regenerate();

    $this->clearLoginAttempts($request);

    return $this->authenticated($request, $this->guard()->user())
            ?: redirect()->intended($this->redirectPath());
}

因此它是此类逻辑的完美候选.

So it's a perfect candidate for such logics.

关于您自己的答案的另一个注意事项,AuthenticatesUser 是水平扩展 LoginController 的特征,您可以安全地覆盖控制器中的任何方法,而无需触及核心文件.

One other note on your own answer, the AuthenticatesUser is a trait that horizontally extends the LoginController, you can safely override any of its methods in your controller without touching the core files.

相关文章