dongluanan7163 2016-05-11 22:51
浏览 324

Laravel 5.0 ::中间件问题(ERR_TOO_MANY_REDIRECTS错误)

I am an absolute beginner of Laravel framework. I am dealing with a middleware issue.

With the authenticate.php and __construct function in AdminController.php combined below, I cannot log in and have to deal with ERR_TOO_MANY_REDIRECTS error.

Has anyone ever encountered this kind of problem? If you have, could me give any advice to solve this issue?

English is not my first language so if this post does not make sense or you need more information, please leave your comments! Any advice would be appreciated! Thanks in advance!

Authenticate.php

public function handle($request, Closure $next){
    if ($this->auth->guest()) {
        if ($request->ajax()) {
            return response('Unauthorized.', 401);
        }
        else {
            return redirect()->guest('auth/login');
        }
     }

   if ($this->whoIsLoggedIn() == 'instructor'){
        return redirect('/instructor/home');
    } elseif ($this->whoIsLoggedIn() == 'admin') {
        return redirect('/admin/home');
    } elseif ($this->whoIsLoggedIn() == 'student') {
        return redirect('/student/home');
    } elseif($this->whoIsLoggedIn() == 'unknown'){
        return redirect('/auth/login');
    }

     return $next($request);
}

public function whoIsLoggedIn(){

    $identity = '';

    foreach (\Auth::user()->roles as $role) {
        if ($role->name == 'admin') {
            return $identity = 'admin';
        } else if ($role->name == 'instructor')  {
            return $identity = 'instructor';
        } else if ($role->name == 'student'){
            return $identity = 'student';
        } else {
            return $identity = 'unknown';
        }
    }
}

AdminController.php

public function __construct(){
    $this->middleware('auth');
}
  • 写回答

1条回答 默认 最新

  • dongzhao1930 2016-05-11 23:48
    关注

    If you look carefully, your code actually implements an infinite redirect loop - once user is logged in, your middleware class will always redirect the user further. Since the next page probably has the same middleware applied - user will be redirected again and again and again.

    At some point, your middleware is supposed to just call the next middleware in the chain:

    return $next($request);
    

    That will finish the middleware chain, and, if everything is fine, finally give the control to your page.

    What you are trying to implement (default starting page for authenticated users) should probably be in your AuthController instead of this middleware. This middleware doesn't know whether the user is just logging in or has been authenticated for a while now.

    I suggest you move your redirect conditions to postLogin() method of AuthController so that it will happen only once – when the user actually logs in. You can pretty much copy paste most of the code.

    评论

报告相同问题?

悬赏问题

  • ¥20 基于MSP430f5529的MPU6050驱动,求出欧拉角
  • ¥20 Java-Oj-桌布的计算
  • ¥15 powerbuilder中的datawindow数据整合到新的DataWindow
  • ¥20 有人知道这种图怎么画吗?
  • ¥15 pyqt6如何引用qrc文件加载里面的的资源
  • ¥15 安卓JNI项目使用lua上的问题
  • ¥20 RL+GNN解决人员排班问题时梯度消失
  • ¥60 要数控稳压电源测试数据
  • ¥15 能帮我写下这个编程吗
  • ¥15 ikuai客户端l2tp协议链接报终止15信号和无法将p.p.p6转换为我的l2tp线路