Logout
@@ -42,7 +50,7 @@ export default function layout() {
return (
-
+
@@ -64,12 +72,13 @@ export default function layout() {
} />
} />
} />
+ } />
-
+
);
}
\ No newline at end of file
diff --git a/sports-matcher/client/src/components/AuthenticationGuard.js b/sports-matcher/client/src/components/AuthenticationGuard.js
index 84622e4..5a85cd9 100644
--- a/sports-matcher/client/src/components/AuthenticationGuard.js
+++ b/sports-matcher/client/src/components/AuthenticationGuard.js
@@ -1,30 +1,34 @@
import React from "react";
import { Navigate } from "react-router-dom";
-import { globalContext } from "../context";
+import context from "../globals";
import { apiClient } from "../utils/httpClients";
export default class AuthenticationGuard extends React.Component {
constructor(props) {
super(props);
}
+ static contextType = context;
async componentDidMount() {
if (!this.context.user) {
let userDataResponse = await apiClient.get("/user/");
if (userDataResponse.status === 200) {
- this.context.user = userDataResponse.data;
+ this.context.setUser(userDataResponse.data);
}
}
}
- static contextType = globalContext;
render() {
- if (!this.context.user) {
- return (
-
- );
- }
- return;
+ return (
+
+ {val => {
+ if (!val.user) {
+ console.log(val);
+ return ;
+ }
+ }}
+
+ );
}
}
\ No newline at end of file
diff --git a/sports-matcher/client/src/context.js b/sports-matcher/client/src/context.js
deleted file mode 100644
index 8f6e61c..0000000
--- a/sports-matcher/client/src/context.js
+++ /dev/null
@@ -1,3 +0,0 @@
-import React from "react";
-
-export const globalContext = React.createContext({});
diff --git a/sports-matcher/client/src/globals.js b/sports-matcher/client/src/globals.js
new file mode 100644
index 0000000..4859047
--- /dev/null
+++ b/sports-matcher/client/src/globals.js
@@ -0,0 +1,7 @@
+import React from "react";
+
+export default React.createContext({
+ user: null,
+ setUser: () => { },
+ navigate: () => { }
+});
\ No newline at end of file
diff --git a/sports-matcher/client/src/pages/Dashboard.js b/sports-matcher/client/src/pages/Dashboard.js
index 9374e5a..61d828b 100644
--- a/sports-matcher/client/src/pages/Dashboard.js
+++ b/sports-matcher/client/src/pages/Dashboard.js
@@ -4,8 +4,8 @@ import "../styles/Dashboard.css";
import { apiClient } from "../utils/httpClients.js";
import MatchInfoCardDisplay from "../components/MatchInfoCardDisplay";
import SportInfoCardDisplay from "../components/SportInfoCardDisplay";
-import { globalContext } from "../context";
import AuthenticationGuard from "../components/AuthenticationGuard";
+import context from "../globals";
export default class Dashboard extends React.Component {
constructor(props) {
@@ -18,7 +18,7 @@ export default class Dashboard extends React.Component {
};
}
- static contextType = globalContext;
+ static contextType = context;
async componentDidMount() {
this.setState({ user: this.context.user });
diff --git a/sports-matcher/client/src/pages/Login.js b/sports-matcher/client/src/pages/Login.js
index 82e99a5..a8ad7d1 100644
--- a/sports-matcher/client/src/pages/Login.js
+++ b/sports-matcher/client/src/pages/Login.js
@@ -1,8 +1,7 @@
import React from "react";
import { Alert, Button, Card, Container, Form } from "react-bootstrap";
-import { globalContext } from "../context";
+import context from "../globals";
import { apiClient } from "../utils/httpClients";
-import AuthenticationGuard from "../components/AuthenticationGuard";
export default class Login extends React.Component {
constructor(props) {
@@ -16,7 +15,7 @@ export default class Login extends React.Component {
this.attemptLogin = this.attemptLogin.bind(this);
}
- static contextType = globalContext;
+ static contextType = context;
async componentDidMount() {
}
@@ -32,6 +31,7 @@ export default class Login extends React.Component {
}
});
if (loginResponse.status === 200) {
+ this.context.user = loginResponse.data;
this.context.navigate("/dashboard", { replace: true });
} else if (loginResponse.status === 401) {
this.setState({ errorDisplayed: true });
diff --git a/sports-matcher/client/src/pages/Logout.js b/sports-matcher/client/src/pages/Logout.js
index 517d95c..7c898eb 100644
--- a/sports-matcher/client/src/pages/Logout.js
+++ b/sports-matcher/client/src/pages/Logout.js
@@ -1,5 +1,5 @@
import React from "react";
-import { useNavigate } from "react-router-dom";
+import globals from "../globals";
import { apiClient } from "../utils/httpClients";
export default class Logout extends React.Component {
@@ -7,16 +7,19 @@ export default class Logout extends React.Component {
super(props);
}
+ static contextType = globals;
+
async componentDidMount() {
const logoutResponse = await apiClient.get("/user/logout");
- let navigation = useNavigate();
if (logoutResponse.status === 401) {
- navigation("/dashboard", { replace: true });
+ globals.navigate("/dashboard", { replace: true });
} else {
this.redirectTimer = setTimeout(() => {
- navigation("/", { replace: true });
+ globals.navigate("/", { replace: true });
}, 2000);
}
+
+ globals.setUser(null);
}
async componentWillUnmount() {
diff --git a/sports-matcher/server/controllers/userController.js b/sports-matcher/server/controllers/userController.js
index 520b788..7267b64 100644
--- a/sports-matcher/server/controllers/userController.js
+++ b/sports-matcher/server/controllers/userController.js
@@ -16,7 +16,8 @@ UserController.post("/login", needDatabase, async (req, res) => {
} else {
req.session.userId = user._id;
req.session.email = user.email;
- res.status(200).send("Authenticated.");
+ user.password = undefined;
+ res.status(200).send(user);
}
} catch (error) {
if (error.name === "TypeError") {