목차
권한분기
- 사용자 서버 안에서 로그인한 유저 / 로그인 안한 유저로 권한을 구분
- 권한을 구분하는 방법으로 useEffect에서 accessToken이 없으면 "/login" 화면으로 페이지를 이동시킴
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
export default function QuizLoginSuccessPage() { | |
const router = useRouter(); | |
const { data } = useQuery(FETCH_USER_LOGGED_IN); | |
const { userInfo, setUserInfo, accessToken } = useContext(GlobalContext); | |
useEffect(() => { | |
if (userInfo.email) return; | |
if (!accessToken) { | |
router.push("/quiz/login"); | |
alert("로그인을 먼저 해주세요"); | |
} | |
setUserInfo({ | |
name: data?.fetchUserLoggedIn.name, | |
email: data?.fetchUserLoggedIn.email, | |
picture: data?.fetchUserLoggedIn.picture, | |
}); | |
}, []); | |
return ( | |
<> | |
<div>로그인에 성공하셨습니다!</div> | |
{data?.fetchUserLoggedIn.name}님 환영합니다~ | |
</> | |
); | |
} |
└ 권한분기를 하려면 로그인 권한을 부여하려는 페이지에 모두 위의 로직을 입력해야하는 번거로움이 있음
└ 따라서, 이 문제를 해결하기 위해서 HOC를 사용함 !!
'React > 2022-上' 카테고리의 다른 글
React HOC vs HOF (0) | 2022.04.18 |
---|---|
JS Closure (0) | 2022.04.18 |
Cookie, Session, Local Storage (0) | 2022.04.18 |
Regular Expression (정규표현식) (0) | 2022.04.17 |
React 상태관리 - Context api, redux, mobx, swr (0) | 2022.04.17 |