diff --git a/src/API/AuthService.js b/src/API/AuthService.js index fa2268f8ceb0d2ac911c0fd9d47ef6aab168ffe8..f39065bf99863821505a73832e3bd2e26c5146de 100644 --- a/src/API/AuthService.js +++ b/src/API/AuthService.js @@ -45,6 +45,31 @@ const AuthService = { const data = await response.json(); return data.accessToken; }, + sendEmail: async (emailValue) => { + const response = await fetch(`${API_Auth_URL}/mail`, { + method: "POST", + headers: { + "Content-Type": "application/json", + }, + body: JSON.stringify({ + email: emailValue, + }), + }); + return response; + }, + mailVerify: async (email, verifyNum) => { + const response = await fetch(`${API_Auth_URL}/mail/verify`, { + method: "POST", + headers: { + "Content-Type": "application/json", + }, + body: JSON.stringify({ + email: email, + authCode: verifyNum, + }), + }); + return response; + } }; export default AuthService; \ No newline at end of file diff --git a/src/API/ChatService.js b/src/API/ChatService.js index c62322d3a016478e1fd44affe7313c421aad3200..6ae98d60b67eb7547b3b1042cb4a07134e6b599c 100644 --- a/src/API/ChatService.js +++ b/src/API/ChatService.js @@ -31,6 +31,22 @@ const ChatService = { }); const data = await response.json(); return data; + }, + getMessages: async (roomId, startId) => { + let apiUrl = `${API_User_URL}/messages/${roomId}`; + if (startId) { + apiUrl = apiUrl + `?startId=${startId}`; + } + + const response = await fetch(apiUrl, { + method: 'GET', + headers: { + 'Content-Type': 'application/json', + 'Authorization': `${accessToken}`, + }, + }); + const data = await response.json(); + return data; } }; diff --git a/src/App.js b/src/App.js index abc947a5d02cfc082a5499871bbcb94104606850..fdbf7e1ed642e68ee4bf55b482ce5f4e30afcecd 100644 --- a/src/App.js +++ b/src/App.js @@ -12,7 +12,6 @@ function App() { const [route, setRoute] = useState(window.location.pathname); useEffect(() => { - console.log(route); const handleLocationChange = () => { setRoute(window.location.pathname); }; diff --git a/src/Components/Auth/Signin/EmailButton.js b/src/Components/Auth/Signin/EmailButton.js index 5e0a32b58eaa577c24e112906183d1812e6763fb..07816129215c37b4d5a7af50e73807e903896bb3 100644 --- a/src/Components/Auth/Signin/EmailButton.js +++ b/src/Components/Auth/Signin/EmailButton.js @@ -1,4 +1,5 @@ import React, { useState } from "react"; +import authService from "../../../API/AuthService"; function EmailButton({ onClick, @@ -20,22 +21,8 @@ function EmailButton({ const handleCheckboxClick = async () => { setWarningMessage(``); - try { - const response = await fetch( - "http://localhost:8080/api/auth/mail/verify", - { - method: "POST", - headers: { - "Content-Type": "application/json", - }, - body: JSON.stringify({ - email: email, - authCode: verifyNum, - }), - } - ); - + const response = await authService.mailVerify(email, verifyNum); if (response.ok) { setVerificationComplete(true); setDisableValue(true); @@ -46,7 +33,7 @@ function EmailButton({ setWarningMessage(errorResponse.error); } } catch (error) { - setWarningMessage(`${error.response.data.error}`); + setWarningMessage('${error.response.data.error}'); } }; diff --git a/src/Components/Chat/ChatScreen.js b/src/Components/Chat/ChatScreen.js index 804fb3b997be42b1e4f189206d4de47947614edd..16a54d88f3b5e112c54e64406b7b1ab0e7808bbd 100644 --- a/src/Components/Chat/ChatScreen.js +++ b/src/Components/Chat/ChatScreen.js @@ -2,7 +2,8 @@ import React, { useState, useEffect } from "react"; import { useSocket } from "../../Contexts/SocketContext"; import { FontAwesomeIcon } from "@fortawesome/react-fontawesome"; import { faArrowUp } from "@fortawesome/free-solid-svg-icons"; -import {} from "../../Contexts/AuthContext"; +import chatService from "../../API/ChatService" +import userService from "../../API/UserService" import Message from "./Message"; function ChatScreen() { @@ -34,42 +35,15 @@ function ChatScreen() { } } - const fetchMesssages = () => { - let apiUrl = `http://localhost:8080/api/chat/messages/${roomId}`; - if (startId) { - apiUrl = apiUrl + `?startId=${startId}`; - } - const requestOptions = { - method: "GET", - headers: { - "Content-Type": "application/json", - Authorization: accessToken, - }, - }; - - fetch(apiUrl, requestOptions) - .then((response) => response.json()) - .then((res) => { - setStartId(res.nextId); - setMessages(res.messages); - }) - .catch((error) => console.error("Error:", error)); + const fetchMesssages = async () => { + const response = await chatService.getMessages(roomId, startId); + setStartId(response.nextId); + setMessages(response.messages); }; - const fetchUserInfo = () => { - const apiUrl = "http://localhost:8080/api/user/profile"; - const requestOptions = { - method: "GET", - headers: { - "Content-Type": "application/json", - Authorization: accessToken, - }, - }; - - fetch(apiUrl, requestOptions) - .then((response) => response.json()) - .then((res) => setUserId(res.id)) - .catch((error) => console.error("Error:", error)); + const fetchUserInfo = async () => { + const response = await userService.searchProfile(); + setUserId(response.id); }; const fetchAllUserInfo = () => { diff --git a/src/Components/Common/CommonForm.js b/src/Components/Common/CommonForm.js index 7e9a3b66705f7a6143271d9d119ccdbfc23b7207..45bb78b4ccc4afc3add3e084e99fb1cfbc5ef0df 100644 --- a/src/Components/Common/CommonForm.js +++ b/src/Components/Common/CommonForm.js @@ -1,6 +1,7 @@ import React from "react"; import { useState } from "react"; import { EmailButton } from '../Auth/Signin/EmailButton.js'; +import authService from "../../API/AuthService.js"; function CommonForm({ onSubmit, buttonText, fields, showVerificationButton, setWarningMessage }) { @@ -15,16 +16,7 @@ function CommonForm({ onSubmit, buttonText, fields, showVerificationButton, setW setEmailMessage("전송 중"); setDisableValue(true); try { - const response = await fetch("http://localhost:8080/api/auth/mail", { - method: "POST", - headers: { - "Content-Type": "application/json", - }, - body: JSON.stringify({ - email: emailValue, - }), - }); - + const response = await authService.sendEmail(emailValue); if (response.ok) { setEmailMessage("재전송"); setDisableValue(false); diff --git a/src/Pages/Auth/LoginApp.js b/src/Pages/Auth/LoginApp.js deleted file mode 100644 index 0dfac28e97f366d43f2e79a5927e13534dd24922..0000000000000000000000000000000000000000 --- a/src/Pages/Auth/LoginApp.js +++ /dev/null @@ -1,162 +0,0 @@ -import React, { useState, useContext } from "react"; -import { useNavigate } from "react-router-dom"; -import { AuthContext } from "../../AuthContext"; -import authService from "../../AuthService"; -import WarningMessage from "../../Components/Common/WarningMessage"; - -import "../../css/components/loginPage.css"; - -function LoginPage() { - const navigate = useNavigate(); - const { signIn } = useContext(AuthContext); - - const [isLoginView, setIsLoginView] = useState(true); - const [isFindIdView, setIsFindIdView] = useState(false); - const [isFindPasswordView, setIsFindPasswordView] = useState(false); - const [warningMessage, setWarningMessage] = useState(""); - - const handleToggleView = () => { - setIsLoginView(!isLoginView); - setIsFindIdView(false); - setIsFindPasswordView(false); - setWarningMessage(""); - }; - - const handleFindIdView = () => { - setIsFindIdView(true); - setIsFindPasswordView(false); - setIsLoginView(false); - setWarningMessage(""); - }; - - const handleFindPasswordView = () => { - setIsFindPasswordView(true); - setIsFindIdView(false); - setIsLoginView(false); - setWarningMessage(""); - }; - - const handleSubmit = async (e) => { - e.preventDefault(); - const { username, email, password } = e.target.elements; - const userData = { - user_name: isLoginView ? "" : username.value, - email: email.value, - password: password.value, - }; - - if (isLoginView) { - try { - const response = await authService.login(userData); - if (response.data.accessToken) { - signIn(response.data.accessToken, userData); - - // navigate("/friends"); - } else { - throw new Error("No access token received"); - } - } catch (error) { - setWarningMessage(`${error.response.data.error}`); - } - } else { - try { - const response = await authService.signUp(userData); - if (response.data.accessToken) { - signIn(response.data.accessToken, userData); - - navigate("/friends"); - } else { - throw new Error("No access token received"); - } - } catch (error) { - console.log(error); - setWarningMessage(`${error.response.data.error}`); - } - } - }; - - if (isFindIdView) { - return <div>ID 찾기</div>; - } - - if (isFindPasswordView) { - return <div>PWD 찾기</div>; - } - - return ( - <div className="login-container"> - <header className="welcome-header"> - <h1 className="login-header"> - {isLoginView ? "CaKaA Talk 로그인" : "회원가입"} - </h1> - <p className="login-description"> - {isLoginView - ? "아주대학교 이메일로 로그인 할 수 있습니다." - : "새로운 계정을 만드세요."} - </p> - <form onSubmit={handleSubmit} id="login-form"> - {!isLoginView && ( - <input - name="username" - type="text" - placeholder="username" - className="input-username" - required - /> - )} - <br></br> - <input - name="email" - type="text" - placeholder="Ajou University Email (@ajou.ac.kr)" - className="input-email" - required - /> - <input - name="password" - type="password" - placeholder="password" - className="input-password" - required - /> - <input - type="submit" - value={isLoginView ? "로그인" : "회원가입"} - className="login-button" - /> - </form> - {warningMessage && <WarningMessage message={warningMessage} />} - <div className="additional-options"> - <span - onClick={handleToggleView} - role="button" - tabIndex={0} - className="text-button" - > - {isLoginView ? "회원가입" : "로그인으로 돌아가기"} - </span> - <span className="text-separator">/</span> - <span - onClick={handleFindIdView} - role="button" - tabIndex={0} - className="text-button" - > - 아이디 찾기 - </span> - <span className="text-separator">/</span> - <span - onClick={handleFindPasswordView} - role="button" - tabIndex={0} - className="text-button" - > - 패스워드 찾기 - </span> - </div> - </header> - </div> - ); -} - -export default LoginPage; diff --git a/src/Pages/FindAccount/FindAccount.js b/src/Pages/FindAccount/FindAccount.js index 06faedd9186fb2de3ca5a9b35751e2ed18a1cf05..e663e78cbbe4fc14ecfac8ad2b4c75475d76b02e 100644 --- a/src/Pages/FindAccount/FindAccount.js +++ b/src/Pages/FindAccount/FindAccount.js @@ -1,5 +1,4 @@ import { FindAccountArea } from "../FindAccount/FindAccountArea"; -import AuthNavigation from "../../Components/Auth/AuthNavigation"; import "../../css/components/loginPage.css"; function FindAccount() { @@ -9,7 +8,7 @@ function FindAccount() { <header className="welcome-header"> <FindAccountArea /> - <AuthNavigation isLoginView={0} /> + {/* <AuthNavigation isLoginView={0} /> */} </header> </div> ); diff --git a/src/Pages/SignUp/SignUp.js b/src/Pages/SignUp/SignUp.js index bb292238ef029ec3f359d287941febf67c3e5409..e4014482a925967824e427fa6993044d0a53607e 100644 --- a/src/Pages/SignUp/SignUp.js +++ b/src/Pages/SignUp/SignUp.js @@ -1,5 +1,4 @@ import InputArea from "../../Components/Auth/Signin/InputArea"; -import AuthNavigation from "../../Components/Auth/AuthNavigation"; import "../../css/components/loginPage.css"; @@ -8,7 +7,7 @@ function SignUp(){ <div className="signUp-container"> <header className="signUp-header"> <InputArea isLoginView={0} /> - <AuthNavigation isLoginView={0} /> + </header> </div> );