psychomantis101
2 years ago
2949
16
Laravel

Find out all logged in users

Posted 2 years ago by psychomantis101

I wish to be able to find all the logged in users in my application. I have had an idea about putting their id's into a database table(lets just call it "logged" for now) when they log in and then delete it when they log out, or their session expires(does this sound like the best way to code this? If not other suggestions would be great). I'm fairly certain I know where to insert my code for the login and logout and what methods to override in the illuminate/foundation/auth/authenticateUsers

For login

public function login(Request $request)
    {
        $this->validateLogin($request);
   
        if ($this->hasTooManyLoginAttempts($request)) {
            $this->fireLockoutEvent($request);

            return $this->sendLockoutResponse($request);
        }

        if ($this->attemptLogin($request)) {
            //INSERT ID INTO DATABASE HERE
            return $this->sendLoginResponse($request);
        }

        $this->incrementLoginAttempts($request);

        return $this->sendFailedLoginResponse($request);
    }

For logout

 public function logout(Request $request)
    {
    //DELETE ID FROM DATABASE HERE
        $this->guard()->logout();

        $request->session()->flush();

        $request->session()->regenerate();

        return redirect('/');
    }

Problem is I have no idea where to put my delete code for when session expires, meaning they would still appear logged in when in fact they are not. Any help would be great

Please sign in or create an account to participate in this conversation.