Skip to content

Commit 28feac3

Browse files
author
bu2000
committed
사용자 성향 부분 백앤드 프런트 연결 완료
1 parent 06d2639 commit 28feac3

11 files changed

Lines changed: 1158 additions & 554 deletions

File tree

src/api/authApi.js

Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
import axios from "axios";
2+
const BACKEND_DOMAIN = "http://localhost:8080/auth";
3+
4+
export const loginAPi = async (userId, password) => {
5+
const response = await axios.post(`${BACKEND_DOMAIN}/login`, {
6+
userId,
7+
password,
8+
});
9+
return response.data;
10+
};
11+
12+
export const signUpApi = async (newUser) => {
13+
const response = await axios.post(`${BACKEND_DOMAIN}/signUp`, newUser);
14+
return response.data;
15+
};
16+
17+
export const findPasswordApi = async (userId, email) => {
18+
const response = await axios.patch(`${BACKEND_DOMAIN}/findPassword`, {
19+
userId,
20+
email,
21+
});
22+
return response.data;
23+
};
24+
25+
export const findUserIdApi = async (email, birthDate) => {
26+
const response = await axios.post(`${BACKEND_DOMAIN}/findUserId`, {
27+
email,
28+
birthDate,
29+
});
30+
return response.data;
31+
};
32+
33+
export const updateTempPasswordApi = async (password) => {
34+
const token = localStorage.getItem("accessToken");
35+
36+
const response = await axios.patch(
37+
`${BACKEND_DOMAIN}/update-tempPassword`,
38+
{ password },
39+
{
40+
headers: {
41+
Authorization: `Bearer ${token}`,
42+
},
43+
}
44+
);
45+
return response.data;
46+
};

src/api/preferenceApi.js

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
import axios from "axios";
2+
const BACKEND_DOMAIN = "http://localhost:8080";
3+
4+
export const getAllergyInfo = async () => {
5+
const token = localStorage.getItem("accessToken");
6+
try {
7+
const response = await axios.get(`${BACKEND_DOMAIN}/allergies`, {
8+
headers: {
9+
Authorization: `Bearer ${token}`,
10+
},
11+
});
12+
return response.data;
13+
} catch (error) {
14+
if (error.response) {
15+
console.log(error.response.status);
16+
console.log(error.response.data);
17+
}
18+
throw error;
19+
}
20+
};
21+
22+
export const getHealthGoalInfo = async () => {
23+
const token = localStorage.getItem("accessToken");
24+
try {
25+
const response = await axios.get(`${BACKEND_DOMAIN}/healthGoals`, {
26+
headers: {
27+
Authorization: `Bearer ${token}`,
28+
},
29+
});
30+
return response.data;
31+
} catch (error) {
32+
if (error.response) {
33+
console.log(error.response.status);
34+
console.log(error.response.data);
35+
}
36+
throw error;
37+
}
38+
};

