Laravel 5.7 Auth :: attempt return false

时间:2018-12-26 04:10:34

标签: authentication laravel-5.7

Auth :: attempt总是返回false,不明白为什么。

  

web.php

Route::post('/login','SessionsController@store');
Route::post('/register','RegisterController@store');
  

RegisterController.php

public function store()
{
    $this->validate(\request(),[
        'name' => 'bail|required|min:3|max:30|string',
        'email' => 'bail|required|email',
        'password' => 'required|confirmed'

    ]);
    $user = User::create(request(['name','email','password']));
    $user->fill([
        'password' => Hash::make(\request()->newPassword)
    ])->save();
    auth()->login($user);
    return redirect()->home();
}
  

SessionsController.php

 public function store(Request $request)
{
    $credentials = $request->only('email', 'password');

    if (! Auth::attempt($credentials)) {
        return back()->withErrors(['message'=>'Email and Password doesn\'t match']);

    }
    return redirect()->home();
}
  

create.blade.php(登录页面)

<form action="/login" method="post">
            @csrf
            <div class="form-group">
                <label for="email" class="form-text">Email :</label>
                <input type="email" id="email" name="email" class="form-control" required>
            </div>
           <div class="form-group">
                <label for="password" class="form-text" >Password :</label>
                <input type="password" class="form-control" id="password" name="password" required>
            </div>
            <input type="submit" class="btn btn-primary float-right" value="Register">
        </form>

注册和数据库都可以,密码散列。 Auth :: attempt始终返回false。 不明白为什么,经过几个小时的搜索后才发布了它。大部分代码只是从文档中复制而来。 预先感谢。

1 个答案:

答案 0 :(得分:1)

只是改变

$user = User::create(request(['name','email','password']));
$user->fill([
    'password' => Hash::make(\request()->newPassword)
])->save();

 $user = User::create([
        'name' => request('name'),
        'email' => request('email'),
        'password' => bcrypt(request('password'))
    ]);