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

58 Views Asked by At

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

There are 2 best solutions below

12
Bhoopendra Kumar Yogi On
namespace App\Http\Middleware;

use Closure;
use Illuminate\Support\Facades\Auth;

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);
    }
}
1
kp shekhawat On
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.');
    }