Skip to content

Commit d9553e7

Browse files
committed
REFACTOR: 비밀번호 변경 API 모듈화
1 parent 456f6d2 commit d9553e7

File tree

4 files changed

+69
-20
lines changed

4 files changed

+69
-20
lines changed
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
import axiosInstance from '../axiosInstance';
2+
3+
export interface ChangePasswordRequest {
4+
old_password: string;
5+
new_password: string;
6+
}
7+
8+
export async function changePassword(userId: string, body: ChangePasswordRequest): Promise<void> {
9+
const response = await axiosInstance.patch(`/user/${userId}`, body);
10+
return response.data;
11+
}

react-app/src/pages/ChangePW.tsx

Lines changed: 52 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,55 @@
1+
import { useState } from 'react';
2+
import { changePassword } from '@/apis/user/changePassword';
3+
14
const ChangePW = () => {
2-
return (
3-
<div>
4-
5-
</div>
6-
);
5+
const [oldPassword, setOldPassword] = useState('');
6+
const [newPassword, setNewPassword] = useState('');
7+
8+
const handleChangePassword = async () => {
9+
try {
10+
const userId = localStorage.getItem('userId');
11+
if (!userId) throw new Error('로그인 정보 없음');
12+
13+
await changePassword(userId, {
14+
old_password: oldPassword,
15+
new_password: newPassword,
16+
});
17+
18+
alert('비밀번호가 변경되었습니다.');
19+
setOldPassword('');
20+
setNewPassword('');
21+
} catch (err) {
22+
alert('비밀번호 변경 실패');
23+
console.error(err);
24+
}
25+
};
26+
27+
return (
28+
<div className="max-w-md mx-auto ">
29+
<h2 className="text-xl font-bold mb-6 text-center">비밀번호 변경 테스트</h2>
30+
<div className="mb-4">
31+
<label className="font-pm block mb-1">현재 비밀번호</label>
32+
<input
33+
type="password"
34+
value={oldPassword}
35+
onChange={(e) => setOldPassword(e.target.value)}
36+
className="w-full px-3 py-2 border rounded focus:ring focus:outline-none"
37+
/>
38+
</div>
39+
<div className="mb-6">
40+
<label className="font-pm block mb-1">새 비밀번호</label>
41+
<input
42+
type="password"
43+
value={newPassword}
44+
onChange={(e) => setNewPassword(e.target.value)}
45+
className="w-full px-3 py-2 border rounded focus:ring focus:outline-none"
46+
/>
47+
</div>
48+
<button onClick={handleChangePassword} className="w-full py-2 text-white rounded ">
49+
변경하기
50+
</button>
51+
</div>
52+
);
753
};
854

9-
export default ChangePW;
55+
export default ChangePW;

react-app/src/pages/Home.tsx

Lines changed: 1 addition & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,3 @@
1-
import { useToast } from '@/hook/useToast';
2-
31
export default function Home() {
4-
const { showToast } = useToast();
5-
6-
const handleButtonClick = () => {
7-
showToast('버튼이 클릭되었습니다', 'success');
8-
};
9-
10-
return (
11-
<div>
12-
<h1>Hello</h1>
13-
<button onClick={handleButtonClick}>토스트 표시</button>
14-
</div>
15-
);
2+
return <div></div>;
163
}

react-app/src/routes.tsx

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ import Layout from '@/components/Layout/Layout';
33
import Login from './pages/Login';
44
import Home from './pages/Home';
55
import Vote from './pages/Vote';
6+
import ChangePW from './pages/ChangePW';
67

78
const router = createBrowserRouter([
89
{
@@ -21,6 +22,10 @@ const router = createBrowserRouter([
2122
path: 'vote',
2223
element: <Vote />,
2324
},
25+
{
26+
path: 'change-password',
27+
element: <ChangePW />,
28+
},
2429
],
2530
},
2631
]);

0 commit comments

Comments
 (0)