fix bug
This commit is contained in:
parent
c72902141f
commit
3a4edcd08b
@ -86,37 +86,65 @@ class MyProfileController extends Controller
|
|||||||
{
|
{
|
||||||
$user = auth()->user();
|
$user = auth()->user();
|
||||||
|
|
||||||
// If password is null, only validate new password and confirm password
|
$rules = [];
|
||||||
|
|
||||||
|
// Define validation rules
|
||||||
if (is_null($user->password)) {
|
if (is_null($user->password)) {
|
||||||
$request->validate([
|
$rules = [
|
||||||
'new_password' => 'required|min:4',
|
'new_password' => 'required|min:8',
|
||||||
'confirm_password' => 'required|same:new_password',
|
'confirm_password' => 'required|same:new_password',
|
||||||
]);
|
];
|
||||||
|
} else {
|
||||||
// Update password
|
$rules = [
|
||||||
$user->update(['password' => Hash::make($request->new_password)]);
|
'current_password' => 'required',
|
||||||
|
'new_password' => 'required|min:8',
|
||||||
Session::flash('success', 'Password set successfully.');
|
'confirm_password' => 'required|same:new_password',
|
||||||
return redirect()->back();
|
];
|
||||||
}
|
}
|
||||||
|
|
||||||
// If password exists, use the original validation
|
$validator = Validator::make($request->all(), $rules);
|
||||||
$request->validate([
|
|
||||||
'current_password' => 'required',
|
// Custom messages with get_phrase
|
||||||
'new_password' => 'required|min:4',
|
$validator->setCustomMessages([
|
||||||
'confirm_password' => 'required|same:new_password',
|
'current_password.required' => get_phrase('Current password is required'),
|
||||||
|
'new_password.required' => get_phrase('New password is required'),
|
||||||
|
'new_password.min' => get_phrase('New password must be at least 8 characters'),
|
||||||
|
'confirm_password.required' => get_phrase('Confirm password is required'),
|
||||||
|
'confirm_password.same' => get_phrase('Confirm password does not match'),
|
||||||
]);
|
]);
|
||||||
|
|
||||||
// Check if the current password is correct
|
// Custom attribute names (optional, makes error messages nicer)
|
||||||
if (!Auth::attempt(['email' => $user->email, 'password' => $request->current_password])) {
|
$validator->setAttributeNames([
|
||||||
Session::flash('error', 'Current password is incorrect.');
|
'current_password' => get_phrase('Current password'),
|
||||||
return redirect()->back();
|
'new_password' => get_phrase('New password'),
|
||||||
|
'confirm_password' => get_phrase('Confirm password'),
|
||||||
|
]);
|
||||||
|
|
||||||
|
if ($validator->fails()) {
|
||||||
|
return redirect()->back()
|
||||||
|
->withErrors($validator)
|
||||||
|
->withInput();
|
||||||
|
}
|
||||||
|
|
||||||
|
// Check current password if exists
|
||||||
|
if (!is_null($user->password)) {
|
||||||
|
// Using Hash::check instead of Auth::attempt for better performance
|
||||||
|
if (!Hash::check($request->current_password, $user->password)) {
|
||||||
|
Session::flash('error', get_phrase('Current password is incorrect'));
|
||||||
|
return redirect()->back();
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// Update password
|
// Update password
|
||||||
$user->update(['password' => Hash::make($request->new_password)]);
|
$user->update(['password' => Hash::make($request->new_password)]);
|
||||||
|
|
||||||
Session::flash('success', 'Password changed successfully.');
|
// Success message
|
||||||
|
$successMessage = is_null($user->password)
|
||||||
|
? get_phrase('Password set successfully')
|
||||||
|
: get_phrase('Password changed successfully');
|
||||||
|
|
||||||
|
Session::flash('success', $successMessage);
|
||||||
return redirect()->back();
|
return redirect()->back();
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|||||||
Loading…
Reference in New Issue
Block a user