如果在Laravel的内置身份验证中经过身份验证的逻辑重定向?

问题描述 投票:10回答:6

之前已经问过这个问题,我相信我的代码是正确的,但我的行为很奇怪。

我需要在登录后将用户重定向到不同的路由,具体取决于某些数据库值。我认为为了做到这一点,我只需将我的逻辑放在handleapp/Http/Middleware/RedirectIfAuthenticated.php方法中。我的方法目前看起来像这样:

public function handle($request, Closure $next)
{

    if ($this->auth->check()) {
        if($this->auth->user()->sign_up_complete == 1){
            return redirect('/');
        } else {
            if($this->auth->user()->step_one_complete == 0){
                return redirect('/register/step-1');
            } elseif($this->auth->user()->step_two_complete == 0){
                return redirect('/register/step-2');
            } else {
                return redirect('/');
            }
        }
    }

    return $next($request);
}

这不起作用,登录后用户被重定向到/home。我曾尝试将dd($this->auth->user())置于$this->auth->check()条件下,但它永远不会被运行。如果我将它放在该检查之外,那么它就会在每次请求时运行。看起来$this->auth->check()永远不会运行。

我的问题:如果不在这里,这个逻辑应该去哪里?

我也从protected $redirectTo = '/account';控制器中删除了AuthController.php

php laravel authentication laravel-5.1
6个回答
5
投票

您没有正确使用中间件。每当您登录时发送请求,这段代码就会触发。

要在登录后更改重定向位置,您可以覆盖AuthController中的redirectPath()方法。 (你可以在vendor/laravel/framework/src/Illuminate/Foundation/Auth/RedirectsUsers.php找到原始方法)

这看起来像这样:

...

public class AuthController extends Controller {

    ...

    public function redirectPath()
    {
        if(Auth::user()->sign_up_complete == 1) {
            return '/';
        } else {
            if(Auth::user()->step_one_complete == 0) {
                return '/register/step-1';
            } elseif(Auth::user()->step_two_complete == 0) {
                return '/register/step-2';
            } else {
                return '/';
            }
        }
    }


    // The rest of the class implementation.

}

注意:我已经用Facade替代品($this->auth())替换了Auth::方法。仅仅因为我不确定AuthController是否有auth()方法。


4
投票

高级答案:RedirectIfAuthenticated的目的是让已经过身份验证的用户无法访问登录或注册路由/视图,因为他们已经登录。

测试:为登录视图添加书签。然后登录。关闭浏览器或窗口。打开登录书签。您将直接进入用户家或RedirectIfAuthenticated指定的地方。

出于LoginController的目的,创建一个redirecTo()方法,这是redirectPath()方法查找是否已自定义重定向的方法。

// example
public function redirectTo()
{
    switch (auth()->user()->role) {
        case 'foo':
            return route('foo.home');

        case 'bar':
            return route('bar.home');

        default:
            auth()->logout();
            return route('web.welcome');
    }
}

2
投票

我认为设置一个自定义中间件类来根据数据库值验证您的请求非常简单,我这样做是为了排除没有正确角色的用户。

该角色在我的用户表中定义,只允许具有管理员角色的用户访问系统。

namespace App\Http\Middleware;

use Closure;
use Illuminate\Support\MessageBag;

class RolesMiddleware
{
    /**
     * Handle an incoming request.
     *
     * @param  \Illuminate\Http\Request  $request
     * @param  \Closure  $next
     * @return mixed
     */
    public function handle($request, Closure $next)
    {
        // If a user is authenticated
        if(\Auth::user() != null)
        {
            // If the user doesn't have the correct role
            if(\Auth::user()->role != 'administrator')
            {
                // logout the user
                \Auth::logout();

                // create a new MessageBag
                $messageBag = new MessageBag;

                // add a message
                $messageBag->add('not_allowed', 'You are not allowed to login, because you do not have the right role!');

                // redirect back to the previous page with errors
                return \Redirect::to('login')->withErrors($messageBag);
            }
        }

        return $next($request);

    }
}

2
投票

解决方案是Mark Walet的答案,但需要稍加修正。返回应该是一个字符串:

public class AuthController extends Controller {

    ...

    public function redirectPath()
    {
        if(Auth::user()->sign_up_complete == 1) {
            return '/';
        } else {
            if(Auth::user()->step_one_complete == 0) {
                return '/register/step-1';
            } elseif(Auth::user()->step_two_complete == 0) {
                return '/register/step-2';
            } else {
                return '/';
            }
        }
    }


    // The rest of the class implementation.

}

0
投票

你不能改变laravel的核心文件,你需要做的就是将这段代码添加到Auth \ AuthController中

protected $redirectPath = '/dashboard';

0
投票

要了解为什么永远不会达到路由逻辑,您应该查看注册了RedirectIfAuthenticated中间件的app/Http/Kernel.php

protected $routeMiddleware = [
    ...
    'guest' => \App\Http\Middleware\RedirectIfAuthenticated::class,
    ...
];

这意味着如果用户导航到不受guest路由中间件保护的路由,则该请求永远不会通过RedirectIfAuthenticated类,因此完全错过了您的逻辑。

您可以将guest虚拟机中间件添加到路由文件中的注册路由,以强制路由通过您的代码,如下所示:

Route::get('/register/step-1', '<YOUR CONTROLLER METHOD>')->middleware('guest');

但是,既然您说用户已经登录(而不是访客),您应该按照其他答案的建议移动您的代码。

只添加此作为答案,因为它无法在评论允许的空间中澄清。

© www.soinside.com 2019 - 2024. All rights reserved.