src/api/userInfo.js

Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
import axios from "axios";
2+
const BACKEND_DOMAIN = "http://localhost:8080/user";
3+
4+
export const makeSurveyApi = async (survey) => {
5+
const token = localStorage.getItem("accessToken");
6+
try {
7+
const response = await axios.post(`${BACKEND_DOMAIN}/survey`, survey, {
8+
headers: {
9+
Authorization: `Bearer ${token}`,
10+
},
11+
});
12+
return response.data;
13+
} catch (error) {
14+
if (error.response) {
15+
console.log(error.response.status);
16+
console.log(error.response.data);
17+
}
18+
throw error;
19+
}
20+
};
21+
22+
export const getUserInfo = async () => {
23+
const token = localStorage.getItem("accessToken");
24+
try {
25+
const response = await axios.get(`${BACKEND_DOMAIN}/mypage`, {
26+
headers: {
27+
Authorization: `Bearer ${token}`,
28+
},
29+
});
30+
return response.data;
31+
} catch (error) {
32+
if (error.response) {
33+
console.log(error.response.status);
34+
console.log(error.response.data);
35+
}
36+
throw error;
37+
}
38+
};
39+
40+
export const updateUserTendency = async (newInfo) => {
41+
const token = localStorage.getItem("accessToken");
42+
try {
43+
const response = await axios.patch(`${BACKEND_DOMAIN}/survey`, newInfo, {
44+
headers: {
45+
Authorization: `Bearer ${token}`,
46+
},
47+
});
48+
return response.data;
49+
} catch (error) {
50+
if (error.response) {
51+
console.log(error.response.status);
52+
console.log(error.response.data);
53+
}
54+
throw error;
55+
}
56+
};
Lines changed: 114 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,114 @@
1+
import { useState } from "react";
2+
import "../style.css";
3+
import { isPasswordValid } from "../../util/validation";
4+
import { updateTempPasswordApi } from "../../../api/authApi";
5+
6+
function ChangePassword({ onSuccess }) {
7+
const [password, setPassword] = useState("");
8+
const [confirmPassword, setConfirmPassword] = useState("");
9+
10+
const [errors, setErrors] = useState({
11+
password: "",
12+
confirmPassword: "",
13+
});
14+
15+
async function handleChangePassword(e) {
16+
e.preventDefault();
17+
18+
let newErrors = {};
19+
20+
if (!isPasswordValid(password)) {
21+
newErrors.password =
22+
"영문자, 특수문자 포함 최소 10자 ~ 최대 20자로 입력해주세요.";
23+
}
24+
25+
if (password !== confirmPassword) {
26+
newErrors.confirmPassword = "비밀번호가 일치하지 않습니다.";
27+
}
28+
29+
setErrors(newErrors);
30+
if (Object.keys(newErrors).length > 0) {
31+
return;
32+
}
33+
34+
try {
35+
const result = await updateTempPasswordApi(password);
36+
37+
if (result) {
38+
alert(result.message || "비밀번호가 성공적으로 변경되었습니다.");
39+
40+
if (onSuccess) {
41+
onSuccess();
42+
}
43+
}
44+
} catch (error) {
45+
console.error("비밀번호 변경 에러:", error);
46+
alert("비밀번호 변경에 실패했습니다. 잠시 후 다시 시도해주세요.");
47+
}
48+
}
49+
50+
const handleInputChange = (setter, field, value) => {
51+
setter(value);
52+
if (errors[field]) {
53+
setErrors({ ...errors, [field]: "" });
54+
}
55+
};
56+
57+
return (
58+
<form className="login-popup-form" onSubmit={handleChangePassword}>
59+
<div className="login-popup-input-group">
60+
<label htmlFor="password" className="login-popup-label">
61+
새 비밀번호
62+
</label>
63+
<input
64+
className={`login-popup-input ${
65+
errors.password ? "input-error" : ""
66+
}`}
67+
type="password"
68+
value={password}
69+
onChange={(e) =>
70+
handleInputChange(setPassword, "password", e.target.value)
71+
}
72+
required
73+
name="password"
74+
placeholder="영문, 특수문자 포함 10~20자"
75+
/>
76+
{errors.password && (
77+
<span className="error-message">{errors.password}</span>
78+
)}
79+
</div>
80+
81+
<div className="login-popup-input-group">
82+
<label htmlFor="confirmPassword" className="login-popup-label">
83+
새 비밀번호 확인
84+
</label>
85+
<input
86+
className={`login-popup-input ${
87+
errors.confirmPassword ? "input-error" : ""
88+
}`}
89+
type="password"
90+
value={confirmPassword}
91+
onChange={(e) =>
92+
handleInputChange(
93+
setConfirmPassword,
94+
"confirmPassword",
95+
e.target.value
96+
)
97+
}
98+
required
99+
name="confirmPassword"
100+
placeholder="비밀번호 확인"
101+
/>
102+
{errors.confirmPassword && (
103+
<span className="error-message">{errors.confirmPassword}</span>
104+
)}
105+
</div>
106+
107+
<button type="submit" className="login-popup-submit-btn">
108+
변경 완료
109+
</button>
110+
</form>
111+
);
112+
}
113+
114+
export default ChangePassword;

src/components/LoginPopup/LoginPopup.jsx

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@ export const LoginPopup = ({
1919
const result = await login(id, password);
2020
if (result.status === "SUCCESS") {
2121
console.log("?α??? ????");
22+
localStorage.setItem("userId", id);
2223
onClose();
2324
} else if (result.status === "FORCE_PASSWORD_CHANGE") {
2425
console.log("????й?? ?????????.");

0 commit comments

Comments
 (0)