Skip to content

Commit

Permalink
Merge pull request #39 from GihoKo/dev
Browse files Browse the repository at this point in the history
Dev
  • Loading branch information
GihoKo authored Aug 14, 2024
2 parents 4a2ca93 + 855282d commit 1221cc6
Show file tree
Hide file tree
Showing 4 changed files with 16 additions and 13 deletions.
9 changes: 6 additions & 3 deletions src/App.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -17,19 +17,22 @@ import { useEffect } from 'react';
import { renewTokens } from './apis/services/auth';

// token
import AccessTokenManager from './authentication/AccessTokenManager';
import accessTokenManager from './authentication/accessTokenManager';

export default function App() {
// 모달
const { isOpen, component: ModalComponent } = useModalStore();

// 새로고침 시 엑세스 토큰 재발급
useEffect(() => {
if (!AccessTokenManager.hasAccessToken()) {
const isincludeMain = window.location.href.includes('main');
const isHasAccessToken = accessTokenManager.hasAccessToken();

if (!isHasAccessToken && isincludeMain) {
renewTokens()
.then((newAccessToken) => {
if (newAccessToken) {
AccessTokenManager.setAccessToken(newAccessToken);
accessTokenManager.setAccessToken(newAccessToken);
}
})
.catch((error) => {
Expand Down
10 changes: 5 additions & 5 deletions src/apis/instances/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import axios, { AxiosError } from 'axios';
import { handleAxiosError } from '../../utils/handleAxiosError';
import { handleUnexpectedError } from '../../utils/handleUnexpectedError';
import { renewTokens } from '../services/auth';
import AccessTokenManager from '@/authentication/accessTokenManager';
import accessTokenManager from '@/authentication/accessTokenManager';

const baseURL = process.env.API_URL;

Expand All @@ -28,10 +28,10 @@ export const axiosInstanceWithToken = axios.create({
// request interceptor의 경우 token을 넣을 때 자주 사용한다.
axiosInstanceWithToken.interceptors.request.use(
(AxiosRequestConfig) => {
const accessToken = AccessTokenManager.getAccessToken();
const accessToken = accessTokenManager.getAccessToken();

// 만약 토큰이 존재하는 경우 헤더에 넣어준다.
if (AccessTokenManager.hasAccessToken()) {
if (accessTokenManager.hasAccessToken()) {
AxiosRequestConfig.headers['authorization'] = `Bearer ${accessToken}`;
}
return AxiosRequestConfig;
Expand Down Expand Up @@ -59,9 +59,9 @@ axiosInstanceWithToken.interceptors.response.use(
// accessToken 갱신
const newAccessToken = await renewTokens();

AccessTokenManager.setAccessToken(newAccessToken);
accessTokenManager.setAccessToken(newAccessToken);

const accessToken = AccessTokenManager.getAccessToken();
const accessToken = accessTokenManager.getAccessToken();

if (originalRequest) {
// 새로운 accessToken으로 요청 재시도
Expand Down
4 changes: 2 additions & 2 deletions src/authentication/accessTokenManager.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
const AccessTokenManager = (function () {
const accessTokenManager = (function () {
let accessToken: string | null = null;

return {
Expand All @@ -20,4 +20,4 @@ const AccessTokenManager = (function () {
};
})();

export default AccessTokenManager;
export default accessTokenManager;
6 changes: 3 additions & 3 deletions src/pages/Signin/_components/GoogleLoginButton.hook.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ import { useGoogleOAuthTokenStore } from '../../../store/useAuthStore';
import { useUserStore } from '../../../store/useUserStore';

// token
import AccessTokenManager from '@/authentication/AccessTokenManager';
import accessTokenManager from '@/authentication/accessTokenManager';

export default function useGoogleLoginButton() {
const navigate = useNavigate();
Expand All @@ -20,11 +20,11 @@ export default function useGoogleLoginButton() {
try {
await axiosInstanceWithToken.post('/auth/google/callback', { code }).then((response) => {
setGoogleOAuthToken(response.data.googleAccessToken);
AccessTokenManager.setAccessToken(response.data.applicationAccessToken);
accessTokenManager.setAccessToken(response.data.applicationAccessToken);
setUser(response.data.user);
});

if (AccessTokenManager.hasAccessToken()) {
if (accessTokenManager.hasAccessToken()) {
navigate('/main');
}
} catch (error) {
Expand Down

0 comments on commit 1221cc6

Please sign in to comment.