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 如何让企业微信机器人实现消息汇总整合
  • ¥50 关于#ui#的问题:做yolov8的ui界面出现的问题
  • ¥15 如何用Python爬取各高校教师公开的教育和工作经历
  • ¥15 TLE9879QXA40 电机驱动
  • ¥20 对于工程问题的非线性数学模型进行线性化
  • ¥15 Mirare PLUS 进行密钥认证?(详解)
  • ¥15 物体双站RCS和其组成阵列后的双站RCS关系验证
  • ¥20 想用ollama做一个自己的AI数据库
  • ¥15 关于qualoth编辑及缝合服装领子的问题解决方案探寻
  • ¥15 请问怎么才能复现这样的图呀