Skip to content

Commit

Permalink
Merge pull request #63 from Beside-Potenday/seungbeom
Browse files Browse the repository at this point in the history
fix: 렌더링시 로그인 상태 유지
  • Loading branch information
seung365 authored Aug 7, 2024
2 parents 6254e0f + 618d1e1 commit d8119fe
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 3 deletions.
22 changes: 20 additions & 2 deletions src/Provider/Auth.tsx
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { AuthContextType, AuthInfo } from '@/types';
import { createContext, useContext, useState, ReactNode } from 'react';
import { createContext, useContext, useState, ReactNode, useEffect } from 'react';

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

Expand All @@ -9,10 +9,28 @@ export const AuthProvider = ({ children }: { children: ReactNode }) => {
const updateAuthInfo = (auth: AuthInfo) => {
if (auth) {
setAuthInfo(auth);
sessionStorage.setItem('authToken', auth.accessToken);
}
};

const handleAuthInfo = () => {
const authToken = sessionStorage.getItem('authToken');
if (authToken) {
const email = sessionStorage.getItem('email') || '';
const name = sessionStorage.getItem('name') || '';
const picture = sessionStorage.getItem('picture') || '';
setAuthInfo({
accessToken: authToken,
email: email,
name: name,
picture: picture,
});
}
};

useEffect(() => {
handleAuthInfo();
});

return (
<AuthContext.Provider value={{ authInfo, updateAuthInfo }}>{children}</AuthContext.Provider>
);
Expand Down
5 changes: 4 additions & 1 deletion src/pages/Login/AuthPage.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,10 @@ export const AuthPage = () => {
name: data.name,
email: data.email,
});

sessionStorage.setItem('authToken', data.accessToken);
sessionStorage.setItem('name', data.name);
sessionStorage.setItem('picture', data.picture);
sessionStorage.setItem('email', data.email);
navigate(RouterPath.home);
} else if (error) {
console.error('Login failed:', error);
Expand Down

0 comments on commit d8119fe

Please sign in to comment.