簡體   English   中英

Laravel 5.2 - 更改密碼的經過身份驗證的用戶 - 更新后的密碼匹配問題

[英]Laravel 5.2 - Authenticated User to Change Password - Password Matching Issue after Update

所以我有一個相當奇怪的問題。 我創建了一個允許用戶更改密碼的表單。

它確實改變了他們的密碼。 但它將密碼更改為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

命名空間App; 使用碳\\碳;

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

class User extends Authenticatable {

/**
 * 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允許我識別我的錯誤

工作函數:public function 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');
    }
}

找到你的問題:

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作為輸入名稱。

你在setter bcrypt($ password)中首先在Hash :: make($ request-> newPassword)中進行雙重密碼哈希

刪除一個,一切都應該沒問題。

試着用這個:

bcrypt($request->newPassword);

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM