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

[ Common ] 엑세스 토큰 관리 #338

Merged
merged 11 commits into from
Jun 1, 2024
9 changes: 1 addition & 8 deletions src/CreateBook/utils/api.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,19 +18,12 @@ interface PostBookData {
backgroundColor: string;
}

const getAuthorizationToken = (): string | null => {
return localStorage.getItem('token');
};

const postBook = async (data: PostBookData): Promise<{ bookUuid: string }> => {
const token = getAuthorizationToken();

const response: AxiosResponse<ApiResponse> = await api.post(
const response: AxiosResponse<ApiResponse> = await api().post(
'/api/books',
data,
{
headers: {
Authorization: `Bearer ${token}`,
'Content-Type': 'application/json',
},
},
Expand Down
2 changes: 1 addition & 1 deletion src/Detail/api/getBookDetail.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { api } from '../../libs/api';

export async function getBookDetail(bookUuid: string) {
const data = await api.get(`/api/books/detail/${bookUuid}`, {
const data = await api().get(`/api/books/detail/${bookUuid}`, {
headers: {
'Content-Type': 'application/json',
},
Expand Down
4 changes: 1 addition & 3 deletions src/Detail/api/getBookDetailLogin.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,9 @@
import { api } from '../../libs/api';

export async function getBookDetailLogin(bookUuid: string) {
const token = localStorage.getItem('token');
const data = await api.get(`/api/books/favorite/${bookUuid}`, {
const data = await api().get(`/api/books/favorite/${bookUuid}`, {
headers: {
'Content-Type': 'application/json',
Authorization: `Bearer ${token}`,
},
});

Expand Down
9 changes: 1 addition & 8 deletions src/Detail/components/BookInfoBox/index.tsx
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@
import { useEffect, useState } from 'react';

import {
IcCrown,
IcDate,
Expand Down Expand Up @@ -33,8 +31,7 @@ function BookInfoBox({
bookId,
bookUuid,
}: BookInfoBoxProps) {
const token = localStorage.getItem('token');
const [isLogin, setIsLogin] = useState(false);
const isLogin = sessionStorage.getItem('token');

const postFavoriteMutation = usePostFavorite('lecueBookDetail', bookUuid);
const deleteFavoriteMutation = useDeleteFavorite('lecueBookDetail', bookUuid);
Expand All @@ -43,10 +40,6 @@ function BookInfoBox({
isFavorite ? deleteFavoriteMutation(bookId) : postFavoriteMutation(bookId);
};

useEffect(() => {
token ? setIsLogin(true) : setIsLogin(false);
}, []);

return (
<S.BookInfoBoxWrapper backgroundColor={bookBackgroundColor}>
<S.ProfileImageWrapper>
Expand Down
6 changes: 3 additions & 3 deletions src/Detail/components/LecueNoteListContainer/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@ import {
BtnFloatingWriteOrange,
} from '../../../assets';
import CommonModal from '../../../components/common/Modal/CommonModal';
import useAuth from '../../../libs/hooks/useAuth';
import useScrollPosition from '../../../utils/savedScrollPosition';
// hooks
import usePostSticker from '../../hooks/usePostSticker';
Expand Down Expand Up @@ -53,7 +52,8 @@ function LecueNoteListContainer(props: LecueNoteListContainerProps) {
const { savedScrollPosition } = useScrollPosition();
const { stickerState, setStickerState, handleDrag } =
useStickerState(savedScrollPosition);
const isLoggedIn = useAuth();

const isLogin = sessionStorage.getItem('token');

//state
const [fullHeight, setFullHeight] = useState<number | null>(null);
Expand Down Expand Up @@ -97,7 +97,7 @@ function LecueNoteListContainer(props: LecueNoteListContainerProps) {
});

const handleClickIconButton = (isSticker: boolean) => {
if (isLoggedIn) {
if (isLogin) {
sessionStorage.setItem('scrollPosition', window.scrollY.toString());
const path = isSticker ? '/sticker-pack' : '/create-note';
navigate(path, { state: { bookId } });
Expand Down
4 changes: 2 additions & 2 deletions src/Detail/page/DetailPage/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -14,10 +14,10 @@ import * as S from './DetailPage.style';
function DetailPage() {
const [isEditable, setIsEditable] = useState(true);

const token = window.localStorage.getItem('token');
const isLogin = sessionStorage.getItem('token');

const { bookUuid } = useParams() as { bookUuid: string };
const { bookDetail, isLoading } = token
const { bookDetail, isLoading } = isLogin
? useGetBookDetailLogin(bookUuid)
: useGetBookDetail(bookUuid);
const postMutation = usePostStickerState(bookUuid);
Expand Down
5 changes: 2 additions & 3 deletions src/EditNickname/api/patchNickname.ts
Original file line number Diff line number Diff line change
@@ -1,13 +1,12 @@
import { api } from '../../libs/api';

export const patchNickname = async (token: string, nickname: string) => {
const response = await api.patch(
export const patchNickname = async (nickname: string) => {
const response = await api().patch(
'/api/nickname',
{ nickname: nickname },
{
headers: {
'Content-Type': 'application/json',
Authorization: `Bearer ${token}`,
},
},
);
Expand Down
7 changes: 2 additions & 5 deletions src/EditNickname/components/EditButton/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -5,24 +5,21 @@ import * as S from './EditButton.style';

function EditButton({
isActive,
token,
nickname,
handleSetIsValid,
handleSetIsActive,
}: EditButtonProps) {
const patchMutation = usePatchNickname({
handleSetIsValid,
handleSetIsActive,
token,
nickname,
});

const handelClickSubmitBtn = (token: string, nickname: string) => {
const handelClickSubmitBtn = (nickname: string) => {
const patchNickname = nickname.trim();

patchMutation.mutate({
nickname: patchNickname,
token: token,
});
};

Expand All @@ -32,7 +29,7 @@ function EditButton({
type="button"
variant="complete"
disabled={!isActive}
onClick={() => handelClickSubmitBtn(token, nickname)}
onClick={() => handelClickSubmitBtn(nickname)}
>
완료
</Button>
Expand Down
2 changes: 1 addition & 1 deletion src/EditNickname/components/NicknameInput/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ function NicknameInput({
handleSetNickname,
handleSetIsValid,
}: NicknameInputProps) {
const currentNickname: string = localStorage.getItem('nickname') || '';
const currentNickname: string = sessionStorage.getItem('nickname') || '';
const [wordCnt, setWordCnt] = useState(currentNickname.length);

const handleSetWordCnt = (wordCnt: number) => {
Expand Down
12 changes: 5 additions & 7 deletions src/EditNickname/hooks/usePatchNickname.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,14 +9,14 @@ import {
} from '../types/editNicknameTypes';

const usePatchNickname = (props: usePatchNicknameProps) => {
const { handleSetIsValid, handleSetIsActive, token, nickname } = props;
const { handleSetIsValid, handleSetIsActive, nickname } = props;

const navigate = useNavigate();
const queryClient = useQueryClient();

const mutation = useMutation({
mutationFn: async ({ token, nickname }: patchNicknameProps) => {
return await patchNickname(token, nickname);
mutationFn: async ({ nickname }: patchNicknameProps) => {
return await patchNickname(nickname);
},
onError: (err: AxiosError) => {
const code = err.response?.status;
Expand All @@ -35,10 +35,8 @@ const usePatchNickname = (props: usePatchNicknameProps) => {
queryClient.refetchQueries(['useGetMyNickName'], {
exact: true,
});

window.localStorage.setItem('token', token);
window.localStorage.setItem('nickname', nickname);
navigate('/');
sessionStorage.setItem('nickname', nickname);
navigate('/mypage');
},
});

Expand Down
6 changes: 1 addition & 5 deletions src/EditNickname/page/index.tsx
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
import React, { useEffect, useState } from 'react';
import { useLocation } from 'react-router-dom';

import Header from '../../components/common/Header';
import EditButton from '../components/EditButton';
Expand All @@ -10,12 +9,10 @@ import * as S from './EditNickname.style';
function EditNickname() {
const [isActive, setIsActive] = useState(false);
const [nickname, setNickname] = useState(
localStorage.getItem('nickname') || '',
sessionStorage.getItem('nickname') || '',
);
const [isValid, setIsValid] = useState<isValidState>('enter');

const { state } = useLocation();

const handleSetNickname = (nickname: string) => {
setNickname(nickname);
};
Expand Down Expand Up @@ -49,7 +46,6 @@ function EditNickname() {
<EditButton
isActive={isActive}
nickname={nickname}
token={state}
handleSetIsValid={handleSetIsValid}
isValid={isValid}
handleSetIsActive={handleSetIsActive}
Expand Down
2 changes: 0 additions & 2 deletions src/EditNickname/types/editNicknameTypes.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,6 @@ export interface NicknameInputProps {
}

export interface EditButtonProps {
token: string;
nickname: string;
isActive: boolean;
isValid: string;
Expand All @@ -30,7 +29,6 @@ export interface CheckNicknameProps {
}

export interface patchNicknameProps {
token: string;
nickname: string;
}

Expand Down
14 changes: 8 additions & 6 deletions src/Enter/page/index.tsx
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import React, { useState } from 'react';
import { useLocation, useNavigate } from 'react-router-dom';
import { useNavigate } from 'react-router-dom';

import { IcMypageArrowRight } from '../../assets';
import Header from '../../components/common/Header';
Expand All @@ -9,14 +9,16 @@ import * as S from './Enter.style';
function Enter() {
const [nickname, setNickname] = useState('');
const navigate = useNavigate();
const { state } = useLocation();
if (state) {

const isLogin = sessionStorage.getItem('token');

if (isLogin) {
const { myNickName } = useGetMyNickName();
if (nickname === '' || nickname !== myNickName) setNickname(myNickName);
}

const handleClickNickname = () => {
navigate('edit-nickname', { state: state });
navigate('edit-nickname');
};
const handleClickHistory = () => {
navigate('select-history');
Expand All @@ -29,15 +31,15 @@ function Enter() {
const isLogout = confirm('로그아웃하시겠습니까?');

if (isLogout) {
window.localStorage.clear();
sessionStorage.clear();
navigate('/', { state: { step: 1 } });
}
};

return (
<React.Fragment>
<Header headerTitle="마이페이지" />
{state ? (
{isLogin ? (
<S.MypageBodyWrapper>
<S.NicknameWrapper>
<S.NicknameText>{nickname}님, 안녕하세요</S.NicknameText>
Expand Down
4 changes: 1 addition & 3 deletions src/History/api/deleteMyBook.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,9 @@
import { api } from '../../libs/api';

export async function deleteMyBook(bookId: number) {
const token = localStorage.getItem('token');
const data = await api.delete(`/api/books/${bookId}`, {
const data = await api().delete(`/api/books/${bookId}`, {
headers: {
'Content-Type': 'application/json',
Authorization: `Bearer ${token}`,
},
});

Expand Down
4 changes: 1 addition & 3 deletions src/History/api/getMyBookList.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,9 @@
import { api } from '../../libs/api';

export async function getMyBookList() {
const token = localStorage.getItem('token');
const data = await api.get(`/api/mypage/book`, {
const data = await api().get(`/api/mypage/book`, {
headers: {
'Content-Type': 'application/json',
Authorization: `Bearer ${token}`,
},
});

Expand Down
4 changes: 1 addition & 3 deletions src/History/api/getMyFavorite.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,9 @@
import { api } from '../../libs/api';

export async function getMyFavorite() {
const token = localStorage.getItem('token');
const data = await api.get(`/api/mypage/favorite`, {
const data = await api().get(`/api/mypage/favorite`, {
headers: {
'Content-Type': 'application/json',
Authorization: `Bearer ${token}`,
},
});

Expand Down
4 changes: 1 addition & 3 deletions src/History/api/getMyNoteList.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,9 @@
import { api } from '../../libs/api';

export async function getMyNoteList() {
const token = localStorage.getItem('token');
const data = await api.get(`/api/mypage/note`, {
const data = await api().get(`/api/mypage/note`, {
headers: {
'Content-Type': 'application/json',
Authorization: `Bearer ${token}`,
},
});
return data.data.data;
Expand Down
2 changes: 1 addition & 1 deletion src/Home/api/getLecueBook.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { api } from '../../libs/api';

const getLecueBook = async () => {
const { data } = await api.get('/api/common/home');
const { data } = await api().get('/api/common/home');
return data.data;
};

Expand Down
8 changes: 4 additions & 4 deletions src/Home/components/HomeMainBanner/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -10,14 +10,14 @@ function NavigateLecueBook() {
const navigate = useNavigate();
const [modalOn, setModalOn] = useState(false);

const handleClickIcProfile = () => {
const token = localStorage.getItem('token');
const isLogin = sessionStorage.getItem('token');

navigate('/mypage', { state: token });
const handleClickIcProfile = () => {
navigate('/mypage');
};

const handleClickNavBtn = () => {
if (localStorage.getItem('token')) {
if (isLogin) {
navigate('/target');
} else {
setModalOn(true);
Expand Down
5 changes: 3 additions & 2 deletions src/Home/page/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,10 @@ import useGetLecueBook from '../hooks/useGetLecueBook';
import * as S from './Home.style';

function Home({ handleStep }: StepProps) {
const token = localStorage.getItem('token');
const { isLoading: isLoadingLecueBook } = useGetLecueBook();

const isLogin = sessionStorage.getItem('token');

useEffect(() => {
handleStep(1);
}, []);
Expand All @@ -21,7 +22,7 @@ function Home({ handleStep }: StepProps) {
<S.Wrapper>
<HomeMainBanner />

{token && <LecueBookList title="즐겨찾기한 레큐북" />}
{isLogin && <LecueBookList title="즐겨찾기한 레큐북" />}
<LecueBookList title="인기 레큐북 구경하기" />
</S.Wrapper>
);
Expand Down
2 changes: 1 addition & 1 deletion src/LecueNote/api/getPresignedUrl.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { api } from '../../libs/api';

const getPresignedUrl = async () => {
const { data } = await api.get('/api/images/note');
const { data } = await api().get('/api/images/note');
return data;
};

Expand Down