From f26ce20e130016fb353dfb1121d700074edbcd05 Mon Sep 17 00:00:00 2001 From: Alexander Polynomdivision Date: Thu, 20 Sep 2018 20:26:40 +0200 Subject: [PATCH] fix: Not setting the auth state in Application --- frontend/src/components/app.tsx | 8 +++++--- frontend/src/models/server.ts | 4 ++++ frontend/src/pages/login.tsx | 8 ++++++-- frontend/src/reducers/index.ts | 2 +- 4 files changed, 16 insertions(+), 6 deletions(-) create mode 100644 frontend/src/models/server.ts diff --git a/frontend/src/components/app.tsx b/frontend/src/components/app.tsx index 43242db..b4b6e84 100644 --- a/frontend/src/components/app.tsx +++ b/frontend/src/components/app.tsx @@ -22,6 +22,7 @@ import { ILearner } from "../models/learner"; import { IVocab, VocabType } from "../models/vocab"; import { IReviewMetadata, ReviewType } from "../models/review"; import { IUser } from "../models/user"; +import { IResponse } from "../models/server"; interface IProps { authenticated: boolean; @@ -195,7 +196,7 @@ export default class Application extends React.Component { }); } - login = (username: string, password: string): Promise => { + login = (username: string, password: string): Promise => { return new Promise((res, rej) => { fetch(`${BACKEND_URL}/login`, { method: "POST", @@ -209,15 +210,16 @@ export default class Application extends React.Component { password, }), }).then(data => data.json()) - .then(resp => { + .then((resp: IResponse) => { if (resp.error === "0") { // Successful login this.props.setUser(resp.data); setSessionToken(window, resp.data.sessionToken); + this.props.setAuthenticated(true); res(resp.data); } else { - rej({}); + rej(resp); } }); }); diff --git a/frontend/src/models/server.ts b/frontend/src/models/server.ts new file mode 100644 index 0000000..5b6ba2b --- /dev/null +++ b/frontend/src/models/server.ts @@ -0,0 +1,4 @@ +export interface IResponse { + error: string; + data: any; +}; diff --git a/frontend/src/pages/login.tsx b/frontend/src/pages/login.tsx index e4a28d5..77bc749 100644 --- a/frontend/src/pages/login.tsx +++ b/frontend/src/pages/login.tsx @@ -11,9 +11,10 @@ import Snackbar from "@material-ui/core/Snackbar"; import { withRouter } from "react-router-dom"; import { IUser } from "../models/user"; +import { IResponse } from "../models/server"; interface IProps { - login: (username: string, password: string) => Promise; + login: (username: string, password: string) => Promise; authenticated: boolean; history: any; @@ -35,6 +36,9 @@ const LoginPageWithRouter = withRouter( const username = this.usernameRef.value || ""; const password = this.passwordRef.value || ""; this.props.login(username, password).then((res: IUser) => { + // Stop the loading animation + this.props.setLoading(false); + if (res.showWelcome) { // If the user logs in for the first time, a welcome // screen should be shown @@ -42,7 +46,7 @@ const LoginPageWithRouter = withRouter( } else { this.props.history.push("/dashboard"); } - }, (err) => { + }, (err: IResponse) => { this.props.setLoading(false); this.props.setSnackbar(true, "Failed to log in"); }); diff --git a/frontend/src/reducers/index.ts b/frontend/src/reducers/index.ts index aef92df..cec60ad 100644 --- a/frontend/src/reducers/index.ts +++ b/frontend/src/reducers/index.ts @@ -58,7 +58,7 @@ const initialState: IState = { drawerButton: true, // Is the user authenticated? // TODO: Set this to false - authenticated: true, + authenticated: false, user: { score: 0,