-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathdashboard.jsx
77 lines (71 loc) · 2.17 KB
/
dashboard.jsx
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
import DashboardBody from "../components/DashboardBody/DashboardBody";
import DashboardNav from "../components/DashboardNav/DashboardNav";
import { signIn, useSession, getSession } from "next-auth/react";
import { useEffect, useState } from "react";
import axios from "axios";
import Layout from "../components/Layout";
import FooterMain from "../components/FooterMain/FooterMain";
export default function Dashboard(props) {
const { data: session, status } = useSession();
const [loading, setLoading] = useState(true);
useEffect(() => {
const securePage = () => {
if (status === "unauthenticated") {
signIn();
} else {
setLoading(false);
}
};
securePage();
});
if (loading) {
return (
<h2 style={{ marginTop: 100, textAlign: "center" }}>LOADING...</h2>
);
}
return (
<Layout title="Dashboard">
<DashboardNav isAdmin={props.isAdmin} url={props.userId} />
<h1 style={{ marginTop: 60, textAlign: "center" }}>
WELCOME{" "}
{status === "authenticated"
? `${session.user.name.toUpperCase()}`
: ""}
!
</h1>
<DashboardBody />
<FooterMain
domainUrl={props.domainUrl}
url={props.userId}
isAdmin={props.isAdmin}
/>
</Layout>
);
}
export async function getServerSideProps(context) {
const session = await getSession(context);
let userId = null;
let isAdmin = false;
const domainUrl = process.env.DOMAIN_URL;
if (session) {
await axios
.get(`${domainUrl}/api/user-email?email=${session.user.email}`)
.then((res) => {
userId = res.data._id;
if (res.data.isAdmin) {
isAdmin = res.data.isAdmin;
}
})
.catch((err) => {
console.log("err", err);
});
}
return {
props: {
session,
userId,
isAdmin,
domainUrl,
},
};
}