Skip to content

Commit

Permalink
Merge pull request #80 from Beside-Potenday/seungbeom
Browse files Browse the repository at this point in the history
feat: 디버깅 코드 추가
  • Loading branch information
seung365 authored Aug 7, 2024
2 parents 4feba39 + 3814787 commit 6cbba90
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 30 deletions.
42 changes: 13 additions & 29 deletions src/Provider/Auth.tsx
Original file line number Diff line number Diff line change
@@ -1,51 +1,35 @@
import { AuthContextType, AuthInfo } from '@/types';
import { createContext, useContext, useState, ReactNode, useEffect, useCallback } from 'react';
import { createContext, useContext, useState, ReactNode, useEffect } from 'react';

export const AuthContext = createContext<AuthContextType | undefined>(undefined);

export const AuthProvider = ({ children }: { children: ReactNode }) => {
const [authInfo, setAuthInfo] = useState<AuthInfo | undefined>(undefined);

const updateAuthInfo = useCallback((auth: AuthInfo) => {
setAuthInfo((prevAuth) => {
if (JSON.stringify(prevAuth) !== JSON.stringify(auth)) {
return auth;
}
return prevAuth;
});
}, []);
const updateAuthInfo = (auth: AuthInfo) => {
if (auth) {
setAuthInfo(auth);
}
};

const handleAuthInfo = useCallback(() => {
const handleAuthInfo = () => {
const authToken = sessionStorage.getItem('accessToken');
if (authToken) {
const email = sessionStorage.getItem('email') || '';
const name = sessionStorage.getItem('name') || '';
const picture = sessionStorage.getItem('picture') || '';
updateAuthInfo({
setAuthInfo({
accessToken: authToken,
email,
name,
picture,
email: email,
name: name,
picture: picture,
});
} else {
setAuthInfo(undefined);
}
}, [updateAuthInfo]);
};

useEffect(() => {
handleAuthInfo();

const handleStorageChange = (event: StorageEvent) => {
if (event.key === 'accessToken') {
handleAuthInfo();
}
};

window.addEventListener('storage', handleStorageChange);
return () => {
window.removeEventListener('storage', handleStorageChange);
};
}, [handleAuthInfo]);
});

return (
<AuthContext.Provider value={{ authInfo, updateAuthInfo }}>{children}</AuthContext.Provider>
Expand Down
4 changes: 3 additions & 1 deletion src/routes/components/PrivateRoute.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,12 @@ import { Navigate, Outlet } from 'react-router-dom';
import { getDynamicPath } from '../path';

export const PrivateRoute = () => {
const authInfo = useAuth();
const { authInfo } = useAuth();

if (!authInfo) {
return <Navigate to={getDynamicPath.login()} />;
} else {
console.log(authInfo.accessToken);
}

return <Outlet />;
Expand Down

0 comments on commit 6cbba90

Please sign in to comment.