Skip to content

Commit

Permalink
[Hotfix] 로그인시 안 보이는 문제 수정
Browse files Browse the repository at this point in the history
  • Loading branch information
jpark0506 committed Aug 22, 2024
1 parent 516d4f7 commit 6b5f498
Show file tree
Hide file tree
Showing 3 changed files with 48 additions and 46 deletions.
5 changes: 3 additions & 2 deletions src/main/components/search/search.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -28,8 +28,9 @@ const Search = (props: Props) => {

if (isFetching || stop) return;// 이미 요청 중이거나 중지 상태이면 반환
setIsFetching(true);
console.log(isAuthenticated);
try {
const res = isAuthenticated ?
const res = isAuthenticated === false?
await axiosAPI.get(`/api/v2/promotions/search?currentPage=${page}&keyword=${encodeURIComponent(query)}`) :
await axiosSemiSecureAPI.get(`/api/v2/promotions/search/auth?currentPage=${page}&keyword=${encodeURIComponent(query)}`)
const result = res.data.result.promotionList;
Expand Down Expand Up @@ -59,7 +60,7 @@ const Search = (props: Props) => {
setResults([]);
setPage(0);
setStop(false);// 새 검색 시 정지 상태 해제
}, [query]);
}, [query, isLoading]);


// 페이지가 변경될 때 결과를 가져오기
Expand Down
8 changes: 4 additions & 4 deletions src/promotion/promotionmain.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -25,8 +25,10 @@ const PromotionView = (props: Props) => {

const fetchData = async () => {
if(isAuthLoading)return;

try {
const response = isAuthenticated ?
console.log(isAuthenticated);
const response = isAuthenticated === true ?
await axiosSemiSecureAPI.get(`/api/v2/promotions/${id}/auth`)
:
await axiosAPI.get(`/api/v2/promotions/${id}`);
Expand All @@ -41,12 +43,10 @@ const PromotionView = (props: Props) => {
};

useEffect(() => {
if(isAuthenticated){
if(isAuthLoading)return;
setIsLoading(true);
fetchData();
setIsLoading(false);

}

}, [isAuthLoading]);

Expand Down
81 changes: 41 additions & 40 deletions src/utils/hooks/useCheckAuth.ts
Original file line number Diff line number Diff line change
@@ -1,61 +1,62 @@
import { useEffect, useState } from 'react';
import { axiosSemiSecureAPI } from '../../axios/index';
import { Member } from '../../promotion/types/common';
import { handleApiError } from '../error';
import axios from 'axios';
import store from '../../store/store';

import { useEffect, useState } from "react";
import { axiosSemiSecureAPI } from "../../axios/index";
import { Member } from "../../promotion/types/common";
import { handleApiError } from "../error";
import axios from "axios";
import store from "../../store/store";

const useCheckAuth = () => {
const [isAuthenticated, setIsAuthenticated] = useState<boolean>(false);
const [memberInfo, setMemberInfo] = useState<Member>({ id: -1, name: '', nickname: '', profileImg: '' });
const [memberInfo, setMemberInfo] = useState<Member>({
id: -1,
name: "",
nickname: "",
profileImg: "",
});
const [isLoading, setIsLoading] = useState<boolean>(true);
const { useAuthStorePersist } = store;
const { accessToken, accessTokenExpireTime } = useAuthStorePersist(state => ({
accessToken: state.accessToken,
accessTokenExpireTime: state.accessTokenExpireTime
}));

const { accessToken, accessTokenExpireTime } = useAuthStorePersist(
(state) => ({
accessToken: state.accessToken,
accessTokenExpireTime: state.accessTokenExpireTime,
})
);

const authenticate = async () => {
if (accessToken!==null && accessTokenExpireTime!==null){
if (accessToken !== null && accessTokenExpireTime !== null) {
try {
const response = await axiosSemiSecureAPI.get('/api/members');
if (response.data.isSuccess){
setIsAuthenticated(true);
setMemberInfo(response.data?.result);
}else{

handleApiError(response.data?.code);
setIsAuthenticated(false);
setMemberInfo({ id: -1, name: '', nickname: '', profileImg: ''});
}
} catch (error) {
if(axios.isAxiosError(error)){
handleApiError(error);

const response = await axiosSemiSecureAPI.get("/api/members");
if (response.data.isSuccess) {
setIsAuthenticated(true);
setMemberInfo(response.data?.result);
} else {
handleApiError(response.data?.code);
setIsAuthenticated(false);
setMemberInfo({ id: -1, name: "", nickname: "", profileImg: "" });
}
else{
} catch (error) {
if (axios.isAxiosError(error)) {
handleApiError(error);
} else {
handleApiError(error);
setIsAuthenticated(false);
setMemberInfo({ id: -1, name: '', nickname: '', profileImg: ''});
setMemberInfo({ id: -1, name: "", nickname: "", profileImg: "" });
}


} finally {
} finally {
setIsLoading(false);
}
} else {
setIsAuthenticated(false);
setMemberInfo({ id: -1, name: "", nickname: "", profileImg: "" });
}
}
setIsLoading(false)


}
setIsLoading(false);
};

useEffect(() => {
authenticate();

}, []);

return { isAuthenticated, memberInfo, isLoading };
};

export default useCheckAuth;
export default useCheckAuth;

0 comments on commit 6b5f498

Please sign in to comment.