Fixed login route and authentication guards.

This commit is contained in:
Harrison Deng 2022-04-07 13:36:49 -05:00
parent ef66904c60
commit fa8552d488
7 changed files with 33 additions and 53 deletions

View File

@ -1,6 +1,6 @@
import "./styles/Layout.css"; import "./styles/Layout.css";
import "./styles/extra.css"; import "./styles/extra.css";
import { useEffect, useState } from "react"; import { useState } from "react";
import { NavLink, Route, Routes, useNavigate } from "react-router-dom"; import { NavLink, Route, Routes, useNavigate } from "react-router-dom";
import Welcome from "./pages/Welcome"; import Welcome from "./pages/Welcome";
import Navbar from "react-bootstrap/Navbar"; import Navbar from "react-bootstrap/Navbar";
@ -15,16 +15,12 @@ import Context from "./globals.js";
export default function layout() { export default function layout() {
const [state, setState] = useState({ const [globals, setGlobals] = useState({
user: null, user: null,
setUser: setUser, update: (updates, onUpdate) => setGlobals((state) => { return { ...state, ...updates }; }, onUpdate),
navigate: useNavigate() navigate: useNavigate()
}); });
function setUser(user) {
setState({ user: user });
}
let identityDisplay = ( let identityDisplay = (
<Nav> <Nav>
<li className="nav-item"> <li className="nav-item">
@ -36,11 +32,11 @@ export default function layout() {
</Nav> </Nav>
); );
if (state.user) { if (globals.user) {
identityDisplay = ( identityDisplay = (
<Nav> <Nav>
<li className="nav-item"> <li className="nav-item">
<NavLink className="nav-link" to="/" >Hi, {state.user.firstName}</NavLink> <NavLink className="nav-link" to="/" >Hi, {globals.user.firstName}</NavLink>
</li> </li>
<li className="nav-item"> <li className="nav-item">
<NavLink className="nav-link" to="/logout" >Logout</NavLink> <NavLink className="nav-link" to="/logout" >Logout</NavLink>
@ -51,7 +47,7 @@ export default function layout() {
return ( return (
<div id="app"> <div id="app">
<Context.Provider value={state}> <Context.Provider value={globals}>
<header> <header>
<Navbar bg="light" expand="md"> <Navbar bg="light" expand="md">
<Container> <Container>

View File

@ -1,34 +1,25 @@
import React from "react"; import React from "react";
import { Navigate } from "react-router-dom"; import globals from "../globals";
import context from "../globals";
import { apiClient } from "../utils/httpClients"; import { apiClient } from "../utils/httpClients";
export default class AuthenticationGuard extends React.Component { export default class AuthenticationGuard extends React.Component {
constructor(props) { constructor(props) {
super(props); super(props);
} }
static contextType = context; static contextType = globals;
async componentDidMount() { async componentDidMount() {
if (!this.context.user) { if (!this.context.user) {
let userDataResponse = await apiClient.get("/user/"); let userDataResponse = await apiClient.get("/user");
if (userDataResponse.status === 200) { if (userDataResponse.status === 200) {
this.context.setUser(userDataResponse.data); this.context.update({ user: userDataResponse.data });
} else if (userDataResponse.status == 401) {
this.context.navigate("/signup");
} }
} }
} }
render() { render() {
return ( return null;
<context.Consumer>
{val => {
if (!val.user) {
console.log(val);
return <Navigate to="/signup" replace="true" />;
}
}}
</context.Consumer>
);
} }
} }

View File

@ -2,6 +2,6 @@ import React from "react";
export default React.createContext({ export default React.createContext({
user: null, user: null,
setUser: () => { }, update: () => { },
navigate: () => { } navigate: () => { }
}); });

View File

@ -5,7 +5,7 @@ import { apiClient } from "../utils/httpClients.js";
import MatchInfoCardDisplay from "../components/MatchInfoCardDisplay"; import MatchInfoCardDisplay from "../components/MatchInfoCardDisplay";
import SportInfoCardDisplay from "../components/SportInfoCardDisplay"; import SportInfoCardDisplay from "../components/SportInfoCardDisplay";
import AuthenticationGuard from "../components/AuthenticationGuard"; import AuthenticationGuard from "../components/AuthenticationGuard";
import context from "../globals"; import globals from "../globals";
export default class Dashboard extends React.Component { export default class Dashboard extends React.Component {
constructor(props) { constructor(props) {
@ -18,7 +18,7 @@ export default class Dashboard extends React.Component {
}; };
} }
static contextType = context; static contextType = globals;
async componentDidMount() { async componentDidMount() {
this.setState({ user: this.context.user }); this.setState({ user: this.context.user });

View File

@ -1,6 +1,6 @@
import React from "react"; import React from "react";
import { Alert, Button, Card, Container, Form } from "react-bootstrap"; import { Alert, Button, Card, Container, Form } from "react-bootstrap";
import context from "../globals"; import globals from "../globals";
import { apiClient } from "../utils/httpClients"; import { apiClient } from "../utils/httpClients";
export default class Login extends React.Component { export default class Login extends React.Component {
@ -15,24 +15,25 @@ export default class Login extends React.Component {
this.attemptLogin = this.attemptLogin.bind(this); this.attemptLogin = this.attemptLogin.bind(this);
} }
static contextType = context; static contextType = globals;
async componentDidMount() { async componentDidMount() {
} }
componentDidUpdate() {
if (this.context.user) {
this.context.navigate("/dashboard");
}
}
async attemptLogin(e) { async attemptLogin(e) {
e.preventDefault(); e.preventDefault();
const loginResponse = await apiClient.post("/user/login", { const loginResponse = await apiClient.post("/user/login", {
email: this.state.email, email: this.state.email,
password: this.state.password, password: this.state.password,
}, {
validateStatus: function (status) {
return status === 200 || status === 401 || status === 400;
}
}); });
if (loginResponse.status === 200) { if (loginResponse.status === 200) {
this.context.user = loginResponse.data; this.context.update({ user: loginResponse.data });
this.context.navigate("/dashboard", { replace: true });
} else if (loginResponse.status === 401) { } else if (loginResponse.status === 401) {
this.setState({ errorDisplayed: true }); this.setState({ errorDisplayed: true });
} }

View File

@ -11,15 +11,15 @@ export default class Logout extends React.Component {
async componentDidMount() { async componentDidMount() {
const logoutResponse = await apiClient.get("/user/logout"); const logoutResponse = await apiClient.get("/user/logout");
if (logoutResponse.status === 401) { if (logoutResponse.status === 200) {
globals.navigate("/dashboard", { replace: true });
} else {
this.redirectTimer = setTimeout(() => { this.redirectTimer = setTimeout(() => {
globals.navigate("/", { replace: true }); this.context.navigate("/", { replace: true });
}, 2000); }, 2000);
} else if (logoutResponse.status == 401) {
this.context.navigate("/", { replace: true });
} }
globals.setUser(null); this.context.update({ user: null });
} }
async componentWillUnmount() { async componentWillUnmount() {

View File

@ -1,15 +1,8 @@
import React from "react"; import React from "react";
import { Button, Table } from "react-bootstrap"; import { Button, Table } from "react-bootstrap";
import "../styles/Admin.css"; import "../styles/Admin.css";
import { globalContext } from "../context"; import globals from "../globals";
import { needUser } from "../utils/routing";
//import Button from "@mui/material/Button";
// import Typography from "@mui/material/Typography";
// import Container from "@mui/material/Container";
// import { TableContainer, TableCell, Table, TableBody, TableRow, TableHead, Paper } from "@mui/material";
// import { apiClient } from "../utils/httpClients.js";
// import MatchInfoCardDisplay from "../components/MatchInfoCardDisplay";
// import { needUser } from "../utils/routing.js";
export default class Admin extends React.Component { export default class Admin extends React.Component {
constructor(props) { constructor(props) {
super(props); super(props);
@ -40,10 +33,9 @@ export default class Admin extends React.Component {
} }
static contextType = globalContext; static contextType = globals;
async componentDidMount() { async componentDidMount() {
await needUser(this.context.navigate);
} }
DeleteButton() { DeleteButton() {