تغییر پسورد در لاراول
برای تغییر password در laravel که کار چندان سختی هم نیست به کدهای زیر دقت کنید:
در قسمت فرم:
<form id="form-change-password" role="form" method="POST" action="{{ url('/user/credentials') }}" novalidate class="form-horizontal">
<div class="col-md-9">
<label for="current-password" class="col-sm-4 control-label">Current Password</label>
<div class="col-sm-8">
<div class="form-group">
<input type="hidden" name="_token" value="{{ csrf_token() }}">
<input type="password" class="form-control" id="current-password" name="current-password" placeholder="Password">
</div>
</div>
<label for="password" class="col-sm-4 control-label">New Password</label>
<div class="col-sm-8">
<div class="form-group">
<input type="password" class="form-control" id="password" name="password" placeholder="Password">
</div>
</div>
<label for="password_confirmation" class="col-sm-4 control-label">Re-enter Password</label>
<div class="col-sm-8">
<div class="form-group">
<input type="password" class="form-control" id="password_confirmation" name="password_confirmation" placeholder="Re-enter Password">
</div>
</div>
</div>
<div class="form-group">
<div class="col-sm-offset-5 col-sm-6">
<button type="submit" class="btn btn-danger">Submit</button>
</div>
</div>
</form>
ایجاد rules برای اعتبارسنجی فرم:
public function admin_credential_rules(array $data)
{
$messages = [
'current-password.required' => 'Please enter current password',
'password.required' => 'Please enter password',
];
$validator = Validator::make($data, [
'current-password' => 'required',
'password' => 'required|same:password',
'password_confirmation' => 'required|same:password',
], $messages);
return $validator;
}
و در آخر در کنترلرمون بعد از validate کردن، کلمه عبور مورد نظر رو تغییر می دیم:
public function postCredentials(Request $request)
{
if(Auth::Check())
{
$request_data = $request->All();
$validator = $this->admin_credential_rules($request_data);
if($validator->fails())
{
return response()->json(array('error' => $validator->getMessageBag()->toArray()), 400);
}
else
{
$current_password = Auth::User()->password;
if(Hash::check($request_data['current-password'], $current_password))
{
$user_id = Auth::User()->id;
$obj_user = User::find($user_id);
$obj_user->password = Hash::make($request_data['password']);;
$obj_user->save();
return "ok";
}
else
{
$error = array('current-password' => 'Please enter correct current password');
return response()->json(array('error' => $error), 400);
}
}
}
else
{
return redirect()->to('/');
}
}
chasboon همیشه update !