Skip to content

Commit

Permalink
Merge pull request #60 from Fashion-Cloud/feat/i59
Browse files Browse the repository at this point in the history
refact: apiV1Instance Auth설정 수정
  • Loading branch information
eric-hjh authored Nov 12, 2023
2 parents 9f55f56 + 2be8e06 commit 78fdd85
Show file tree
Hide file tree
Showing 16 changed files with 115 additions and 167 deletions.
30 changes: 25 additions & 5 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 2 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
"@testing-library/react": "^13.4.0",
"@testing-library/user-event": "^13.5.0",
"@types/jest": "^27.5.2",
"@types/js-cookie": "^3.0.6",
"@types/node": "^16.18.20",
"@types/react": "^18.0.29",
"@types/react-dom": "^18.0.11",
Expand All @@ -22,6 +23,7 @@
"eslint-config-next": "^13.5.4",
"http-proxy-middleware": "^2.0.6",
"install": "^0.13.0",
"js-cookie": "^3.0.5",
"lottie-react": "^2.4.0",
"lottie-web": "^5.10.2",
"next": "^13.5.4",
Expand Down
15 changes: 10 additions & 5 deletions src/api/api-instance.ts
Original file line number Diff line number Diff line change
@@ -1,12 +1,17 @@
import axios from 'axios';
import { token } from 'src/assets/data/token';
import Cookies from 'js-cookie';

const apiV1Instance = axios.create({
baseURL: process.env.NEXT_PUBLIC_API,
headers: {
'Content-Type': 'application/json',
Authorization: `Bearer ${token}`,
},
});
apiV1Instance.interceptors.request.use((config) => {
const accessToken = Cookies.get('accessToken');

if (accessToken) {
config.headers.Authorization = `Bearer ${accessToken}`;
}

return config;
});

export default apiV1Instance;
8 changes: 2 additions & 6 deletions src/api/hook/CheckAuthHook.ts
Original file line number Diff line number Diff line change
@@ -1,14 +1,10 @@
import Cookies from 'js-cookie';
import router from 'next/router';
import { useEffect } from 'react';

