-
Notifications
You must be signed in to change notification settings - Fork 0
/
globalApp.js
94 lines (88 loc) · 2.67 KB
/
globalApp.js
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
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
import { ChakraProvider } from "@chakra-ui/react";
import axios from "axios";
import { useEffect, useState } from "react";
import { useDispatch, useSelector } from "react-redux";
import {
setAuth,
setClassmates,
setCollege,
setUser,
} from "./redux/actions/actions";
import Home from "./pages";
import Loader from "./components/loader";
export function extractCookies(cookieStr) {
return cookieStr
.match(/(^|(?<=, ))[^=;,]+=[^;]+/g)
.map((cookie) => cookie.split("=").map((v) => v.trim()))
.filter((v) => v[0].length && v[1].length)
.reduce((builder, cur) => {
builder[cur[0]] = cur[1];
return builder;
}, {});
}
const GlobalApp = ({ Component, pageProps }) => {
let globalState = useSelector((state) => state);
let [authDone, setAuthDone] = useState(false);
let dispatch = useDispatch();
let authUser = () => {
if (document.cookie !== null && document.cookie !== "") {
let token = extractCookies(document.cookie).jwt;
if (token !== undefined || token !== null || token !== "") {
axios
.post("http://localhost:8080/api/v1/auth/verifyToken", {
token: token,
})
.then((res) => {
try {
if (res.data.res) {
dispatch(setAuth(true));
dispatch(setUser(res.data.userdata));
dispatch(setCollege(res.data.college));
console.log("token verified");
axios
.post(
"http://localhost:8080/api/v1/classmates/getClassmates",
res.data.userdata
)
.then((classRes) => {
dispatch(setClassmates(classRes.data.classmates));
})
.catch((err) => console.log(err));
setAuthDone(true);
} else {
dispatch(setAuth(false));
dispatch(setUser(null));
setAuthDone(true);
}
} catch (err) {
dispatch(setAuth(false));
dispatch(setUser(null));
setAuthDone(true);
console.log(err);
}
})
.catch((err) => console.log(err));
} else {
dispatch(setAuth(false));
dispatch(setUser(null));
setAuthDone(true);
}
} else {
dispatch(setAuth(false));
dispatch(setUser(null));
setAuthDone(true);
}
};
useEffect(() => {
authUser();
}, []);
if (authDone === false) {
return <Loader />;
}
return (
<ChakraProvider>
{!globalState.auth ? <Home /> : <Component {...pageProps} />}
</ChakraProvider>
);
};
export default GlobalApp;