Skip to content

Commit

Permalink
Fix : fix instance interceptor
Browse files Browse the repository at this point in the history
  • Loading branch information
sparrowscout committed Dec 17, 2023
1 parent ce09f17 commit 555365a
Show file tree
Hide file tree
Showing 3 changed files with 17 additions and 20 deletions.
15 changes: 7 additions & 8 deletions src/components/login/KakaoOauth.tsx
Original file line number Diff line number Diff line change
@@ -1,12 +1,13 @@
import { useLocation, useNavigate } from 'react-router';
import instance from 'shared/axios';
import { setItem } from 'utils/localStorage';
import { getItem, setItem } from 'utils/localStorage';
import { ILoginResponse } from './NaverOauth';
import { useEffect } from 'react';
import { useEffect, useState } from 'react';

export default function KakaoOauth() {
const location = useLocation();
const navigate = useNavigate();

const urlParams = new URLSearchParams(location.search);
const codeValue = urlParams.get('code');

Expand All @@ -15,12 +16,10 @@ export default function KakaoOauth() {
`/api/user/kakao?code=${codeValue}`
);

if (response.data) {
setItem('token', response.data.Authorization);
setItem('id', response.data.id);
setItem('nickname', response.data.nickname);
navigate('/');
}
setItem('token', response.Authorization);
setItem('id', response.id);
setItem('nickname', response.nickname);
navigate('/');
};

Login();
Expand Down
14 changes: 7 additions & 7 deletions src/components/login/NaverOauth.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,9 @@ interface TokenSet {
nickname: string;
}
export interface ILoginResponse {
data: TokenSet;
Authorization: string;
id: string;
nickname: string;
}

export default function NaverOauth() {
Expand All @@ -22,12 +24,10 @@ export default function NaverOauth() {
`/api/user/naver?code=${codeValue}`
);

if (response.data) {
setItem('token', response.data.Authorization);
setItem('id', response.data.id);
setItem('nickname', response.data.nickname);
navigate('/');
}
setItem('token', response.Authorization);
setItem('id', response.id);
setItem('nickname', response.nickname);
navigate('/');
};

Login();
Expand Down
8 changes: 3 additions & 5 deletions src/shared/axios.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
import axios, { InternalAxiosRequestConfig } from 'axios';

const token = localStorage.getItem('token');
import { getItem } from 'utils/localStorage';

const baseURL = process.env.REACT_APP_BASE_URL;

Expand All @@ -9,13 +8,12 @@ const instance = axios.create({ baseURL });
const setToken = (
config: InternalAxiosRequestConfig
): InternalAxiosRequestConfig => {
const token = getItem('token');
config.headers['Authorization'] = `${token}`;
return config;
};

if (token) {
instance.interceptors.request.use(setToken);
}
instance.interceptors.request.use(setToken);

instance.interceptors.response.use(
(response) => {
Expand Down

0 comments on commit 555365a

Please sign in to comment.