Skip to content

Commit

Permalink
Merge pull request #79 from HGU-WALAB/#78/api-work-2
Browse files Browse the repository at this point in the history
bug: fix relogin bug
  • Loading branch information
healim01 authored Nov 27, 2023
2 parents 477296b + e886117 commit 7ec1ad7
Show file tree
Hide file tree
Showing 3 changed files with 7 additions and 21 deletions.
18 changes: 5 additions & 13 deletions src/auth/context/jwt/auth-provider.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,6 @@ import { useEffect, useReducer, useCallback, useMemo } from 'react';
// utils
import axiosInstance, { endpoints } from 'src/utils/axios';
//
import { useSetRecoilState } from 'recoil';
import { IsLoginState } from 'src/utils/atom';
import axios from 'axios';
import { BASE_URL } from 'src/config-global';
import { AuthContext } from './auth-context';
Expand Down Expand Up @@ -88,12 +86,11 @@ export function AuthProvider({ children }: Props) {

const initialize = useCallback(async () => {
try {
const accessToken = sessionStorage.getItem(STORAGE_KEY);

const accessToken = localStorage.getItem(STORAGE_KEY);
if (accessToken && isValidToken(accessToken)) {
setSession(accessToken);

const res = await axiosInstance.get(endpoints.auth.me);
const res = await axiosInstance.get(endpoints.auth.info);

const { user } = res.data;

Expand Down Expand Up @@ -137,15 +134,13 @@ export function AuthProvider({ children }: Props) {
email,
// password,
};
console.log('login');
console.log(data);

const res = await axios.post(`${BASE_URL}${endpoints.auth.login}`, data);
console.log(res);

const accessToken = res.data.token;
console.log(accessToken);

setSession(accessToken);
const info = await axiosInstance.get(endpoints.auth.info);

const user = { name: info.data.name, email: info.data.email, hanRole: info.data.hanRole };

dispatch({
Expand Down Expand Up @@ -190,10 +185,7 @@ export function AuthProvider({ children }: Props) {

// LOGOUT
const logout = useCallback(async () => {
// setIsLoginState(false);
setSession(null);
// setUserState(null);
console.log('logout');
dispatch({
type: Types.LOGOUT,
});
Expand Down
9 changes: 2 additions & 7 deletions src/auth/context/jwt/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -49,8 +49,7 @@ export const tokenExpired = (exp: number) => {

expiredTimer = setTimeout(() => {
alert('Token expired');

sessionStorage.removeItem('accessToken');
localStorage.removeItem('accessToken');

window.location.href = paths.auth.jwt.login;
}, timeLeft);
Expand All @@ -68,12 +67,8 @@ export const setSession = (accessToken: string | null) => {
const { exp } = jwtDecode(accessToken); // ~3 days by minimals server
tokenExpired(exp);
} else {
console.log('set session null remove');
localStorage.removeItem('accessToken');
localStorage.removeItem('userId');
localStorage.removeItem('deptId');
localStorage.removeItem('IsLoginState');

console.log('setSession');

delete axios.defaults.headers.common.Authorization;
}
Expand Down
1 change: 0 additions & 1 deletion src/utils/axios.ts
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,6 @@ export const endpoints = {
auth: {
me: '/api/auth/me',
login: '/hanSpace/login',
// login: '/api/auth/login',
register: '/api/auth/register',
info: '/hanSpace/info',
},
Expand Down

0 comments on commit 7ec1ad7

Please sign in to comment.