update password logic

This commit is contained in:
baghizadizn 2025-12-02 14:12:57 +07:00
parent 045510e91a
commit c72902141f
5 changed files with 107 additions and 26 deletions

View File

@ -49,7 +49,7 @@ class GoogleAuthController extends Controller
if (!$existingUser->google_id) { if (!$existingUser->google_id) {
$existingUser->update([ $existingUser->update([
'google_id' => $googleUser->getId(), 'google_id' => $googleUser->getId(),
'avatar' => $googleUser->getAvatar() ?? $existingUser->avatar, 'photo' => $googleUser->getAvatar() ?? $existingUser->avatar,
]); ]);
} }

View File

@ -57,20 +57,63 @@ class MyProfileController extends Controller
} }
User::where('id', auth()->user()->id)->update($profile); User::where('id', auth()->user()->id)->update($profile);
} else { } else {
$old_pass_check = Auth::attempt(['email' => auth()->user()->email, 'password' => $request->current_password]); $user = auth()->user();
if (! $old_pass_check) { $rules = [];
Session::flash('error', get_phrase('Current password wrong.'));
return redirect()->back(); if (is_null($user->password)) {
// For setting initial password
$rules = [
'new_password' => 'required|min:8',
'confirm_password' => 'required|same:new_password',
];
} else {
// For changing existing password
$rules = [
'current_password' => 'required',
'new_password' => 'required|min:8',
'confirm_password' => 'required|same:new_password',
];
} }
if ($request->new_password != $request->confirm_password) { $validator = Validator::make($request->all(), $rules);
Session::flash('error', get_phrase('Confirm password not same'));
return redirect()->back(); // Set custom messages
$validator->setCustomMessages([
'same' => get_phrase('Confirm password does not match'),
'required' => get_phrase(':attribute is required'),
'min' => get_phrase(':attribute must be at least :min characters'),
]);
// Set custom attribute names
$validator->setAttributeNames([
'current_password' => get_phrase('Current password'),
'new_password' => get_phrase('New password'),
'confirm_password' => get_phrase('Confirm password'),
]);
if ($validator->fails()) {
return redirect()->back()
->withErrors($validator)
->withInput();
} }
$password = Hash::make($request->new_password); // Additional check for current password if it exists
User::where('id', auth()->user()->id)->update(['password' => $password]); if (!is_null($user->password)) {
if (!Hash::check($request->current_password, $user->password)) {
Session::flash('error', get_phrase('Current password is incorrect'));
return redirect()->back();
}
}
$user->update(['password' => Hash::make($request->new_password)]);
$message = is_null($user->password)
? get_phrase('Password has been set successfully.')
: get_phrase('Password has been updated successfully.');
Session::flash('success', $message);
return redirect()->back();
} }
Session::flash('success', get_phrase('Your changes has been saved.')); Session::flash('success', get_phrase('Your changes has been saved.'));
return redirect()->back(); return redirect()->back();

View File

