Remove superfluous goHome() calls

pull/498/head
Rick Carlino 2017-10-12 16:48:41 -05:00
parent 23cae64af7
commit 18584552f8
7 changed files with 18 additions and 28 deletions

View File

@ -9,8 +9,7 @@ import {
SemverResult,
trim,
bitArray,
withTimeout,
goHome
withTimeout
} from "../util";
describe("util", () => {
describe("safeStringFetch", () => {
@ -175,9 +174,3 @@ describe("withTimeout()", () => {
}, fail);
});
});
// describe("goHome()", () => {
// it("clears the session and sends user to '/'", () => {
// goHome();
// });
// });

View File

@ -2,7 +2,7 @@ import { didLogin, setToken } from "../auth/actions";
import { Thunk } from "../redux/interfaces";
import { Session } from "../session";
import { maybeRefreshToken } from "../refresh_token";
import { goHome, withTimeout } from "../util";
import { withTimeout } from "../util";
import { AuthState } from "../auth/interfaces";
export const storeToken =
@ -24,9 +24,9 @@ export function ready(): Thunk {
const auth = Session.fetchStoredToken() || getState().auth;
if (auth) {
withTimeout(MAX_TOKEN_WAIT_TIME, maybeRefreshToken(auth))
.then(storeToken(auth, dispatch), goHome);
.then(storeToken(auth, dispatch), Session.clear);
} else {
goHome();
Session.clear();
}
};
}

View File

@ -2,10 +2,11 @@ import * as React from "react";
import axios from "axios";
import { t } from "i18next";
import { error as log, init as logInit } from "farmbot-toastr";
import { prettyPrintApiErrors, goHome } from "../util";
import { prettyPrintApiErrors } from "../util";
import { API } from "../api";
import { State, Props } from "./interfaces";
import { Widget, WidgetHeader, WidgetBody, Row, Col } from "../ui/index";
import { Session } from "../session";
export class PasswordReset extends React.Component<Props, State> {
constructor() {
@ -41,11 +42,11 @@ export class PasswordReset extends React.Component<Props, State> {
id: token,
password,
password_confirmation: passwordConfirmation,
}).then(() => {
goHome();
}).catch((error: string) => {
log(prettyPrintApiErrors(error as {}));
});
})
.then(Session.clear)
.catch((error: string) => {
log(prettyPrintApiErrors(error as {}));
});
}
render() {

View File

@ -1,8 +1,9 @@
import axios from "axios";
import { API } from "./api/index";
import { AuthState } from "./auth/interfaces";
import { HttpData, goHome } from "./util";
import { HttpData } from "./util";
import { setToken } from "./auth/actions";
import { Session } from "./session";
type Resp = HttpData<AuthState>;
@ -17,5 +18,5 @@ const ok = (x: Resp) => {
export let maybeRefreshToken = (old: AuthState): Promise<AuthState> => {
API.setBaseUrl(old.token.unencoded.iss);
setToken(old); // Precaution: The Axios interceptors might not be set yet.
return axios.get(API.current.tokensPath).then(ok, goHome);
return axios.get(API.current.tokensPath).then(ok, Session.clear);
};

View File

@ -9,7 +9,7 @@ import { history } from "./history";
import { Store } from "./redux/interfaces";
import { ready } from "./config/actions";
import { Session } from "./session";
import { isMobile, attachToRoot, goHome } from "./util";
import { isMobile, attachToRoot } from "./util";
import { Callback } from "i18next";
interface RootComponentProps {
@ -292,7 +292,7 @@ export class RootComponent extends React.Component<RootComponentProps, {}> {
const notLoggedIn = !Session.fetchStoredToken();
const restrictedArea = window.location.pathname.includes("/app");
if (notLoggedIn && restrictedArea) {
goHome();
Session.clear();
}
// ==== END HACK ====
return <Provider store={_store}>

View File

@ -36,10 +36,11 @@ export namespace Session {
}
/** Clear localstorage and sessionstorage. */
export function clear() {
export function clear(): never {
localStorage.clear();
sessionStorage.clear();
window.location.href = window.location.origin;
throw new Error("session cleared");
}
/** Fetch a *boolean* value from localstorage. Returns `undefined` when

View File

@ -449,12 +449,6 @@ export function bail(message: string): never {
throw new Error(message);
}
export const goHome = (): never => {
Session.clear();
window.location.href = "/";
return bail("Going to home page.");
};
// Thanks,
// https://italonascimento.github.io
// /applying-a-timeout-to-your-promises/#implementing-the-timeout