feat: update password

This commit is contained in:
2026-05-26 13:48:22 +02:00
parent 835dfcc677
commit b3968a8181
6 changed files with 138 additions and 1 deletions
+28 -1
View File
@@ -7,6 +7,7 @@ use App\Http\Requests\ForgotPasswordRequest;
use App\Http\Requests\LoginRequest;
use App\Http\Requests\RegisterRequest;
use App\Http\Requests\ResetPasswordRequest;
use App\Http\Requests\UpdatePasswordRequest;
use App\Http\Requests\UpdateUserRequest;
use App\Http\Resources\UserResource;
use App\Models\User;
@@ -255,7 +256,7 @@ class AuthController extends Controller
Storage::disk()->delete($user->avatar_url);
}
$path = $request->file('avatar')->store('avatars', );
$path = $request->file('avatar')->store('avatars');
$data['avatar_url'] = $path;
}
@@ -275,6 +276,32 @@ class AuthController extends Controller
return new UserResource($user->fresh());
}
public function updatePassword(UpdatePasswordRequest $request): JsonResponse
{
$user = $request->user();
abort_if($user->isSuspended(), 403, __('api.auth.suspended'));
$currentAccessToken = $user->currentAccessToken();
$user->forceFill([
'password' => Hash::make($request->validated('password')),
'remember_token' => Str::random(60),
])->save();
if ($currentAccessToken instanceof PersonalAccessToken) {
$user->tokens()
->whereKeyNot($currentAccessToken->getKey())
->delete();
} else {
$user->tokens()->delete();
}
return response()->json([
'message' => __('api.auth.password_updated'),
]);
}
public function destroy(Request $request): JsonResponse
{
$user = $request->user();
@@ -0,0 +1,44 @@
<?php
namespace App\Http\Requests;
use Illuminate\Foundation\Http\FormRequest;
class UpdatePasswordRequest extends FormRequest
{
/**
* Determine if the user is authorized to make this request.
*/
public function authorize(): bool
{
return true;
}
/**
* Get the validation rules that apply to the request.
*
* @return array<string, \Illuminate\Contracts\Validation\ValidationRule|array<mixed>|string>
*/
public function rules(): array
{
return [
'current_password' => ['required', 'string', 'current_password:sanctum'],
'password' => ['required', 'string', 'min:8', 'confirmed', 'different:current_password'],
];
}
protected function prepareForValidation(): void
{
if ($this->has('currentPassword') && ! $this->has('current_password')) {
$this->merge([
'current_password' => $this->input('currentPassword'),
]);
}
if ($this->has('passwordConfirmation') && ! $this->has('password_confirmation')) {
$this->merge([
'password_confirmation' => $this->input('passwordConfirmation'),
]);
}
}
}