【问题标题】:Password reset for first user in laravellaravel 中第一个用户的密码重置
【发布时间】:2015-02-13 16:13:07
【问题描述】:

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

这是视图部分

<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';
        }
    }
}

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

【问题讨论】:

    标签: php laravel laravel-4 laravel-validation


    【解决方案1】:

    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';
    }
    

    【讨论】:

      【解决方案2】:

      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();
                      }
      
                  }
              }
      

      【讨论】:

        猜你喜欢
        • 2020-01-31
        • 2014-12-09
        • 1970-01-01
        • 2018-09-14
        • 2015-11-28
        • 1970-01-01
        • 2017-03-27
        • 1970-01-01
        • 2015-05-27
        相关资源
        最近更新 更多