auth.js
1.25 KB
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
32
33
34
35
36
37
38
39
40
41
42
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
}