keyboard_return  چسبون مقالات آموزشی تغییر password پسورد در لاراول
  1. تغییر password پسورد در لاراول

تغییر پسورد در لاراول

برای تغییر 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 !  
تغییر password پسورد در لاراول - 4.2 از 5 بر اساس 150 رای

امتیاز کاربران

تغییر password پسورد در لاراول
دیدگاه‌ها
رضا رضا 6 سال پیش
خیلی عالی بود و به دردم خورد. مرسی.
آرمان بابائی آرمان بابائی 6 سال پیش
ممنون از فیدبک شما
وحید وحید 6 سال پیش
ممنون.کوتاه و مختصر و مفید.
آرمان بابائی آرمان بابائی 6 سال پیش
خوشحالیم مطالب سایت چسبون مورد پسندتون قرار گرفته.

2024 © Copyright کليه حقوق مادی و معنوی براي چسبون محفوظ است و هرگونه کپی برداری پیگرد قانونی دارد.