简体   繁体   English

Laravel 5.4:在用户登录时将用户重定向到他的帐户,然后尝试转到登录页面

[英]Laravel 5.4: redirect user to his account when user is login and try to go to login page

I have tree Auth guard (Customer, supplier, admin) and here is the RedirectIfAuthenticated.php looks like 我有树身份验证卫士(客户,供应商,管理员),这是RedirectIfAuthenticated.php看起来像

public function handle( $request, Closure $next, $guard = null ) {
        switch ( $guard ) {
            case 'supplier':
                {
                    if ( Auth::guard( $guard )->check() ) {
                        return redirect()->route( 'supplier-dashboard' );
                    }
                    break;
                }
            case 'admin':
                {
                    if ( Auth::guard( $guard )->check() ) {
                        return redirect()->route( 'admin-dashboard' );
                    }
                    break;
                }

            default :
                {
                    if ( Auth::guard( $guard )->check() ) {
                        return redirect( '/my-account' );
                    }
                }
        }

        return $next( $request );
    }

when I am logged in as supplier or admin and try to go to login page it takes me to the right route 当我以供应商或管理员身份登录并尝试进入登录页面时,它将带我到正确的route

but comes to the customer it takes me to the login page which is wrong here as I defined the default case to redirect to /my-account 但是遇到客户时,我将我带到登录页面,这在这里是错误的,因为我定义了重定向到/my-account的默认情况

default :
                {
                    if ( Auth::guard( $guard )->check() ) {
                        return redirect( '/my-account' );
                    }
                }

What I am missing here? 我在这里想念的是什么?

Solution

As @BilalAhmed suggest I debugged the return of ( Auth::guard( $guard )->check() ) and the result was false then I try ( Auth::guard( 'web' )->check() ) and this worked fine for me. 正如@BilalAhmed建议的那样,我调试了( Auth::guard( $guard )->check() )的返回结果,结果为false然后尝试使用( Auth::guard( 'web' )->check() )对我来说很好。

Laravel (authenticate middleware) have two types of auth guard, web & api . Laravel(身份验证中间件)具有两种类型的身份验证保护,即webapi for custom you must define in authenticate model like protected $guard = 'customer'; 对于自定义,您必须在身份验证模型中定义,例如protected $guard = 'customer';

By default laravel used web like Auth::guard('web')->user() . 默认情况下laravel使用web就像Auth::guard('web')->user() it's similar to Auth::user() . 它类似于Auth::user() also you can check default guard in config/auth.php file 您也可以在config/auth.php文件中检查默认防护

when execute this line if ( Auth::guard( $guard )->check() ) that's mean Auth::guard( 'customer' )->check() but there is no customer guard define in auth.php file 当执行此行时if ( Auth::guard( $guard )->check() )意味着Auth::guard( 'customer' )->check()但在auth.php文件中没有定义客户保护

if you want to create custom guard then read this articles 如果要创建自定义防护,请阅读此文章

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM