zafercuz
1 month ago
173
0
Laravel

Logging out other devices not working after overriding login function

Posted 1 month ago by zafercuz

I overrode the login function since I need to have the user login with their username/email. I also added invalidating session if ever they login to their account on another device. I followed Invalidating Session on other devices, added the line to authenticated function. Was there any code that was gone due to overriding the login function?

Here is my code:

    protected function authenticated()
    {
        Auth::logoutOtherDevices(request('password'));
    }

    public function login(Request $request)
    {
        $this->validate($request, [
            'login'    => 'required',
            'password' => 'required',
        ]);

        $login_type = filter_var($request->input('login'), FILTER_VALIDATE_EMAIL ) 
            ? 'email' 
            : 'username';

        $request->merge([
            $login_type => $request->input('login')
        ]);

        if (Auth::attempt($request->only($login_type, 'password'))) {
            return redirect()->intended($this->redirectPath());
        }

        return redirect()->back()
            ->withInput()
            ->withErrors([
                'login' => 'These credentials do not match our records.',
            ]);
    }

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