为laravel中的第一个用户重置密码


Password reset for first user in laravel

我正试图在我的项目中创建一个重置选项。若用户是第一次登录,它将重定向到重置页面。

这是视图部分

<div class="form-group{{ ($errors->has('cpassword')) ? 'has error' : '' }}">
    <label for="cpassword">Current Password: </label>
    <input id="cpassword" name="cpassword" type="text" class="form-control">
            @if($errors->has('cpassword'))
                {{ $errors->first('cpassword')}}
            @endif
</div>
<div class="form-group{{ ($errors->has('password')) ? 'has error' : '' }}">
    <label for="password">New Password: </label>
    <input id="password" name="password" type="password" class="form-control">
            @if($errors->has('password'))
                {{ $errors->first('password')}}
            @endif
</div>
<div class="form-group{{ ($errors->has('password2')) ? 'has error' : '' }}">
    <label for="password2">Confirm Password: </label>
    <input id="password2" name="password2" type="password"   class="form-control">
            @if($errors->has('password2'))
                {{ $errors->first('password2')}}
            @endif
</div>
        {{ Form::token() }}
<div class="form-group">
    <input type="submit" value="Submit" class="btn btn-default">
</div>

在重置页面中,我们需要输入旧密码、新密码和确认密码。。

控制器部分在下方

公共函数postReset(){

    $validator =Validator::make(Input::all(), array(
        'cpassword' => 'required',
        'password' => 'required|min:8',
        'password2' => 'required|min:8|same:password'
    ));
    if ($validator->fails())
    {
        return Redirect::route('resetPassword')->withErrors($validator)->withInput();
    }
    else
    {
        if (Auth::attempt(array('username'=>Auth::user()->username, 'password'=>Hash::make(Input::get('cpassword'))))) {
            return 'password is resetted';
        }
    }
}

但如果我尝试验证当前密码和userpasssword,它们的哈希代码不匹配。有其他方法可以重置密码吗。我需要相同的视图部分。有人能帮忙吗??

Auth::attempt()方法需要纯密码,您不能自己生成哈希。此外,Laravel的散列不能仅仅通过比较来验证,因为每个散列都包含随机盐。要将密码与其哈希进行比较,必须使用Hash::check()。此外,虽然Auth::attempt()可以工作,但Auth::validate()将是更好的选择,因为您的用户已经登录:

$credentials = array(
    'username' => Auth::user()->username,
    'password' => Input::get('cpassword')
);
if(Auth::validate($credentials)){
    return 'password is resetted';
}

Laravel内置了"忘记"密码功能,您可以使用该功能在人们忘记密码时重置密码。

您可以在以下位置找到:http://laravel.com/docs/5.0/authentication

如果你想让某人能够更改他们的密码(并且他们能够记住旧密码),你可以这样做:

if(Input::get('passwordIs')) {
            if (!Hash::check(Input::get('passwordIs'), Auth::user()->password))
                return Redirect::to('my-account/settings/')->withErrors(Lang::get('account.password-wrong'));
            else {
                if(Input::get('passwordIsNew') !== Input::get('passwordIs_confirmation'))
                    return Redirect::to('my-account/settings/')->withErrors(Lang::get('account.passwords-must-match'));
                else{
                    $password = Hash::make(Input::get('passwordIs_confirmation'));
                    $customer = Auth::user();
                    $customer->password = $password;
                    $customer->save();
                }
            }
        }