Skip to content

Commit

Permalink
[#66] 토큰 관리 함수 작성
Browse files Browse the repository at this point in the history
  • Loading branch information
dewy-i committed Aug 1, 2021
1 parent 5fe347b commit 522a19a
Show file tree
Hide file tree
Showing 4 changed files with 25 additions and 8 deletions.
14 changes: 13 additions & 1 deletion src/utils/api.js
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,7 @@ const axiosInstance = axios.create({

axiosInstance.interceptors.request.use(
config => {
const token = localStorage.getItem('token');
const token = getToken();
if (token) {
config.headers.Authorization = token;
}
Expand Down Expand Up @@ -96,3 +96,15 @@ export async function requestAPI(config) {
return null;
}
}

export function setToken(token) {
localStorage.setItem('token', token);
}

export function getToken() {
return localStorage.getItem('token');
}

export function removeToken() {
localStorage.removeItem('token');
}
3 changes: 2 additions & 1 deletion src/utils/socket.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import io from 'socket.io-client';
import { getToken } from './api';

let socket;

Expand All @@ -8,7 +9,7 @@ export function getSocket() {
path: '/websocket/entryPoint',
transports: ['websocket'],
query: {
token: window.localStorage.getItem('token')
token: getToken()
},
reconnectionAttempts: 10
});
Expand Down
4 changes: 3 additions & 1 deletion src/views/login/SignUp.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import UosInput from '@components/UosInput';
import useButtonStyles from '@utils/styles/Button';
import useFontStyles from '@utils/styles/Font';
import UosDialog from '@components/UosDialog';
import { requestAPI, API_SIGN_UP } from '@utils/api';
import { requestAPI, API_SIGN_UP, removeToken } from '@utils/api';
import { uosRed } from '@utils/styles/Colors';
import Loading from '@components/Loading';

Expand Down Expand Up @@ -88,6 +88,8 @@ export default function SignUpDialog({onClose, open}) {

setIsLoading(true);

removeToken();

const response = await requestAPI(API_SIGN_UP(), newUser);

if(response.status === StatusCodes.CREATED) {
Expand Down
12 changes: 7 additions & 5 deletions src/views/login/index.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ import Loading from '@components/Loading';
import UosInput from '@components/UosInput';
import FindIdDialog from '@views/login/FindId';
import FindPWDialog from '@views/login/FindPw';
import { API_LOGIN, API_GET_SEMESTER, requestAPI } from '@utils/api';
import { API_LOGIN, API_GET_SEMESTER, requestAPI, removeToken, setToken } from '@utils/api';
import { foregroundColor } from '@utils/styles/Colors';
import useLogoLayoutStyles from '@utils/styles/login/LogoLayout';
import useLoginLabelStyles from '@utils/styles/login/LoginLabel';
Expand Down Expand Up @@ -57,11 +57,13 @@ export default function Login() {
const callLogin = async () => {
setLoading(true);
setError(null);

const response = await requestAPI(API_LOGIN(), loginInfo);

removeToken();

const response = await requestAPI(API_LOGIN(loginInfo));

if(response.status === StatusCodes.OK) {
window.localStorage.setItem('token', response.data.token);
setToken(response.data.token);
window.localStorage.setItem('userID', response.data.userId);
setUserID(response.data.userId);
setSemester({year: '2021', term: 'A10'})
Expand All @@ -87,7 +89,7 @@ export default function Login() {
return;
}

window.localStorage.setItem('token', response.data.token);
setToken(response.data.token);
window.localStorage.setItem('userID', response.data.userId);
setUserID(response.data.userId);

Expand Down

0 comments on commit 522a19a

Please sign in to comment.