Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[Fix] 채팅 관련 API V2 연결 #170

Merged
merged 19 commits into from
Jan 15, 2025
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
Show all changes
19 commits
Select commit Hold shift + click to select a range
9103730
fix(#169): 채팅 api v2 연결
HelloWorldDogs223 Jan 5, 2025
57b8cab
fix(#169) type 지정
HelloWorldDogs223 Jan 7, 2025
36d3761
feat(#164): CategoryToggle 컴포넌트 제작
yyypearl Jan 9, 2025
14cd37a
feat(#164): 겜구 약관 및 개인정보 보호 페이지 제작, 경로 설정
yyypearl Jan 9, 2025
6c3bda4
refactor(#164): /policy 하위 페이지 params를 이용해 하나의 페이지로 통합
yyypearl Jan 9, 2025
c05827b
Merge pull request #165 from Gamegoo-repo/feat/#164
HelloWorldDogs223 Jan 9, 2025
bd55833
fix(#164): useSearchParams 클라이언트 실행을 위한 Suspense 설정
yyypearl Jan 9, 2025
aa46260
Merge pull request #166 from Gamegoo-repo/feat/#164
yyypearl Jan 9, 2025
0064aeb
fix(#164): Suspense 내 children 추가
yyypearl Jan 9, 2025
f367671
Merge pull request #167 from Gamegoo-repo/feat/#164
yyypearl Jan 9, 2025
1cef6e9
fix(#169) 주소 변경
HelloWorldDogs223 Jan 12, 2025
b55a3a9
Merge branch 'develop' of https://github.com/Gamegoo-repo/Gamegoo-fro…
HelloWorldDogs223 Jan 12, 2025
326ffe9
Merge branch 'server2' of https://github.com/Gamegoo-repo/Gamegoo-fro…
HelloWorldDogs223 Jan 12, 2025
55ccc8d
fix(#169) 채팅 api2 연결
HelloWorldDogs223 Jan 12, 2025
4193cfa
fix(#169): api param 수정
HelloWorldDogs223 Jan 12, 2025
d979b27
fix(#160) base URL 수정
HelloWorldDogs223 Jan 14, 2025
33e5a69
fix(#169) 빌드 에러 해결
HelloWorldDogs223 Jan 14, 2025
7bb1cb6
fix(#160) 빌드 에러 해결2
HelloWorldDogs223 Jan 14, 2025
dc758cc
fix(#169) 진짜 빌드 해결
HelloWorldDogs223 Jan 14, 2025
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
fix(#169) type 지정
  • Loading branch information
HelloWorldDogs223 committed Jan 7, 2025
commit 57b8cab64779e8c9f75deaa53d6624b3e8882f96
59 changes: 31 additions & 28 deletions src/api/auth.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import { notify } from "@/hooks/notify";
import { connectSocket } from "@/socket";

export const reissueToken = async () => {
const endpoint = '/v1/member/refresh';
const endpoint = "/v1/member/refresh";

try {
const refreshToken = getRefreshToken();
Expand All @@ -23,7 +23,7 @@ export const reissueToken = async () => {
export const AuthAxios: AxiosInstance = axios.create({
baseURL: BASE_URL,
headers: {
'Content-Type': 'application/json;charset=UTF-8',
"Content-Type": "application/json;charset=UTF-8",
},
});

Expand All @@ -32,7 +32,7 @@ AuthAxios.interceptors.request.use(
(config: InternalAxiosRequestConfig) => {
const token = getAccessToken();
if (token) {
config.headers['Authorization'] = `Bearer ${token}`;
config.headers["Authorization"] = `Bearer ${token}`;
}
return config;
},
Expand All @@ -47,41 +47,44 @@ AuthAxios.interceptors.response.use(
return response;
},
async (error) => {
const { config, response: { status } } = error;
const {
config,
response: { status },
} = error;

/* 토큰 만료 시 */
if (status === 401) {
console.log("AuthAxios 401 Error Response Interceptor");

try {
/* 토큰 재발급 요청 */
/* 토큰 재발급 요청 */
const response = await reissueToken();
const newAccessToken = response.result.accessToken;
const originRequest = config; // 이전 요청 저장
const originRequest = config; // 이전 요청 저장

// 로컬 또는 세션에 재발급된 토큰 저장
if (localStorage.getItem('accessToken')) {
localStorage.setItem('accessToken', newAccessToken);
localStorage.setItem('refreshToken', response.result.refreshToken);
} else {
sessionStorage.setItem('accessToken', newAccessToken);
sessionStorage.setItem('refreshToken', response.result.refreshToken);
}
connectSocket();
originRequest.headers['Authorization'] = `Bearer ${newAccessToken}`;
return axios(originRequest);
} catch (reissueError:any) {
if(reissueError.response && reissueError.response.status === 404){
notify({ text: LOGIN.MESSAGE.EXPIRED, icon: '🚫', type: 'error' });
} else {
notify({ text: LOGIN.MESSAGE.ETC, icon: '🚫', type: 'error' });
}
// 토큰 재발급 실패 시 처리
console.error("토큰 재발급 실패:", reissueError);
clearTokens(); // 저장된 토큰 삭제
window.location.replace('/login'); // 로그인 페이지로 이동
if (localStorage.getItem("accessToken")) {
localStorage.setItem("accessToken", newAccessToken);
localStorage.setItem("refreshToken", response.result.refreshToken);
} else {
sessionStorage.setItem("accessToken", newAccessToken);
sessionStorage.setItem("refreshToken", response.result.refreshToken);
}
connectSocket();
originRequest.headers["Authorization"] = `Bearer ${newAccessToken}`;
return axios(originRequest);
} catch (reissueError: any) {
if (reissueError.response && reissueError.response.status === 404) {
notify({ text: LOGIN.MESSAGE.EXPIRED, icon: "🚫", type: "error" });
} else {
notify({ text: LOGIN.MESSAGE.ETC, icon: "🚫", type: "error" });
}
// 토큰 재발급 실패 시 처리
console.error("토큰 재발급 실패:", reissueError);
clearTokens(); // 저장된 토큰 삭제
window.location.replace("/login"); // 로그인 페이지로 이동
}
}
return Promise.reject(error);
}
);
);
12 changes: 6 additions & 6 deletions src/api/chat.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import {
charRoomExitResponse,
chatRoomExitResponse,
chatRoomReadRequest,
charRoomSearchResponse,
chatRoomSearchResponse,
chatRoomExitRequest,
chatRoomReadData,
chatRoomMessageRequest,
Expand Down Expand Up @@ -94,7 +94,7 @@ export const getChatList = async ({
/* 채팅방 나가기 */
export const leaveChatroom = async (
uuid: chatRoomExitRequest
): Promise<charRoomExitResponse> => {
): Promise<chatRoomExitResponse> => {
try {
const response = await AuthAxios.patch(
`http://13.124.213.255:8080/api/v2/chat/${uuid}/exit`
Expand All @@ -111,8 +111,8 @@ export const markChatAsRead = async ({
timestamp,
}: chatRoomReadRequest): Promise<chatRoomReadData> => {
const url = timestamp
? `http://13.124.213.255:8080/v2/chat/${uuid}/read?timestamp=${timestamp}`
: `http://13.124.213.255:8080/v2/chat/${uuid}/read`;
? `http://13.124.213.255:8080/api/v2/chat/${uuid}/read?timestamp=${timestamp}`
: `http://13.124.213.255:8080/api/v2/chat/${uuid}/read`;
try {
const response = await AuthAxios.patch(url);
return response.data;
Expand All @@ -122,7 +122,7 @@ export const markChatAsRead = async ({
};

/* 안 읽은 채팅방 uuid 가져오기 */
export const getUnreadUuid = async (): Promise<charRoomSearchResponse> => {
export const getUnreadUuid = async (): Promise<chatRoomSearchResponse> => {
try {
const response = await AuthAxios.get(
"http://13.124.213.255:8080/api/v2/chat/unread"
Expand Down
43 changes: 20 additions & 23 deletions src/api/login.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,29 +2,26 @@ import axios from "axios";
import { BASE_URL } from ".";

interface loginProps {
email: string;
password: string;
email: string;
password: string;
}

export const postLogin = async ({
email,
password,
}: loginProps) => {
try {
const formData = new URLSearchParams();
formData.append('email', email);
formData.append('password', password);
export const postLogin = async ({ email, password }: loginProps) => {
try {
const formData = new URLSearchParams();
formData.append("email", email);
formData.append("password", password);

const response = await axios.post("/v1/member/login", formData, {
baseURL: BASE_URL,
headers: {
"Content-Type": "application/x-www-form-urlencoded",
},
});
return response.data;
} catch (error) {
console.error('로그인 실패:', error);
throw error;
}
};
const response = await axios.post("/v1/member/login", formData, {
baseURL: BASE_URL,
headers: {
"Content-Type": "application/x-www-form-urlencoded",
},
});

return response.data;
} catch (error) {
console.error("로그인 실패:", error);
throw error;
}
};
5 changes: 3 additions & 2 deletions src/app/page.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import { theme } from "@/styles/theme";
import { useState } from "react";
import Alert from "@/components/common/Alert";
import { getAccessToken } from "@/utils/storage";
import { AuthAxios } from "@/api/auth";

const HomePage = () => {
const router = useRouter();
Expand Down Expand Up @@ -119,8 +120,8 @@ const ContentWrapper = styled.div`
`;

const StyledObject = styled.object`
pointer-events: none;
`
pointer-events: none;
`;

const ContentTitle = styled.p`
position: absolute;
Expand Down
6 changes: 3 additions & 3 deletions src/types/chat/chat.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -174,9 +174,9 @@ export interface chatRoomFriendData {
}

// response
export type charRoomExitResponse = ApiResponse<LoginData>;
export type charRoomReadResponse = ApiResponse<chatRoomReadData>;
export type charRoomSearchResponse = ApiResponse<chatRoomSearchUuid>;
export type chatRoomExitResponse = ApiResponse<LoginData>;
export type chatRoomReadResponse = ApiResponse<chatRoomReadData>;
export type chatRoomSearchResponse = ApiResponse<chatRoomSearchUuid>;
export type chatRoomMessageResponse = ApiResponse<chatRoomMessageData>;
export type chatRoomEnterResponse = ApiResponse<chatRoomEnterData>;
export type chatRoomGetResponse = ApiResponse<chatRoomGetData>;
Expand Down