我正在开发laravel 5电子商务门户网站。
当用户使用现成的脚本更新密码时,我遇到了问题。
问题是我可以完美地将链接发送给客户而不会出现任何问题,客户也可以更改密码。但是当注销并重新登录时,我得到的错误是Invalid credentials
。
在我的routes.php中,我有这个:
Route::controllers([
'auth' => 'Auth\AuthController',
'password' => 'Auth\PasswordController',
]);
这是登录页面:
<form class="form-horizontal" role="form" method="POST" action="{{ url('/login') }}">
<input type="hidden" name="_token" value="{{ csrf_token() }}">
<div class="form-group">
<label class="col-md-4 control-label">E-Mail Address</label>
<div class="col-md-6">
<input type="email" class="form-control" name="email" value="{{ old('email') }}">
</div>
</div>
<div class="form-group">
<label class="col-md-4 control-label">Password</label>
<div class="col-md-6">
<input type="password" class="form-control" name="password">
</div>
</div>
<div class="form-group">
<div class="col-md-4"></div>
<div class="col-md-4">
<a href="{{url('/password/email')}}">Forgot Password</a>
</div>
</div>
<div class="form-group">
<div class="col-md-6 col-md-offset-4">
<button type="submit" class="btn btn-primary btn-block">Login</button>
</div>
</div>
</form>
密码重置后,我在注销后无法再次登录。
编辑1:
在登录表单页面上单击登录按钮时,将调用postLogin
方法。这是代码
public function postLogin( Request $request ) {
$this->validate( $request, [
'email' => ['required', 'exists:users,email,role,customer'],
'password' => 'required'
]);
$credentials = $request->only('email', 'password');
if ( \Auth::attempt($credentials) ) {
\Session::flash('logged_in', 'You have successfully logged in.');
return redirect('/');
}
return redirect('/login')->withInput($request->all())->withErrors(['email' => 'Invalid Email Address Or Password']);
}
编辑2:
我刚刚意识到登录不会检查哈希值,因此在更新密码并重新登录后,在执行false
时返回dd(\Hash::check($request->password, $user->password))
。这可能是什么问题?
我哪里弄错了?请指导我。
提前致谢。
P.S。:我使用默认值只更新密码,休息所有,我已经制作了所有工作正常的控制器和模型没有任何问题。
如果更改后新密码不起作用,则更改密码时会出现问题。
最可能怀疑是加密。您可能没有使用Hash :: make($ password)并以纯文本格式保存它。
如果使用函数Hash :: check($ password,$ hash)将哈希正确保存到DB,则可以重新检查;
在登录期间,您可以将密码检查为
public function postLogin( Request $request ) {
$user=User::where('email', $request->email);
Log::debug("Testing $request->password $user->password ". Hash::check($request->password, $user->password));
}
如果Hash :: check为false,则在保存新密码时出现问题。 $ user-> password必须采用哈希形式。
我偶然发现了这个问题,并找到了答案here,只是添加这个以供将来参考..
原因是,只要在setPasswordAttribute
模型上添加User
方法,当使用Laravel的内置密码重置功能时,密码将被哈希两次。正如在Laracast页面上所解释的,它所需要的只是检查已经散列的密码,例如:
// Add Hash facade
use Illuminate\Support\Facades\Hash;
class User extends Authenticatable
{
// ...
/**
* Automatically hash password
*
* @param String $value The password, maybe hashed already
*
* @return string|null
*/
public function setPasswordAttribute($value)
{
if ($value) {
$this->attributes['password'] = Hash::needsRehash($value) ? Hash::make($value) : $value;
}
}
}