Laravel — Confide — Сохранить Изменить пароль

Мне удалось сохранить новый пароль или изменить пароль для вошедшего в систему пользователя.

    public function saveNewPassword() {
$rules = array(
'old_password'            => 'required',
'password'       => 'required|confirmed|different:old_password',
'password_confirmation' => 'required|different:old_password|same:password_confirmation'
);
$user = User::findOrFail(Auth::user()->id);
// Validate the inputs
$validator = Validator::make(Input::all(), $rules);if ($validator->fails()) {
return Redirect::back()
->withErrors($validator)
->withInput();
} else {
$password = Input::get( 'password' );
$passwordConfirmation = Input::get( 'password_confirmation' );

if(!empty($password)) {
if($password === $passwordConfirmation) {
$user->password = $password;
$user->password_confirmation = $passwordConfirmation;
}
} else {
unset($user->password);
unset($user->password_confirmation);
}

// Save if valid. Password field will be hashed before save
$user->save();
}

// Get validation errors (see Ardent package)
$error = $user->errors()->all();

if(empty($error)) {
Session::flash('message', 'Successfully saved!');
return Redirect::back();

} else {
Session::flash('error', $error);
return Redirect::back();
}
}

У меня проблема, как проверить старый пароль, который равен текущему паролю? Есть идеи? У Confide есть свои методы смены паролей?

0

Решение

Я использую это решение для смены пароля. В ваших правилах у вас есть одна ошибка: password_confirmation должно совпадать с паролем, а не password_confirmation.

Вот полная и проверенная функция:

public function changePassword($id){
$rules = array(
'old_password'                  => 'required',
'new_password'                  => 'required|confirmed|different:old_password',
'new_password_confirmation'     => 'required|different:old_password|same:new_password'
);

$user = User::find(Auth::user()->id);
$validator = Validator::make(Input::all(), $rules);

//Is the input valid? new_password confirmed and meets requirements
if ($validator->fails()) {
Session::flash('validationErrors', $validator->messages());
return Redirect::back()->withInput();
}

//Is the old password correct?
if(!Hash::check(Input::get('old_password'), $user->password)){
return Redirect::back()->withInput()->withError('Password is not correct.');
}

//Set new password to user
$user->password = Input::get('new_password');
$user->password_confirmation = Input::get('new_password_confirmation');

$user->touch();
$save = $user->save();

return Redirect::to('logout')->withMessage('Password has been changed.');

}

Это также работает, если вы не работаете с Confide.

4

Другие решения

Из Github довериться:

Интегрирован с компонентами / конфигами Laravel Auth и Reminders.

Так что я бы предположил, используя Auth::validate() метод сделает свое дело.

0

По вопросам рекламы ammmcru@yandex.ru
Adblock
detector