I want to make a middleware in my Laravel 9 project where if the user is inactive, they automatically get logged out. In my middleware class, like the following.
public function handle(Request $request, Closure $next)
{
if (Auth::user()->is_active != 1) {
return Auth::logout();
}
return $next($request);
}
But when I try it, I get an error like this.
Am I doing something wrong?
The logout function will not return the object, and you can't redirect it from middleware.
What you can do is
public function handle(Request $request, Closure $next)
{
if (Auth::check() && Auth::user()->is_active != 1) {
Auth::logout();
return redirect('/login'); # add you login route
}
return $next($request);
}