duandi4815 2019-05-21 11:37
浏览 123
已采纳

登录时,登录视图显示带有url:/ login的空白页面

I'm using Laravel build-in authentication methods.

I have a redirect function which returns routes for different user roles.

It doesn't redirect to the specified URL when going to /login it just shows a blank URL with the plain route (/login) in the HTML

I have searched this docs but couldn't find a solution for this problem.

This code does produce the problem I think:

//Auth/LoginController 

protected function redirectTo()
{

    if (!Auth::guest()) {
        if (Auth::user()->is_active != 0) {
            if(Auth::user()->hasRole('administrator')) {
                $this->redirectTo = '/admin';
                return $this->redirectTo;
           } else if(Auth::user()->hasRole('customer')) {
                $this->redirectTo = '/customer';
                return $this->redirectTo;
           } else {
              return '/logout'; 
           } 
        } else {
            return '/dashboard';
        }
    } else {
        return '/login';
    }
}

When i use redirect:

 protected function redirectTo()
    {
        if (!Auth::guest()) {
            if (Auth::user()->is_active != 0) {
                if(Auth::user()->hasRole('administrator')) {
                    $this->redirectTo = '/admin';
                    return redirect($this->redirectTo);
               } else if(Auth::user()->hasRole('customer')) {
                    $this->redirectTo = '/customer';
                    return redirect($this->redirectTo);
               } else {
                  return redirect('/logout'); 
               } 
            } else {
                return redirect('/dashboard');
            }
        } else {
            return redirect('/login');
        }
    }

I get this error:

This page isn’t working [..] redirected you too many times.

How do I redirect users also when going to the login route.

Thanks in advance!!

  • 写回答

1条回答 默认 最新

  • drccfl9407 2019-05-21 11:46
    关注

    This solved redirect and too many redirects error because of conflict with another login redirect function:

    protected function loggedOut(Request $request) {
        return redirect('/login');
    }
    
        protected function redirectTo()
    {
        if (!Auth::guest()) {
            if (Auth::user()->is_active != 0) {
                if(Auth::user()->hasRole('administrator')) {
                    $this->redirectTo = '/admin';
                    return redirect($this->redirectTo);
               } else if(Auth::user()->hasRole('customer')) {
                    $this->redirectTo = '/customer';
                    return redirect($this->redirectTo);
               } else {
                  return redirect('/logout'); 
               } 
            } else {
                return redirect('/dashboard');
            }
        } else {
            return view('auth.login');
        }
    }
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 ads仿真结果在圆图上是怎么读数的
  • ¥20 Cotex M3的调试和程序执行方式是什么样的?
  • ¥20 java项目连接sqlserver时报ssl相关错误
  • ¥15 一道python难题3
  • ¥15 用matlab 设计一个不动点迭代法求解非线性方程组的代码
  • ¥15 牛顿斯科特系数表表示
  • ¥15 arduino 步进电机
  • ¥20 程序进入HardFault_Handler
  • ¥15 oracle集群安装出bug
  • ¥15 关于#python#的问题:自动化测试