2016-09-19 124 views
1

所以我有一个相当奇怪的问题。我创建了一个允许用户更改密码的表单。Laravel 5.2 - 验证用户更改密码 - 更新后密码匹配问题

它确实更改了密码。但它将密码更改为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 

命名空间应用; 使用Carbon \ Carbon;

使用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鲍曼让我indentify我的错误

工作职能: 公共职能updatePassword(UserSecurityFormRequest $要求) {

$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'); 
    } 
} 
+0

发布您的'用户'型号代码。 '$ fillable'属性中的'password'属性是什么?否则,您发布的这段代码将无法使用。 –

+0

发布用户模型代码。 “密码”实际上是$可填写属性。仍然没有运气。 –

回答

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作为输入名称。

+0

哦,我的这个真的有效!非常感谢!!你是绝对的明星!我从文档中得到了“newPassword”,认为它是一个Laravel函数。 –

+0

完全没问题!很高兴你解决了它:)。 –

+0

当然不是没有你的帮助。我会更新我的帖子,以防万一有人犯了同样的错误。尽管我怀疑任何人都会是那种心不在焉的人! 再次感谢您!^_ ^ –

0

你让双重密码hasing,第一在哈希::使($请求 - > NEWPASSWORD)在二传手bcrypt($密码)

删除一个,一切都应该没问题。

+0

嗯,我应该说但我删除了'公共函数setPasswordAttribute($ password) { return $ this-> attributes ['password'] = bcrypt($ password);我意识到它不起作用后,不幸的是,它仍然不起作用。 –

0

尝试使用此:

bcrypt($request->newPassword); 
+0

我所做的: '如果(哈希::检查($ current_password,$ hashed_pa​​ssword)){ \t $用户>填写([ '密码'=> bcrypt($请求 - > NEWPASSWORD) ]) - >保存(); \t}' 不幸的是它没有工作。 –