I’m using Auth::attempt($credentials) in Laravel 8.7, it always returns false.
My Login Blade Is
resources/views/login/login.blade.php
<form action="{{ route('login.custom') }}" method="post"> @csrf <div class="input-group mb-3"> <input type="email" class="form-control" placeholder="Email" name="email"> <div class="input-group-append"> <div class="input-group-text"> <span class="fas fa-envelope"></span> </div> </div> </div> @error('email') <div class="alert alert-danger">{{ $message }}</div> @enderror <div class="input-group mb-3"> <input type="password" class="form-control" placeholder="Password" name="password"> <div class="input-group-append"> <div class="input-group-text"> <span class="fas fa-lock"></span> </div> </div> </div> @error('password') <div class="alert alert-danger">{{ $message }}</div> @enderror <div class="row"> <div class="col-8"> <div class="icheck-primary"> <input type="checkbox" id="remember"> <label for="remember"> Remember Me </label> </div> </div> <!-- /.col --> <div class="col-4"> <button type="submit" class="btn btn-primary btn-block">Sign In</button> </div> <!-- /.col --> </div> </form>
Router
Route::post('custom-login', [LoginController::class, 'customLogin'])->name('login.custom');
My Controller code is like this – I’m accessing the controller using a router
LoginController.php
public function customLogin(Request $request) { //dd(print_r($request)); $request->validate([ 'email' => 'required', 'password' => 'required', ]); // $email = $request->input('email'); // $password = $request->input('password'); $credentials = $request->only('email', 'password'); if (Auth::attempt($credentials)) { return "Success"; } else { return "Fail"; } //return redirect("login")->withSuccess('Login details are not valid'); }
When I’m using Auth::attempt($credentials), returning always a false statement. Please help me to find out the actual problem or suggest me best solution. I have tried many combinations to solve this problem but still not working.
Advertisement
Answer
You need to hash the password in the database. The authentication system is expecting the password to be hashed in the database. It will do a hash check against the plain text version (user submitted value) and the hashed password from the database.
“If the user is found, the hashed password stored in the database will be compared with the
password
value passed to the method via the array.”
Laravel 8.x Docs – Authentication – Manually Authenticating Users