auth.js 1.25 KB
import React, { useEffect } from 'react';
import { auth } from '../_actions/user_actions';
import { useSelector, useDispatch } from "react-redux";

export default function (SpecificComponent, option, adminRoute = null) {
    function AuthenticationCheck(props) {

        let user = useSelector(state => state.user);
        const dispatch = useDispatch();

        useEffect(() => {
            // 현재 나의 상태를 알기 위해서 Auth request보냄
            dispatch(auth()).then(response => {
                // 로그인 상태가 아님
                if (!response.payload.isAuth) {
                    if (option) {
                        props.history.push('/login')
                    }
                    // 로그인 상태
                } else {
                  
                    if (adminRoute && !response.payload.isAdmin) {
                        props.history.push('/')
                    }
                    else {
                        if (option === false) {
                            props.history.push('/')
                        }
                    }
                }
            })

        }, [])

        return (
            <SpecificComponent {...props} user={user} />
        )
    }
    return AuthenticationCheck
}