【问题标题】:Laravel 5.2 - Authenticated User to Change Password - Password Matching Issue after UpdateLaravel 5.2 - 经过身份验证的用户更改密码 - 更新后的密码匹配问题
【发布时间】:2017-01-27 15:17:57
【问题描述】:

所以我遇到了一个相当奇怪的问题。我创建了一个允许用户更改密码的表单。

它确实更改了他们的密码。但它会将他们的密码更改为 Laravel 显然无法识别的密码。

因此,如果我要使用“修补程序”手动将密码更新为“测试”之类的密码;我将能够成功更改我的密码。但是,一旦我将密码更改为某个值(例如 123456),表单就不会接受密码。

当我注销用户并尝试使用新密码登录时;它不会让我登录。

很明显,Laravel 无法识别新密码。

代码在这里:

查看:

<div class="panel panel-default">
        <div class="panel-heading">Change Your Password</div>
            {{ Form::open(array('url' => 'security/change_password')) }}
                <div class="form-group">
                    {!! Form::label('current_password', 'Enter Current Password:') !!}
                    {!! Form::text('current_password', null, ['class'=>'form-control']) !!}
                </div>

                <div class="form-group">
                    {!! Form::label('password', 'Enter New Password:') !!}
                    {!! Form::text('password', null, ['class'=>'form-control']) !!}
                </div>

                <div class="form-group">
                    {!! Form::label('password_confirmation', 'Confirm New Password:') !!}
                    {!! Form::text('password_confirmation', null, ['class'=>'form-control']) !!}
                </div>

                <div class="form-group">
                    {!! Form::submit('Change Password', ['class' => 'btn btn-primary form-control']) !!}
                </div>                  
            {!! Form::close() !!}

    </div>

控制器:

public function updatePassword(UserSecurityFormRequest $request)
{

    $user = Auth::user();
    $current_password = $request->input('current_password');
    $new_password = $request->input('password');
    if (Hash::check($current_password, $user->password)) {
        $user->fill([
                'password' => Hash::make($request->newPassword)
            ])->save();
    }
    else{
        return ('Please enter the correct password');
    }
}

我还尝试在用户中设置密码属性..

public function setPasswordAttribute($password) 
{ 
    return $this->attributes['password'] = bcrypt($password); 
}

那没用。 (编辑:然后我删除了上面的属性)如果有什么阻止注册表(作为 Laravel 的默认身份验证系统的一部分)创建登录表单识别的密码。

我已检查以确保表单提交的详细信息正确无误。我通过在表单提交成功时转储表单输入中的所有数据来做到这一点。

用户模型:

<?php

命名空间应用程序; 使用碳\碳;

使用 Illuminate\Foundation\Auth\User 作为 Authenticatable; //使用 App\Http\Controllers\traits\HasRoles;

类用户扩展可验证 {

/**
 * The attributes that are mass assignable.
 *
 * @var array
 */
protected $fillable = [
    'name', 'email', 'password',
];

/**
 * The attributes that should be hidden for arrays.
 *
 * @var array
 */
protected $hidden = [
    'password', 'remember_token',
];

//If register dont work or passwords arent being recognised then remove the following:

/* public function setPasswordAttribute($password) 
{ 
    return $this->attributes['password'] = bcrypt($password); 
}*/

//turns dates to carbon
protected $dates = ['created_at'];

     //Creates Many to Many Relationship between Users table (and model) and Roles Table (and model)
public function roles()
{
    return $this->belongsToMany(Roles::class);
}


//Checks for a specific role
public function hasRole($role)
{
    if(is_string($role))
    {
        return $this->roles->contains('name', $role);
    }

    return !! $role->intersect($this->roles)->count();
}

//gives the user the role
public function assignRole($role)
{
    return $this->roles()->save(
        Roles::whereName($role)->firstOrFail()
    );
}

//Checks whether the user has a role with that permission
public function hasPermission($permission)
{
    return $this->hasRole($permission->roles);
}

public function owns($related)
{
    return $this->id === $related->user_id;
}

}

如您所见,我已经注释掉了密码的属性设置器,因此不应该影响它。但是还是不行。

任何帮助将不胜感激。

谢谢。

编辑

它不工作。非常感谢所有回复的人以及@Steve Bauman 让我指出我的错误

工作功能: 公共函数 updatePassword(UserSecurityFormRequest $request) {

    $user = Auth::user();
    $hashed_password = Auth::user()->password;
    $current_password = $request->input('current_password');
    $new_password = $request->input('password');
    if (Hash::check($current_password, $hashed_password)) {
        $user->fill([
                                'password' => Hash::make($request->password)

            ])->save();
    }
    else{
        return ('Please enter the correct password');
    }
}

【问题讨论】:

  • 发布您的User 型号代码。您的 password 属性是否在 $fillable 属性属性中?否则,您发布的这段代码将不起作用。
  • 已发布用户模型代码。 “密码”确实在 $fillables 属性中。还是没有运气。

标签: php forms laravel authentication passwords


【解决方案1】:

找到您的问题:

public function updatePassword(UserSecurityFormRequest $request)
{

    $user = Auth::user();

    $current_password = $request->input('current_password');

    $new_password = $request->input('password');

    if (Hash::check($current_password, $user->password)) {

        $user->fill([

                // This should be $request->password, not `$request->newPassword`

                'password' => Hash::make($request->newPassword)

            ])->save();

    } else {
        return ('Please enter the correct password');
    }
}

请求的变量newPassword 将为空,这就是您的密码不起作用的原因。您要查找的请求字段是password

这是由于您的表单字段使用password 作为输入名称。

【讨论】:

  • 哦,天哪..这确实有效!太感谢了!!你是绝对的明星!我从文档中得到了“newPassword”,认为它是一个 Laravel 函数..
  • 当然不是没有你的帮助。如果有人犯了和我一样的错误,我会更新我的帖子。虽然我怀疑有人会那么心不在焉!再次感谢你! ^_^
【解决方案2】:

尝试使用这个:

bcrypt($request->newPassword);

【讨论】:

  • 我做了:if (Hash::check($current_password, $hashed_password)) { $user-&gt;fill([ 'password' =&gt; bcrypt($request-&gt;newPassword) ])-&gt;save(); } 不幸的是它没有工作。
【解决方案3】:

你创建双重密码,首先在 Hash::make($request->newPassword) 第二个在 setter bcrypt($password)

删除一个,一切都会好起来的。

【讨论】:

  • 啊,我应该说,但是在我意识到它不起作用之后我删除了public function setPasswordAttribute($password) { return $this-&gt;attributes['password'] = bcrypt($password); }。不幸的是,它仍然不起作用。
猜你喜欢
  • 2011-09-10
  • 1970-01-01
  • 2021-07-08
  • 2020-03-26
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多