Skip to content

Commit

Permalink
#184 fix: 학생회원일시 application/json, 관리자 시 multipart/form-data로 로그인 요청
Browse files Browse the repository at this point in the history
  • Loading branch information
pillow12360 committed Dec 23, 2024
1 parent 811e6f9 commit 24a8441
Show file tree
Hide file tree
Showing 2 changed files with 59 additions and 32 deletions.
63 changes: 36 additions & 27 deletions frontend/src/context/AuthContext.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -8,16 +8,20 @@ import React, {
import axios from 'axios';
import { apiEndpoints } from '../config/apiConfig';

// AuthContext 타입 정의
// 로그인 크리덴셜 타입 정의
interface LoginCredentials {
loginId: string;
password: string;
}

interface AuthContextType {
user: string | null;
isAuthenticated: boolean;
isAdmin: boolean;
isLoading: boolean;
error: string | null;
signin: (
username: string,
password: string,
credentials: FormData | LoginCredentials,
isAdminLogin?: boolean,
) => Promise<void>;
signout: () => Promise<void>;
Expand All @@ -41,7 +45,6 @@ export const AuthProvider: React.FC<AuthProviderProps> = ({ children }) => {

const clearError = () => setError(null);

// 인증 상태 설정 함수
const setAuthState = (userName: string | null, adminRole = false) => {
if (userName) {
localStorage.setItem('user', userName);
Expand All @@ -58,7 +61,6 @@ export const AuthProvider: React.FC<AuthProviderProps> = ({ children }) => {
}
};

// 로그아웃 함수
const signout = useCallback(async () => {
try {
await axios.post(apiEndpoints.admin.signOut);
Expand All @@ -70,7 +72,6 @@ export const AuthProvider: React.FC<AuthProviderProps> = ({ children }) => {
}
}, []);

// 초기화
useEffect(() => {
const initializeAuth = () => {
const savedUser = localStorage.getItem('user');
Expand All @@ -85,40 +86,48 @@ export const AuthProvider: React.FC<AuthProviderProps> = ({ children }) => {
}, []);

const signin = async (
userName: string,
password: string,
credentials: FormData | LoginCredentials,
isAdminLogin = false,
) => {
if (!userName || !password) {
setError('아이디와 비밀번호를 입력해주세요.');
if (!credentials) {
setError('로그인 정보가 누락되었습니다.');
return;
}

setIsLoading(true);
clearError();

try {
const formData = new FormData();

// 관리자/학생 로그인에 따라 다른 프로퍼티명 사용
if (isAdminLogin) {
formData.append('loginId', userName); // 관리자용 ID 프로퍼티
formData.append('password', password);
} else {
formData.append('loginId', String(userName)); // 학생용 ID 프로퍼티
formData.append('password', String(password));
}

// 관리자/학생 로그인 엔드포인트 선택
const loginEndpoint = isAdminLogin
? apiEndpoints.admin.login
: apiEndpoints.user.login;

const response = await axios.post(loginEndpoint, formData, {
headers: {
'Content-Type': 'application/json',
},
});
let response;
let userName: string;

if (isAdminLogin) {
// 관리자 로그인: multipart/form-data
if (!(credentials instanceof FormData)) {
throw new Error('관리자 로그인에는 FormData가 필요합니다.');
}
response = await axios.post(loginEndpoint, credentials, {
headers: {
'Content-Type': 'multipart/form-data',
},
});
userName = String(credentials.get('loginId'));
} else {
// 학생 로그인: application/json
if (credentials instanceof FormData) {
throw new Error('학생 로그인에는 JSON 형식이 필요합니다.');
}
response = await axios.post(loginEndpoint, credentials, {
headers: {
'Content-Type': 'application/json',
},
});
userName = credentials.loginId;
}

if (response.status === 200) {
setAuthState(userName, isAdminLogin);
Expand Down
28 changes: 23 additions & 5 deletions frontend/src/pages/Auth/SignInPage.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -55,15 +55,31 @@ const SignInPage: React.FC = () => {

try {
setIsSubmitting(true);
await signin(loginId, password, activeTab === 'admin');

// 관리자/학생 로그인 데이터 준비
const isAdminLogin = activeTab === 'admin';
let loginData;

if (isAdminLogin) {
// 관리자 로그인: FormData 형식
loginData = new FormData();
loginData.append('loginId', loginId);
loginData.append('password', password);
} else {
// 학생 로그인: JSON 형식
loginData = {
loginId,
password,
};
}

await signin(loginData, isAdminLogin);

openModal(
<>
<Modal.Header>로그인 성공</Modal.Header>
<Modal.Content>
<p>
{activeTab === 'admin' ? '관리자' : '학생'} 계정으로
로그인되었습니다.
</p>
<p>{isAdminLogin ? '관리자' : '학생'} 계정으로 로그인되었습니다.</p>
</Modal.Content>
<Modal.Footer>
<Button
Expand Down Expand Up @@ -95,6 +111,8 @@ const SignInPage: React.FC = () => {
}
};

// ... 나머지 JSX 반환 부분은 동일 ...

return (
<Container>
<ContentWrapper>
Expand Down

0 comments on commit 24a8441

Please sign in to comment.