skip to Main Content

Want to logout the specific user, when admin inactive this specific user with Laravel

public function userInactive1($id)
    {
        $selectedUser = User::find($id);
        if ($selectedUser) {
            // Update user status to inactive
            // $selectedUser->status = 0;
            $selectedUser->save();
        }

        return redirect()->back();
    }

2

Answers


  1. namespace AppHttpMiddleware;
    
    use Closure;
    use IlluminateSupportFacadesAuth;
    
    class ActiveUserCheck
    {
        public function handle($request, Closure $next)
        {
            if (Auth::check() && !Auth::user()->isActive()) {
                Auth::logout();
                return redirect()->route('login')->with('error', 'Your account is inactive. Please contact the administrator.');
            }
    
            return $next($request);
        }
    }
    
    Login or Signup to reply.
  2. If you need logout from the backend, when admin inactive this specific user  
    
    
    
    public function logoutUserById($userId)
        {
            $user = User::find($userId);
    
            if ($user) {
                Auth::logoutOtherDevices($user->password);
                Auth::logout();
                // You can change the redirect URL according to your requirement 
                return redirect('/login')->with('status', 'User logged out successfully.');
            }
           // You can change the redirect URL according to your requirement 
            return redirect('/admin')->with('error', 'User not found.');
        }
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search