Home > OS >  Auth::attempt($credentials) Always returns false in Laravel 8.76.2
Auth::attempt($credentials) Always returns false in Laravel 8.76.2

Time:12-21

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 >
          <input type="email"  placeholder="Email" name="email">
          <div >
            <div >
              <span ></span>
            </div>
          </div>
        </div>
        @error('email')
            <div >{{ $message }}</div>
        @enderror
        <div >
          <input type="password"  placeholder="Password" name="password">
          <div >
            <div >
              <span ></span>
            </div>
          </div>
        </div>
        @error('password')
            <div >{{ $message }}</div>
        @enderror
        <div >
          <div >
            <div >
              <input type="checkbox" id="remember">
              <label for="remember">
                Remember Me
              </label>
            </div>
          </div>
          <!-- /.col -->
          <div >
            <button type="submit" >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.

This is my table structure

This is output

CodePudding user response:

It looks like you may be using a slightly outdated code example that's not quite right for Laravel 8. Try :

    $credentials = $request->validate([
        'email' => ['required', 'email'],
        'password' => ['required'],
    ]);

    if (Auth::attempt($credentials)) {
        return "Success";
    } else { 
        return "Fail";
    }

CodePudding user response:

    =>your customLogin function working i checked. 
=>Problem is your email and password not match on your database please check your email and password both are match on your database  then login success please check. 
  • Related