2016-09-19 5 views
1

だから私はかなり奇妙な問題があります。ユーザーがパスワードを変更できるフォームを作成しました。Laravel 5.2 - パスワードを変更するための認証されたユーザー - 更新後のパスワードマッチング問題

パスワードは変更されます。しかし、Laravelが明らかに認識できないものにパスワードを変更します。

"tinker"を使って手動でパスワードを "testing"のように更新する場合は、私は正常に私のパスワードを変更することができるだろう。しかし、パスワードを何か(たとえば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); 
} 

それはworをしなかったk。 (編集:私は上記の属性を削除しました)登録フォーム(Laravelのデフォルト認証システムの一部として)がログインフォームが認識するパスワードを作成できないようなものがあれば。

私はフォームが正しい詳細を提出していることを確認しました。私は、フォームが正常に送信されたときにフォームの入力からすべてのデータをダンプすることでこれを行いました。

ユーザーモデル:

<?php 

名前空間のApp。 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; 
} 

}

を拡張することがそれに影響を与えるべきではないので、私はパスワードの属性セッターをコメントアウトしました。それでもまだ動作しません。

ご協力いただければ幸いです。

ありがとうございます。

EDIT

それが機能していません。最初 パブリック関数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

'User'モデルコードを投稿してください。あなたの 'password'属性は' $ fillable'属性の属性にありますか?そうしないと、あなたが投稿したこのコードは動作しません。 –

+0

ユーザモデルコードが掲載されています。 "パスワード"は実際に$ fillablesプロパティにあります。まだ運がありません。 –

答えて

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は、あなたのパスワードが動作しない理由です、空になり、これを使用する

+0

ああ私は..これは実際に動作します!どうもありがとうございます!!あなたは絶対星です!私はそれがLaravel関数だと思ってドキュメントから "newPassword"を得た。 –

+0

全く問題はない!あなたはそれを解決してうれしい:)。 –

+0

確かにあなたの助けなしにはありません。誰かが私と同じミスを犯した場合に備えて私の投稿を更新するつもりです。 もう一度ありがとうございます!^_ ^ –

0

あなたは二重のパスワードhasingを作り、:私は私のミス

作業する機能をindentifyすることを可能にするための対応と@Steveバウマンのためのみんなに大規模な感謝ハッシュ::($要求 - >新パスワード)を作るセッターでbcryptの($パスワード)

で1つを削除し、すべてが大丈夫でなければなりません。あなたの問題を発見

bcrypt($request->newPassword); 
+0

ああ、私は言ったはずですが、私は 'public function setPasswordAttribute($ password)を削除しました。 { return $ this-> attributes ['password'] = bcrypt($ password); } '私はそれが動作しなかったことを理解した後。残念ながら、それはまだ動作しません。 –

0

してみてください。探しているリクエストフィールドはpasswordです。

これは、フォームフィールドが入力名としてpasswordを使用しているためです。

+0

私がやった:([ ) 'パスワード' => bcryptの($要求 - >新パスワード]) '場合(ハッシュ::チェック($ current_password、$ hashed_pa​​ssword)){ \tする$ user->記入を - >保存(); \t} ' 残念なことにそれは機能しませんでした。 –

関連する問題