feat: 프로필 설정 페이지 추가
- 프로필 정보 수정 (이름, 전화번호) - 비밀번호 변경 기능 - 헤더 드롭다운 메뉴 연결 🤖 Generated with [Claude Code](https://claude.com/claude-code) Co-Authored-By: Claude <noreply@anthropic.com>
This commit is contained in:
60
app/Http/Controllers/ProfileController.php
Normal file
60
app/Http/Controllers/ProfileController.php
Normal file
@@ -0,0 +1,60 @@
|
||||
<?php
|
||||
|
||||
namespace App\Http\Controllers;
|
||||
|
||||
use App\Http\Requests\ChangePasswordRequest;
|
||||
use App\Http\Requests\UpdateProfileRequest;
|
||||
use App\Services\ProfileService;
|
||||
use Illuminate\Http\JsonResponse;
|
||||
use Illuminate\View\View;
|
||||
|
||||
class ProfileController extends Controller
|
||||
{
|
||||
public function __construct(
|
||||
private readonly ProfileService $profileService
|
||||
) {}
|
||||
|
||||
/**
|
||||
* 프로필 페이지
|
||||
*/
|
||||
public function index(): View
|
||||
{
|
||||
return view('profile.index');
|
||||
}
|
||||
|
||||
/**
|
||||
* 프로필 정보 수정
|
||||
*/
|
||||
public function update(UpdateProfileRequest $request): JsonResponse
|
||||
{
|
||||
$user = auth()->user();
|
||||
$result = $this->profileService->updateProfile($user, $request->validated());
|
||||
|
||||
if ($result) {
|
||||
return response()->json([
|
||||
'success' => true,
|
||||
'message' => '프로필이 수정되었습니다.',
|
||||
]);
|
||||
}
|
||||
|
||||
return response()->json([
|
||||
'success' => false,
|
||||
'message' => '프로필 수정에 실패했습니다.',
|
||||
], 500);
|
||||
}
|
||||
|
||||
/**
|
||||
* 비밀번호 변경
|
||||
*/
|
||||
public function changePassword(ChangePasswordRequest $request): JsonResponse
|
||||
{
|
||||
$user = auth()->user();
|
||||
$result = $this->profileService->changePassword(
|
||||
$user,
|
||||
$request->current_password,
|
||||
$request->password
|
||||
);
|
||||
|
||||
return response()->json($result, $result['success'] ? 200 : 422);
|
||||
}
|
||||
}
|
||||
56
app/Http/Requests/ChangePasswordRequest.php
Normal file
56
app/Http/Requests/ChangePasswordRequest.php
Normal file
@@ -0,0 +1,56 @@
|
||||
<?php
|
||||
|
||||
namespace App\Http\Requests;
|
||||
|
||||
use Illuminate\Foundation\Http\FormRequest;
|
||||
|
||||
class ChangePasswordRequest 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',
|
||||
'password' => 'required|string|min:8|confirmed',
|
||||
];
|
||||
}
|
||||
|
||||
/**
|
||||
* Get custom attributes for validator errors.
|
||||
*
|
||||
* @return array<string, string>
|
||||
*/
|
||||
public function attributes(): array
|
||||
{
|
||||
return [
|
||||
'current_password' => '현재 비밀번호',
|
||||
'password' => '새 비밀번호',
|
||||
'password_confirmation' => '새 비밀번호 확인',
|
||||
];
|
||||
}
|
||||
|
||||
/**
|
||||
* Get custom messages for validator errors.
|
||||
*
|
||||
* @return array<string, string>
|
||||
*/
|
||||
public function messages(): array
|
||||
{
|
||||
return [
|
||||
'password.confirmed' => '새 비밀번호가 일치하지 않습니다.',
|
||||
'password.min' => '비밀번호는 최소 8자 이상이어야 합니다.',
|
||||
];
|
||||
}
|
||||
}
|
||||
42
app/Http/Requests/UpdateProfileRequest.php
Normal file
42
app/Http/Requests/UpdateProfileRequest.php
Normal file
@@ -0,0 +1,42 @@
|
||||
<?php
|
||||
|
||||
namespace App\Http\Requests;
|
||||
|
||||
use Illuminate\Foundation\Http\FormRequest;
|
||||
|
||||
class UpdateProfileRequest 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 [
|
||||
'name' => 'required|string|max:100',
|
||||
'phone' => 'nullable|string|max:20',
|
||||
];
|
||||
}
|
||||
|
||||
/**
|
||||
* Get custom attributes for validator errors.
|
||||
*
|
||||
* @return array<string, string>
|
||||
*/
|
||||
public function attributes(): array
|
||||
{
|
||||
return [
|
||||
'name' => '이름',
|
||||
'phone' => '연락처',
|
||||
];
|
||||
}
|
||||
}
|
||||
53
app/Services/ProfileService.php
Normal file
53
app/Services/ProfileService.php
Normal file
@@ -0,0 +1,53 @@
|
||||
<?php
|
||||
|
||||
namespace App\Services;
|
||||
|
||||
use App\Models\User;
|
||||
use Illuminate\Support\Facades\Hash;
|
||||
|
||||
class ProfileService
|
||||
{
|
||||
/**
|
||||
* 프로필 정보 수정 (이름, 전화번호)
|
||||
*/
|
||||
public function updateProfile(User $user, array $data): bool
|
||||
{
|
||||
$user->name = $data['name'];
|
||||
$user->phone = $data['phone'] ?? null;
|
||||
$user->updated_by = $user->id;
|
||||
|
||||
return $user->save();
|
||||
}
|
||||
|
||||
/**
|
||||
* 비밀번호 변경
|
||||
*/
|
||||
public function changePassword(User $user, string $currentPassword, string $newPassword): array
|
||||
{
|
||||
// 현재 비밀번호 확인
|
||||
if (! Hash::check($currentPassword, $user->password)) {
|
||||
return [
|
||||
'success' => false,
|
||||
'message' => '현재 비밀번호가 일치하지 않습니다.',
|
||||
];
|
||||
}
|
||||
|
||||
// 새 비밀번호가 현재 비밀번호와 동일한지 확인
|
||||
if (Hash::check($newPassword, $user->password)) {
|
||||
return [
|
||||
'success' => false,
|
||||
'message' => '새 비밀번호는 현재 비밀번호와 다르게 설정해주세요.',
|
||||
];
|
||||
}
|
||||
|
||||
// 비밀번호 업데이트
|
||||
$user->password = Hash::make($newPassword);
|
||||
$user->updated_by = $user->id;
|
||||
$user->save();
|
||||
|
||||
return [
|
||||
'success' => true,
|
||||
'message' => '비밀번호가 변경되었습니다.',
|
||||
];
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user