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'); } }
edit: i found that the auth trying to find a user using query SELECT * FROM
usersWHERE
Username = '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!!