Skip to content
Advertisement

Laravel Auth successfully logged in but keep redirecting to login page and not showing failed error flash message

I’m trying to authenticate user login with username and password in Laravel 8 (manual auth). I can’t go to next page after logged in and it kept stuck on login page. This is the auth function:

public function authenticate(Request $request) {
        $credentials = $request->validate([
            'username' => 'required',
            'password' => 'required'
        ]);

        if(Auth::attempt($credentials)) {
            $request->session()->regenerate();
            return redirect()->intended('/')->with('loginSuccess', 'Welcome back!');
        };

        return back()->with('loginError', 'Login failed.');
    }

My table is users, with the PK is UserId, and then it has Username and Password columns. I have tried to override username() function in my LoginController.php:

public function username() {
        return 'Username';
    }

and also changing getAuth...() functions in User.php model:

    public function getAuthIdentifier()
    {
        return $this->attributes['UserId'];
    }

    public function getAuthIdentifierName()
    {
        return 'Username';
    }

    public function getAuthPassword()
    {
        return $this->attributes['Password'];
        // return 'Password';
    }

What prevents my app to auth the user? Big thanks!!

p.s. there seems to be a new link appeared after i named my route for logout() in LoginController.php:

public function logout(Request $request) {
        Auth::logout();
        $request->session()->invalidate();
        $request->session()->regenerateToken();
        return view('/login');
    }
protected function redirectTo($request)
    {
        if (! $request->expectsJson()) {
            return route('login');
        }
    }

No error flash message after login process in Laravel manual auth and a new link on the left side after naming a route

edit: i found that the auth trying to find a user using query SELECT * FROM usersWHEREUsername = 'UserId' LIMIT 1 dd($request) result and clockwork screenshot

Advertisement

Answer

I found that my getAuthIndentifier() method that was later modified to:

    public function getAuthIdentifier()
    {
        return 'UserId';
    }

causing the trouble that the query changed to SELECT * FROM `users` WHERE `Username` = 'UserId' LIMIT 1. So i had to turn the method to comment and my username input can be read to the query again. Thank you!!

successful auth with the right query for the logged in user

User contributions licensed under: CC BY-SA
8 People found this is helpful
Advertisement