@ -84,6 +84,23 @@ class MyProfileController extends Controller
public function changePassword(Request $request) public function changePassword(Request $request)
{ {
$user = auth()->user();
// If password is null, only validate new password and confirm password
if (is_null($user->password)) {
$request->validate([
'new_password' => 'required|min:4',
'confirm_password' => 'required|same:new_password',
]);
// Update password
$user->update(['password' => Hash::make($request->new_password)]);
Session::flash('success', 'Password set successfully.');
return redirect()->back();
}
// If password exists, use the original validation
$request->validate([ $request->validate([
'current_password' => 'required', 'current_password' => 'required',
'new_password' => 'required|min:4', 'new_password' => 'required|min:4',
@ -91,13 +108,13 @@ class MyProfileController extends Controller
]); ]);
// Check if the current password is correct // Check if the current password is correct
if (!Auth::attempt(['email' => auth()->user()->email, 'password' => $request->current_password])) { if (!Auth::attempt(['email' => $user->email, 'password' => $request->current_password])) {
Session::flash('error', 'Current password is incorrect.'); Session::flash('error', 'Current password is incorrect.');
return redirect()->back(); return redirect()->back();
} }
// Update password // Update password
auth()->user()->update(['password' => Hash::make($request->new_password)]); $user->update(['password' => Hash::make($request->new_password)]);
Session::flash('success', 'Password changed successfully.'); Session::flash('success', 'Password changed successfully.');
return redirect()->back(); return redirect()->back();

View File

@ -104,17 +104,24 @@
<button class="eBtn btn gradient mt-10">{{ get_phrase('Save Changes') }}</button> <button class="eBtn btn gradient mt-10">{{ get_phrase('Save Changes') }}</button>
</form> </form>
</div> </div>
<div class="my-panel message-panel edit_profile"> <div class="my-panel message-panel edit_profile">
<h4 class="g-title mb-5">{{ get_phrase('Change Password') }}</h4> <h4 class="g-title mb-5">
@if(is_null($user_details->password))
{{ get_phrase('Set Password') }}
@else
{{ get_phrase('Change Password') }}
@endif
</h4>
<form action="{{ route('password.change') }}" method="POST">@csrf <form action="{{ route('password.change') }}" method="POST">@csrf
<div class="row"> <div class="row">
@if(!is_null($user_details->password))
<div class="col-lg-12 mb-20"> <div class="col-lg-12 mb-20">
<div class="form-group"> <div class="form-group">
<label class="form-label">{{ get_phrase('Current password') }}</label> <label class="form-label">{{ get_phrase('Current password') }}</label>
<input type="password" class="form-control" name="current_password" required> <input type="password" class="form-control" name="current_password" required>
</div> </div>
</div> </div>
@endif
<div class="col-lg-12 mb-20"> <div class="col-lg-12 mb-20">
<div class="form-group"> <div class="form-group">
<label class="form-label">{{ get_phrase('New password') }}</label> <label class="form-label">{{ get_phrase('New password') }}</label>
@ -128,7 +135,13 @@
</div> </div>
</div> </div>
</div> </div>
<button class="eBtn btn gradient mt-10">{{ get_phrase('Update password') }}</button> <button class="eBtn btn gradient mt-10">
@if(is_null($user_details->password))
{{ get_phrase('Set Password') }}
@else
{{ get_phrase('Update password') }}
@endif
</button>
</form> </form>
</div> </div>
</div> </div>

View File

@ -117,10 +117,12 @@
<div class="ol-card p-4"> <div class="ol-card p-4">
<div class="ol-card-body"> <div class="ol-card-body">
<form action="{{ route('instructor.manage.profile.update') }}" method="post"> @csrf <form action="{{ route('instructor.manage.profile.update') }}" method="post"> @csrf
@if(!is_null(auth()->user()->password))
<div class="fpb7 mb-2"> <div class="fpb7 mb-2">
<label class="form-label ol-form-label">{{ get_phrase('Current password') }}</label> <label class="form-label ol-form-label">{{ get_phrase('Current password') }}</label>
<input type="password" class="form-control ol-form-control" name="current_password" required /> <input type="password" class="form-control ol-form-control" name="current_password" @if(is_null(auth()->user()->password)) disabled @else required @endif />
</div> </div>
@endif
<div class="fpb7 mb-2"> <div class="fpb7 mb-2">
<label class="form-label ol-form-label">{{ get_phrase('New password') }}</label> <label class="form-label ol-form-label">{{ get_phrase('New password') }}</label>
<input type="password" class="form-control ol-form-control" name="new_password" required /> <input type="password" class="form-control ol-form-control" name="new_password" required />
@ -130,7 +132,13 @@
<input type="password" class="form-control ol-form-control" name="confirm_password" required /> <input type="password" class="form-control ol-form-control" name="confirm_password" required />
</div> </div>
<div class="fpb7 mb-2"> <div class="fpb7 mb-2">
<button type="submit" class="ol-btn-primary">{{ get_phrase('Update password') }}</button> <button type="submit" class="ol-btn-primary">
@if(is_null(auth()->user()->password))
{{ get_phrase('Set Password') }}
@else
{{ get_phrase('Update password') }}
@endif
</button>
</div> </div>
</form> </form>
</div> </div>