App.js
1.48 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
43
44
45
46
47
48
49
50
51
52
53
54
55
56
import "./App.css";
import {useEffect } from "react";
import firebase from "./components/firebase/firebase";
import { Switch, Route, useHistory } from "react-router-dom";
import SignupPage from "./components/signuppage/SignupPage";
import LoginPage from "./components/loginpage/LoginPage";
import MainPage from "./components/mainpage/MainPage";
import { useDispatch, useSelector } from "react-redux";
import { setUser, clearUser } from "./redux/actions/user_action";
function App(props) {
let history = useHistory();
let dispatch = useDispatch();
const isLoading = useSelector((state) => state.user.isLoading);
useEffect(() => {
firebase.auth().onAuthStateChanged(function (user) {
// user 있으면 로그인 된 / user 없으면 안 된 상태
if (user) {
history.push("/");
dispatch(setUser(user));
} else {
history.push("/login");
dispatch(clearUser());
}
});
}, [dispatch, history]);
if (isLoading) {
return (
<div
style={{
display: "flex",
alignItems: "center",
justifyContent: "center",
height: "100vh ",
}}
>
<span>LOADING...</span>
</div>
);
} else {
return (
<Switch>
<Route exact path="/" component={MainPage}></Route>
<Route exact path="/login" component={LoginPage}></Route>
<Route exact path="/signup" component={SignupPage}></Route>
</Switch>
);
}
}
export default App;