【问题标题】:Laravel 5.7 Auth::attempt return falseLaravel 5.7 Auth::attempt 返回 false
【发布时间】:2019-05-24 10:29:56
【问题描述】:

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 。 不明白为什么,经过几个小时的搜索后发布..大部分代码只是从文档中复制的。 提前致谢。

【问题讨论】:

  • dd($credentials) 得到什么?
  • @IjasAmeenudeen 我提交的数据相同。
  • 已解决,感谢您的帮助,非常感谢。

标签: authentication laravel-5.7


【解决方案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'))
    ]);

【讨论】:

    猜你喜欢
    • 2017-05-11
    • 2014-11-22
    • 1970-01-01
    • 2015-03-03
    • 2016-09-12
    • 2017-05-24
    • 2013-06-11
    • 2015-12-10
    相关资源
    最近更新 更多