Skip to content

Commit

Permalink
게스트 로그인 로직을 작성한다. (SWYP-team-2th#159)
Browse files Browse the repository at this point in the history
  • Loading branch information
YOOJS1205 committed Mar 2, 2025
1 parent 9a1a09a commit 31b32a8
Show file tree
Hide file tree
Showing 5 changed files with 73 additions and 6 deletions.
10 changes: 7 additions & 3 deletions src/App.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,11 @@ import { getAccessToken } from '@/components/login/Auth/token';
function App() {
const navigate = useNavigate();
const accessToken = getAccessToken();
const { data: myInfo, isLoading: isMyInfoLoading } = useGetMyInfo({
const {
data: myInfo,
isLoading: isMyInfoLoading,
isSuccess: isMyInfoSuccess,
} = useGetMyInfo({
enabled: !!accessToken,
});

Expand All @@ -18,10 +22,10 @@ function App() {
return;
}

if (myInfo) {
if (myInfo && isMyInfoSuccess) {
navigate(`/user/${myInfo.id}`, { replace: true });
}
}, [navigate, myInfo]);
}, [navigate, myInfo, isMyInfoSuccess]);

return (
<div className="w-full max-w-[480px] mx-auto my-0 h-[100dvh] flex items-center justify-center">
Expand Down
22 changes: 22 additions & 0 deletions src/api/config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -79,6 +79,28 @@ axiosInstance.interceptors.response.use(
}
}

if (
error.response?.status === 400 &&
error.response.data.errorCode === 'INVALID_GUEST_HEADER'
) {
try {
const response = await axiosInstance.post<{ guestToken: string }>(
'/auth/guest/token',
null,
);

if (response.data.guestToken) {
localStorage.setItem('guestToken', response.data.guestToken);
error.config.headers['Guest-Token'] = response.data.guestToken;
return axios(error.config);
}
} catch (err) {
console.error('400시 게스트 토큰 발급 중 오류:', err);
removeAccessToken();
window.location.href = '/onboarding';
}
}

return Promise.reject(error);
},
);
Expand Down
26 changes: 26 additions & 0 deletions src/api/usePostGuestVote.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
import { useMutation, UseMutationOptions } from '@tanstack/react-query';
import { request } from '@/api/config';

export default function usePostGuestVote(
postId: number,
options?: UseMutationOptions<unknown, Error, number>,
) {
return useMutation<unknown, Error, number>({
mutationFn: (imageId: number) => {
return request({
method: 'POST',
url: `/posts/${postId}/votes/guest`,
headers: {
'Guest-Token': localStorage.getItem('guestToken') ?? '',
},
data: {
imageId,
},
});
},
onError: (err) => {
console.error('투표 에러:', err);
},
...options,
});
}
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ export default function MyVoteList() {
<div className="overflow-scroll grid grid-cols-3 gap-2">
{participatedVoteList.map((vote) => (
<Link
to={vote.shareUrl}
to={`/votes/${vote.shareUrl}`}
key={vote.id}
className="relative aspect-[71/106] rounded-xl overflow-hidden"
>
Expand Down
19 changes: 17 additions & 2 deletions src/components/vote-detail/Vote/VoteCard/VoteCardList.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,16 @@ import { useQueryClient } from '@tanstack/react-query';
import { useParams } from 'react-router-dom';
import VoteCardItem from './VoteCardItem';
import ImageDetailModal from '../../ImageDetailModal';
import usePostGuestVote from '@/api/usePostGuestVote';
import useVote from '@/api/useVote';
import { useDialog } from '@/components/common/Dialog/hooks';
import Loading from '@/components/common/Loading';
import { getAccessToken } from '@/components/login/Auth/token';
import useVoteDetail from '@/components/vote-detail/Vote/VoteCard/hooks';

export default function VoteCardList() {
const { shareUrl } = useParams<{ shareUrl: string }>();
const { openDialog } = useDialog();
const { voteDetail } = useVoteDetail(shareUrl ?? '');
const queryClient = useQueryClient();
const { mutate: voteMutate, isPending } = useVote(voteDetail.id, {
Expand All @@ -21,7 +24,13 @@ export default function VoteCardList() {
});
},
});
const { openDialog } = useDialog();
const { mutate: postGuestVote } = usePostGuestVote(voteDetail.id, {
onSuccess: () => {
queryClient.invalidateQueries({
queryKey: ['voteDetail', shareUrl],
});
},
});

const handleClickVoteCardItem = (id: number) => {
openDialog(
Expand All @@ -32,7 +41,13 @@ export default function VoteCardList() {
const handleVote =
(id: number) => (e: React.MouseEvent<HTMLButtonElement>) => {
e.stopPropagation();
voteMutate(id);
const accessToken = getAccessToken();

if (accessToken) {
voteMutate(id);
} else {
postGuestVote(id);
}
};

return (
Expand Down

0 comments on commit 31b32a8

Please sign in to comment.