티스토리 뷰

반응형

로그인 기능을 갖춘 페이지 구현시, URL 에 경로를 입력해서 들어오는 비정상적 접근을 차단해야하는 경우 사용할 수 있음

 

 return (
        <div className="root" id="root">
            <div className="wrap">
          
                <section>
                    <Route exact path="/" component={SignIn} />
                    <Switch>
                   		 <RouteAuthChek path="/temp" component={Temp} auth={AUTH} url={"temp"} />
                   </Switch>
                </section>
      
            </div>
        </div>
    );
}
const RouteAuthChek = ({ auth, url, component: Component, ...rest }) => {
    //redux 불러오기

    const [show, setShow] = useState(false);

    return (
        <Route
            {...rest}
            render={(props) => {
                // DB에서 권한 체크
                axios
                    .post(`${db}/path...`, { auth: auth, url: url })
                    .then((response) => {
                        setShow(response.data);
                    })
                    .catch((err) => {
                        console.log(err);
                    })
                    .finally(() => {});

                if (show && Component) {
                    return <Component {...props} />;
                } else {
                     return <ErrorPage {...props} />;
                }

                return null;
            }}
        />
    );
};

Router을 커스텀해서 사용 가능

 

반응형

'Web' 카테고리의 다른 글

[React] timer (OTP 창 만들기)  (0) 2022.05.03
[React] 엔터키 입력으로 로그인 하기 onKeyPress  (0) 2022.05.03
[React]React에서 WAS 와 WebServer, Web Architecture  (0) 2021.09.13
Context  (0) 2021.07.29
React 설치  (1) 2021.07.29
댓글
반응형
공지사항
최근에 올라온 글
최근에 달린 댓글
Total
Today
Yesterday
링크
TAG more
«   2024/07   »
1 2 3 4 5 6
7 8 9 10 11 12 13
14 15 16 17 18 19 20
21 22 23 24 25 26 27
28 29 30 31
글 보관함