feat: 최초 로그인 시 비밀번호 변경 강제 기능

- User 모델에 must_change_password 필드 추가
- UserService: createUser(), resetPassword()에서 플래그 설정
- ProfileService: changePassword()에서 플래그 해제
- EnsurePasswordChanged 미들웨어 추가
- 인증 라우트에 password.changed 미들웨어 적용
- 프로필 페이지에 비밀번호 변경 필요 알림 추가

🤖 Generated with [Claude Code](https://claude.com/claude-code)

Co-Authored-By: Claude <noreply@anthropic.com>
This commit is contained in:
2025-12-01 23:44:56 +09:00
parent 817690f544
commit 7ea8997927
7 changed files with 92 additions and 5 deletions

View File

@@ -0,0 +1,39 @@
<?php
namespace App\Http\Middleware;
use Closure;
use Illuminate\Http\Request;
use Symfony\Component\HttpFoundation\Response;
class EnsurePasswordChanged
{
/**
* 비밀번호 변경이 필요한 사용자를 프로필 페이지로 리다이렉트
*
* @param \Closure(\Illuminate\Http\Request): (\Symfony\Component\HttpFoundation\Response) $next
*/
public function handle(Request $request, Closure $next): Response
{
$user = $request->user();
// 로그인한 사용자가 비밀번호 변경이 필요한 경우
if ($user && $user->must_change_password) {
// 프로필 관련 라우트는 허용 (무한 리다이렉트 방지)
if ($request->routeIs('profile.*')) {
return $next($request);
}
// 로그아웃 라우트는 허용
if ($request->routeIs('logout')) {
return $next($request);
}
// 그 외 모든 요청은 프로필 페이지로 리다이렉트
return redirect()->route('profile.index')
->with('warning', '보안을 위해 비밀번호를 변경해주세요.');
}
return $next($request);
}
}

View File

@@ -25,6 +25,7 @@ class User extends Authenticatable
'email',
'phone',
'password',
'must_change_password',
'options',
'profile_photo_path',
'role',
@@ -60,6 +61,7 @@ protected function casts(): array
'options' => 'array',
'is_active' => 'boolean',
'is_super_admin' => 'boolean',
'must_change_password' => 'boolean',
];
}

View File

@@ -40,8 +40,9 @@ public function changePassword(User $user, string $currentPassword, string $newP
];
}
// 비밀번호 업데이트
// 비밀번호 업데이트 + 비밀번호 변경 필요 플래그 해제
$user->password = Hash::make($newPassword);
$user->must_change_password = false;
$user->updated_by = $user->id;
$user->save();

View File

@@ -95,6 +95,9 @@ public function createUser(array $data): User
// is_active 처리
$data['is_active'] = isset($data['is_active']) && $data['is_active'] == '1';
// 최초 로그인 시 비밀번호 변경 필요
$data['must_change_password'] = true;
// 생성자 정보
$data['created_by'] = auth()->id();
@@ -136,8 +139,9 @@ public function resetPassword(int $id): bool
// 임의 비밀번호 생성
$plainPassword = $this->generateRandomPassword();
// 비밀번호 업데이트
// 비밀번호 업데이트 + 비밀번호 변경 필요 플래그
$user->password = Hash::make($plainPassword);
$user->must_change_password = true;
$user->updated_by = auth()->id();
$user->save();