提问者:小点点

在laravel 8.76中,Auth::trument($credentials)总是返回false。2.


我在Laravel 8.7中使用Auth::attempt($凭据),它总是返回false。

我的登录刀片是

资源/视图/登录/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>

路由器

Route::post('custom-login', [LoginController::class, 'customLogin'])->name('login.custom');

我的控制器代码是这样的-我正在使用路由器访问控制器

登录控制器。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');
    }

当我使用Auth::trument($credentials)时,总是返回一个false语句。请帮我找出实际问题或建议我最好的解决方案。我尝试了许多组合来解决这个问题,但仍然不起作用。


共1个答案

匿名用户

看起来您可能正在使用一个稍微过时的代码示例,它不太适合Laravel8。尝试:

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

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