diff --git a/app/Http/Controllers/Auth/LoginController.php b/app/Http/Controllers/Auth/LoginController.php index 97b868e4..fb2379b6 100644 --- a/app/Http/Controllers/Auth/LoginController.php +++ b/app/Http/Controllers/Auth/LoginController.php @@ -28,13 +28,16 @@ public function showLoginForm(): View /** * 로그인 처리 + * - 이메일 또는 아이디로 로그인 가능 + * - 아이디 로그인은 본사 소속만 허용 */ public function login(LoginRequest $request): RedirectResponse { - $credentials = $request->only('email', 'password'); + $credentials = $request->getCredentials(); + $loginField = $request->getLoginField(); $remember = $request->boolean('remember', false); - if ($this->authService->login($credentials, $remember)) { + if ($this->authService->login($credentials, $remember, $loginField)) { $request->session()->regenerate(); return redirect()->intended('/dashboard') @@ -43,11 +46,11 @@ public function login(LoginRequest $request): RedirectResponse // AuthService에서 설정한 오류 메시지 사용 $errorMessage = $this->authService->getLoginError() - ?? '이메일 또는 비밀번호가 올바르지 않습니다.'; + ?? '이메일/아이디 또는 비밀번호가 올바르지 않습니다.'; return back() - ->withErrors(['email' => $errorMessage]) - ->withInput($request->only('email')); + ->withErrors(['login' => $errorMessage]) + ->withInput($request->only('login')); } /** diff --git a/app/Http/Requests/Auth/LoginRequest.php b/app/Http/Requests/Auth/LoginRequest.php index 4962a6f2..0cc936b6 100644 --- a/app/Http/Requests/Auth/LoginRequest.php +++ b/app/Http/Requests/Auth/LoginRequest.php @@ -22,7 +22,7 @@ public function authorize(): bool public function rules(): array { return [ - 'email' => ['required', 'email', 'max:255'], + 'login' => ['required', 'string', 'max:255'], 'password' => ['required', 'string', 'min:8'], 'remember' => ['nullable', 'boolean'], ]; @@ -34,8 +34,7 @@ public function rules(): array public function messages(): array { return [ - 'email.required' => '이메일을 입력해주세요.', - 'email.email' => '올바른 이메일 형식을 입력해주세요.', + 'login.required' => '이메일 또는 아이디를 입력해주세요.', 'password.required' => '비밀번호를 입력해주세요.', 'password.min' => '비밀번호는 최소 8자 이상이어야 합니다.', ]; @@ -47,8 +46,35 @@ public function messages(): array public function attributes(): array { return [ - 'email' => '이메일', + 'login' => '이메일/아이디', 'password' => '비밀번호', ]; } + + /** + * 입력값이 이메일인지 아이디인지 판단 + */ + public function isEmail(): bool + { + return filter_var($this->input('login'), FILTER_VALIDATE_EMAIL) !== false; + } + + /** + * 로그인 필드명 반환 (email 또는 user_id) + */ + public function getLoginField(): string + { + return $this->isEmail() ? 'email' : 'user_id'; + } + + /** + * 인증용 자격증명 반환 + */ + public function getCredentials(): array + { + return [ + $this->getLoginField() => $this->input('login'), + 'password' => $this->input('password'), + ]; + } } diff --git a/app/Services/AuthService.php b/app/Services/AuthService.php index cfb8b40a..5503e9e5 100644 --- a/app/Services/AuthService.php +++ b/app/Services/AuthService.php @@ -20,15 +20,38 @@ public function __construct( /** * 웹 세션 로그인 - * - 본사(HQ) 테넌트 소속만 로그인 허용 + * - 이메일 로그인: 본사(HQ) 테넌트 소속만 허용 + * - 아이디 로그인: 본사(HQ) 테넌트 소속만 허용 + * + * @param array $credentials 인증 정보 ['email' => ..., 'password' => ...] 또는 ['user_id' => ..., 'password' => ...] + * @param bool $remember 로그인 유지 여부 + * @param string $loginField 로그인 필드 ('email' 또는 'user_id') */ - public function login(array $credentials, bool $remember = false): bool + public function login(array $credentials, bool $remember = false, string $loginField = 'email'): bool { $this->loginError = null; + // 아이디 로그인인 경우 사전 검증 + if ($loginField === 'user_id') { + $user = User::where('user_id', $credentials['user_id'])->first(); + + if (! $user) { + $this->loginError = '아이디 또는 비밀번호가 올바르지 않습니다.'; + return false; + } + + // 아이디 로그인은 본사 소속만 허용 + if (! $user->belongsToHQ()) { + $this->loginError = '아이디 로그인은 본사 소속 직원만 가능합니다.'; + return false; + } + } + // 1. 기본 인증 시도 if (! Auth::attempt($credentials, $remember)) { - $this->loginError = '이메일 또는 비밀번호가 올바르지 않습니다.'; + $this->loginError = $loginField === 'email' + ? '이메일 또는 비밀번호가 올바르지 않습니다.' + : '아이디 또는 비밀번호가 올바르지 않습니다.'; return false; } diff --git a/resources/views/auth/login.blade.php b/resources/views/auth/login.blade.php index 2c45331c..89bbd97e 100644 --- a/resources/views/auth/login.blade.php +++ b/resources/views/auth/login.blade.php @@ -41,20 +41,21 @@
{{ $message }}
@enderror +* 아이디 로그인은 본사 소속만 가능합니다.