const useCheckAuth = () => {
useEffect(() => {
const getCookie = (name: string) => {
const value = document.cookie.match('(^|;) ?' + name + '=([^;]*)(;|$)');
return value ? value[2] : null;
};

const token = getCookie('token');
const token = Cookies.get('accessToken');

if (token) {
router.push('/');
Expand Down
7 changes: 2 additions & 5 deletions src/api/hook/UserHook.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import Cookies from 'js-cookie';
import router from 'next/router';
import { useMutation } from 'react-query';

Expand Down Expand Up @@ -32,13 +33,9 @@ export const useLogout = () => {
return useMutation({
mutationFn: () => UserService.logout(),
onSuccess: () => {
deleteCookie('token');
Cookies.remove('accessToken');
router.replace('login');
},
onError: () => alert('다시 로그아웃 해주세요.'),
});
};

function deleteCookie(name: string) {
document.cookie = name + '=; expires=Thu, 01 Jan 1970 00:00:00 UTC; path=/;';
}
3 changes: 0 additions & 3 deletions src/assets/data/token.ts

This file was deleted.

12 changes: 3 additions & 9 deletions src/components/AddFashion/AddFashion/AddImage.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,11 @@ import 'react-toastify/dist/ReactToastify.css';

import CloudUploadIcon from '@mui/icons-material/CloudUpload';
import { Box, Button, Typography } from '@mui/material';
import axios from 'axios';
import Image from 'next/image';
import React, { useState } from 'react';
import Resizer from 'react-image-file-resizer';
import { toast, ToastContainer } from 'react-toastify';
import { token } from 'src/assets/data/token';
import apiV1Instance from 'src/api/api-instance';

type ImageProps = {
getImageData: (data: string) => void;
Expand Down Expand Up @@ -67,13 +66,8 @@ export default function AddImage({ getImageData }: ImageProps) {
formData.append('image', imgURL);

try {
await axios
.post(`${process.env.NEXT_PUBLIC_API}/images`, formData, {
headers: {
'Content-Type': 'multipart/form-data',
Authorization: `Bearer ${token}`,
},
})
await apiV1Instance
.post(`${process.env.NEXT_PUBLIC_API}/images`, formData)
.then((response) => {
success();
console.log('sendImage URL response: ', response.data);
Expand Down
10 changes: 3 additions & 7 deletions src/components/AddFashion/AddFashion/AddLocation.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,8 @@ import '../../../../public/fonts/font.css';

import NearMeIcon from '@mui/icons-material/NearMe';
import { IconButton, Toolbar, Typography } from '@mui/material';
import axios from 'axios';
import { useEffect, useState } from 'react';
import apiV1Instance from 'src/api/api-instance';

import useGeoLocation from '../../../assets/hooks/useGeoLocation';
import { LocationType } from '../../../utils/types';
Expand All @@ -21,12 +21,8 @@ export default function AddLocation({ getLocationData }: LocationProps) {
const locationAPI = async () => {
if (locationData !== undefined) {
try {
await axios
.get(`/api/v1/address?latitude=${latitude}&longitude=${longitude}`, {
headers: {
Accept: 'application/json',
},
})
await apiV1Instance
.get(`/api/v1/address?latitude=${latitude}&longitude=${longitude}`)
.then((response) => {
console.log('response: ', response);
const data = response.data.data;
Expand Down
9 changes: 2 additions & 7 deletions src/components/Lookbook/LookbookLayout/AddLookbookButton.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -10,9 +10,8 @@ import {
Paper,
Typography,
} from '@mui/material';
import axios from 'axios';
import { useState } from 'react';
import { token } from 'src/assets/data/token';
import apiV1Instance from 'src/api/api-instance';

// import SearchIcon from '@mui/icons-material/Search';
import AddImage from '../../../components/AddFashion/AddFashion/AddImage';
Expand Down Expand Up @@ -54,14 +53,10 @@ export default function AddLookbookButton() {
}

try {
await axios
await apiV1Instance
.post(`${process.env.NEXT_PUBLIC_API}/lookbooks`, {
title: title,
image: postImage,
}, {
headers: {
Authorization: `Bearer ${token}`
},
})
.then((response) => {
console.log(response);
Expand Down
1 change: 0 additions & 1 deletion src/components/Lookbook/LookbookLayout/LookbookBox.tsx
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
import { Box, Card, CardMedia, Grid, Typography } from '@mui/material';
import Link from 'next/link';
// import axios from 'axios';
import { useEffect, useState } from 'react';
import { useFindAllBooks } from 'src/api/hook/LookbookHook';

Expand Down
29 changes: 7 additions & 22 deletions src/components/MainLayout/FashionListBox.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -16,20 +16,18 @@ import {
Toolbar,
Typography,
} from '@mui/material';
import axios from 'axios';
import Image from 'next/image';
import React, { useEffect, useRef, useState } from 'react';
import apiV1Instance from 'src/api/api-instance';
import useCheckAuth from 'src/api/hook/CheckAuthHook';
import { useLogout } from 'src/api/hook/UserHook';
import { token } from 'src/assets/data/token';

import logoUrl from '../../../public/title-logo.png';
import { WeatherPostType } from '../../utils/types';
import useRainfallTypeStore from '../../utils/zustand/weather/RainfallTypeStore';
import useSkyStatusStore from '../../utils/zustand/weather/SkyStatusStore';
import useWindChillSearchStore from '../../utils/zustand/weather/WindChillSearchStore';
import AddFashionButton from './FashionListBox/AddFashionButton';
import FashionModal from "./FashionListBox/FashionModal";
import FashionModal from './FashionListBox/FashionModal';
import PaginationBox from './FashionListBox/PaginationBox';
import SearchBox from './FashionListBox/SearchBox';

Expand Down Expand Up @@ -113,15 +111,9 @@ export default function FashionListBox() {
console.log('[Recoil] rainfallCode: ', rainfallType);
console.log('[Recoil] windChillSearch: ', windChillSearch);
try {
await axios
await apiV1Instance
.get(
`${process.env.NEXT_PUBLIC_API}/posts/weather?skyStatus=${skyStatus}&rainfallType=${rainfallType}&minWindChill=${windChillSearch[0]}&maxWindChill=${windChillSearch[1]}&page=${page}&size=8`,
{
headers: {
Accept: 'application/json',
Authorization: `Bearer ${token}`,
},
}
`${process.env.NEXT_PUBLIC_API}/posts/weather?skyStatus=${skyStatus}&rainfallType=${rainfallType}&minWindChill=${windChillSearch[0]}&maxWindChill=${windChillSearch[1]}&page=${page}&size=8`
)
.then((response) => {
const data = response.data;
Expand All @@ -138,13 +130,8 @@ export default function FashionListBox() {

const followTimelineAPI = async () => {
try {
await axios
.get(`/api/v1/posts/follow/timeline?page=${page}&size=8`, {
headers: {
Accept: 'application/json',
Authorization: `Bearer ${token}`,
},
})
await apiV1Instance
.get(`/api/v1/posts/follow/timeline?page=${page}&size=8`)
.then((response) => {
const data = response.data;
console.log('data.data: ', data.data);
Expand Down Expand Up @@ -230,8 +217,6 @@ export default function FashionListBox() {

useCheckAuth();

const { mutate: logout } = useLogout();

return (
<Box sx={{ height: '100vh', backgroundColor: '#F5F8FC' }}>
<Box height="75px" />
Expand Down Expand Up @@ -291,7 +276,7 @@ export default function FashionListBox() {

<Modal open={openDetail} onClose={handleCloseDetail} closeAfterTransition>
<Box sx={style}>
<FashionModal singleId={singleId} setOpenDetail={setOpenDetail}/>
<FashionModal singleId={singleId} setOpenDetail={setOpenDetail} />
</Box>
</Modal>

Expand Down
Loading

0 comments on commit 78fdd85

Please sign in to comment.