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');
}