【问题标题】:How to change password in laravel-4 with auth如何使用 auth 在 laravel-4 中更改密码
【发布时间】:2013-12-19 06:39:05
【问题描述】:

我正在使用 laravel-4 身份验证组件。我想做一个更改用户密码的功能。我的看法如下:

密码 - 文本框; new_password - 文本框; confirm_new_password - 文本框

我还检查了密码重置手册,但在该文档中(http://laravel.com/docs/security#password-reminders-and-reset),他们正在发送邮件进行密码重置。 视图如下:

   @extends('layouts.main') 
@section('title') Change Password 
@stop
@section('content')
{{ Form::open(array('url'=>'users/user-password-change', 'class'=>'block small center login')) }}
<h3 class="">Change Password</h3>
   <h6>Please change your password below.</h6>
    <ul>
        @foreach($errors->all() as $error)
            <li>{{ $error }}</li>
        @endforeach
    </ul>

    {{ Form::password('password', array('class'=>'input-block-level', 'placeholder'=>'Old Password')) }}
    {{ Form::password('new_password', array('class'=>'input-block-level', 'placeholder'=>'New Password')) }}
    {{ Form::password('confirm_new_password', array('class'=>'input-block-level', 'placeholder'=>'Confirm New Password')) }}


    {{ Form::submit('Register', array('class'=>'k-button'))}}
{{ Form::close() }}
@stop

控制器代码如下:

public function postUserPasswordChange(){
    $validator = Validator::make(Input::all(), User::$change_password_rules);
    if($validator->passes()){
        $user=new UserEventbot;
        $user->password=Hash::make(Input::get('new_password'));
        $user->save();
        return Redirect::to('users/change-password');
    }else {
        return Redirect::to('users/change-password')->with('message', 'The following errors occurred')->withErrors($validator)->withInput();
    }
}

请帮助我,如何首先将此密码与数据库表用户匹配,然后是整个过程。 谢谢。

【问题讨论】:

  • 你的控制器里有什么?更新函数里面是什么。
  • 我添加了有问题的控制器代码,请检查。
  • 是用户已登录。

标签: laravel-4


【解决方案1】:

要将当前用户密码与数据库中的密码匹配,您可以执行以下操作,

// retrieve the authenticated user
$user = Auth::user();

检索表单内用户指定的当前密码,

$current_password = Input::get('current_password');

我们可以查看是否指定了当前密码,并检查哈希密码,如下所示,

if (strlen($current_password) > 0 && !Hash::check($current_password, $user->password)) {
        return Redirect::to('/user/edit-profile')->withErrors('Please specify the good current password');
    }

这里要注意的是函数

Hash::check(CURRENT_PASSWORD_ENTERED_IN_FORM, HASHED_VERSION_OF_PASSWORD_STORED_IN_AUTH_USER)

最后,如果一切顺利,您可以在 Auth 和数据库中更新当前用户密码,如下所示,

// authenticated user
$user = Auth::user();
$user->password = Hash::make($new_password);

// finally we save the authenticated user
$user->save();

【讨论】:

    【解决方案2】:

    尝试以下方法:

    public function postUserPasswordChange(){
        $validator = Validator::make(Input::all(), User::$change_password_rules);
        if($validator->passes()){
    
            $user = UserEventbot::findOrFail(Auth::user()->id);
    
            $user->password = Hash::make(Input::get('new_password'));
            $user->save();
            return Redirect::to('users/change-password');
        }else {
            return Redirect::to('users/change-password')->with('message', 'The following errors occurred')->withErrors($validator)->withInput();
        }
    }
    

    【讨论】:

    • 是的,现在可以使用了,谢谢。剩下一件事如何将密码与数据库密码匹配。
    【解决方案3】:

    这是我自己的解决方案,我有 3 个输入 = old_password、password、password_confirmation、带有 action=post 的表单。 验证器检查要求,哈希检查旧密码是否匹配

    public function changePassword() 
    {
        $user = Auth::user();
        $rules = array(
            'old_password' => 'required|alphaNum|between:6,16',
            'password' => 'required|alphaNum|between:6,16|confirmed'
        );
    
        $validator = Validator::make(Input::all(), $rules);
    
        if ($validator->fails()) 
        {
            return Redirect::action('UserController@show',$user->id)->withErrors($validator);
        } 
        else 
        {
            if (!Hash::check(Input::get('old_password'), $user->password)) 
            {
                return Redirect::action('UserController@show',$user->id)->withErrors('Your old password does not match');
            }
            else
            {
                $user->password = Hash::make(Input::get('password'));
                $user->save();
                return Redirect::action('UserController@show',$user->id)->withMessage("Password have been changed");
            }
        }
    }
    

    【讨论】:

      【解决方案4】:

      请尝试以下更改

      public function postUserPasswordChange(){
          $validator = Validator::make(Input::all(), User::$change_password_rules);
          if($validator->passes()){
              $user = UserEventbot::findOrFail(Auth::user()->id);
              if(Hash::check(Input::get('password'), $user->getAuthPassword())) {
                  $user->password = Hash::make(Input::get('new_password'));
                  $user->save();
                  return Redirect::to('users/change-password')->with('message','Your password has been changed');
              }
          }
          return Redirect::to('users/change-password')->with('message', 'The following errors occurred')->withErrors($validator);
      }
      

      您应该在密码方面排除 withInput()。希望有帮助

      【讨论】:

        猜你喜欢
        • 2017-09-12
        • 2014-08-27
        • 2019-03-10
        • 2014-01-14
        • 2013-06-29
        • 1970-01-01
        • 2016-12-30
        • 1970-01-01
        • 2020-01-21
        相关资源
        最近更新 更多