Login now displays an error message on a failed login.

This commit is contained in:
Harrison Deng 2022-04-05 19:51:13 -05:00
parent c4c4031e4c
commit 999f884694
2 changed files with 26 additions and 9 deletions

View File

@ -1,5 +1,5 @@
import React from "react"; import React from "react";
import { Button, Card, Container, Form } from "react-bootstrap"; import { Alert, Button, Card, Container, Form } from "react-bootstrap";
import { globalContext } from "../context"; import { globalContext } from "../context";
import { apiClient } from "../utils/httpClients"; import { apiClient } from "../utils/httpClients";
import { guard } from "../utils/routing"; import { guard } from "../utils/routing";
@ -9,7 +9,8 @@ export default class Login extends React.Component {
super(props); super(props);
this.state = { this.state = {
email: "", email: "",
password: "" password: "",
errorDisplayed: false,
}; };
this.attemptLogin = this.attemptLogin.bind(this); this.attemptLogin = this.attemptLogin.bind(this);
@ -33,18 +34,36 @@ export default class Login extends React.Component {
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.navigate("/dashboard", { replace: true }); this.context.navigate("/dashboard", { replace: true });
} else if (loginResponse.status === 401) {
this.setState({ errorDisplayed: true });
} }
} }
render() { render() {
let errorMsg = (
<div></div>
);
if (this.state.errorDisplayed) {
errorMsg = (
< Alert variant="danger" onClose={() => this.setState({ errorDisplayed: false })} dismissible >
<Alert.Heading>Incorrect credentials</Alert.Heading>
<p>Double check your provided e-mail and password!</p>
</Alert >
);
}
return ( return (
<div className="d-flex justify-content-center align-items-center <div className="d-flex justify-content-center align-items-center
page-root"> page-root">
<Container style={{ maxWidth: "30rem" }}> {errorMsg}
<Container style={{ maxWidth: "35rem" }}>
<Card> <Card>
<Card.Body> <Card.Body>
<Card.Title>Login</Card.Title> <Card.Title>Login</Card.Title>

View File

@ -1,9 +1,7 @@
import axios from "axios"; import axios from "axios";
const apiClientConf = { export const apiClient = axios.create({
baseURL: process.env.REACT_APP_API_HOST + "/api/", baseURL: process.env.REACT_APP_API_HOST + "/api/",
timeout: 5000, timeout: 5000,
withCredentials: process.env.NODE_ENV === "development" withCredentials: process.env.NODE_ENV === "development",
}; });
console.log(apiClientConf);
export const apiClient = axios.create(apiClientConf);