fix: Multiple API calls when bypassing /login

This commit is contained in:
Alexander Polynomdivision
2018-09-23 16:30:14 +02:00
parent 08cd51c2a3
commit be7a616fb5
7 changed files with 41 additions and 13 deletions

View File

@@ -3,7 +3,7 @@ import * as React from "react";
import { BrowserRouter, Route, Redirect } from "react-router-dom";
import AuthRoute from "../security/AuthRoute";
import { setSessionToken, removeSessionToken } from "../security/Token";
import { setSessionToken, removeSessionToken, getSessionToken } from "../security/Token";
import Dashboard from "../containers/Dashboard";
import LoginPage from "../containers/LoginPage";
@@ -29,13 +29,21 @@ interface IProps {
user: IUser;
setAuthenticated: (status: boolean) => void;
setDidLogin: (status: boolean) => void;
setUser: (user: IUser) => void;
};
// TODO: Replace the sessionStorage with localStorage?
// TODO: Cache API-Calls
// TODO: When mounting without a login, check if the sessionToken is still valid
export default class Application extends React.Component<IProps> {
componentDidMount() {
// TODO: Ask the server if our session is still valid
// TODO: When asking the server if our session is still valid, a spinner
// should be shown
if (getSessionToken(window) !== null) {
this.props.setAuthenticated(true);
}
}
getLevels(): Promise<ILevel[]> {
console.log("STUB: Application::getLevels");
@@ -143,8 +151,6 @@ export default class Application extends React.Component<IProps> {
}).then(resp => resp.json(),
err => rej(err))
.then(data => {
console.log(data);
if (data.error === "0") {
res(data.data.topTen);
} else {
@@ -164,8 +170,6 @@ export default class Application extends React.Component<IProps> {
}).then(resp => resp.json(),
err => rej(err))
.then(data => {
console.log(data);
if (data.error === "0") {
res(data.data);
} else {
@@ -215,6 +219,7 @@ export default class Application extends React.Component<IProps> {
if (resp.error === "0") {
// Successful login
this.props.setUser(resp.data);
this.props.setDidLogin(true);
setSessionToken(window, resp.data.sessionToken);
this.props.setAuthenticated(true);
@@ -234,8 +239,6 @@ export default class Application extends React.Component<IProps> {
// Checks whether the user is logged in
isAuthenticated = () => {
// TODO: Security?
// TODO: Implement
return this.props.authenticated;
}