fix: Not setting the auth state in Application

This commit is contained in:
Alexander Polynomdivision 2018-09-20 20:26:40 +02:00
parent ba1521fbd8
commit f26ce20e13
4 changed files with 16 additions and 6 deletions

View File

@ -22,6 +22,7 @@ import { ILearner } from "../models/learner";
import { IVocab, VocabType } from "../models/vocab"; import { IVocab, VocabType } from "../models/vocab";
import { IReviewMetadata, ReviewType } from "../models/review"; import { IReviewMetadata, ReviewType } from "../models/review";
import { IUser } from "../models/user"; import { IUser } from "../models/user";
import { IResponse } from "../models/server";
interface IProps { interface IProps {
authenticated: boolean; authenticated: boolean;
@ -195,7 +196,7 @@ export default class Application extends React.Component<IProps> {
}); });
} }
login = (username: string, password: string): Promise<IUser | {}> => { login = (username: string, password: string): Promise<IUser | IResponse> => {
return new Promise((res, rej) => { return new Promise((res, rej) => {
fetch(`${BACKEND_URL}/login`, { fetch(`${BACKEND_URL}/login`, {
method: "POST", method: "POST",
@ -209,15 +210,16 @@ export default class Application extends React.Component<IProps> {
password, password,
}), }),
}).then(data => data.json()) }).then(data => data.json())
.then(resp => { .then((resp: IResponse) => {
if (resp.error === "0") { if (resp.error === "0") {
// Successful login // Successful login
this.props.setUser(resp.data); this.props.setUser(resp.data);
setSessionToken(window, resp.data.sessionToken); setSessionToken(window, resp.data.sessionToken);
this.props.setAuthenticated(true);
res(resp.data); res(resp.data);
} else { } else {
rej({}); rej(resp);
} }
}); });
}); });

View File

@ -0,0 +1,4 @@
export interface IResponse {
error: string;
data: any;
};

View File

@ -11,9 +11,10 @@ import Snackbar from "@material-ui/core/Snackbar";
import { withRouter } from "react-router-dom"; import { withRouter } from "react-router-dom";
import { IUser } from "../models/user"; import { IUser } from "../models/user";
import { IResponse } from "../models/server";
interface IProps { interface IProps {
login: (username: string, password: string) => Promise<IUser | {}>; login: (username: string, password: string) => Promise<IUser | IResponse>;
authenticated: boolean; authenticated: boolean;
history: any; history: any;
@ -35,6 +36,9 @@ const LoginPageWithRouter = withRouter(
const username = this.usernameRef.value || ""; const username = this.usernameRef.value || "";
const password = this.passwordRef.value || ""; const password = this.passwordRef.value || "";
this.props.login(username, password).then((res: IUser) => { this.props.login(username, password).then((res: IUser) => {
// Stop the loading animation
this.props.setLoading(false);
if (res.showWelcome) { if (res.showWelcome) {
// If the user logs in for the first time, a welcome // If the user logs in for the first time, a welcome
// screen should be shown // screen should be shown
@ -42,7 +46,7 @@ const LoginPageWithRouter = withRouter(
} else { } else {
this.props.history.push("/dashboard"); this.props.history.push("/dashboard");
} }
}, (err) => { }, (err: IResponse) => {
this.props.setLoading(false); this.props.setLoading(false);
this.props.setSnackbar(true, "Failed to log in"); this.props.setSnackbar(true, "Failed to log in");
}); });

View File

@ -58,7 +58,7 @@ const initialState: IState = {
drawerButton: true, drawerButton: true,
// Is the user authenticated? // Is the user authenticated?
// TODO: Set this to false // TODO: Set this to false
authenticated: true, authenticated: false,
user: { user: {
score: 0, score: 0,