Log user out on 401

pull/757/head
Rick Carlino 2018-03-29 09:54:37 -05:00
parent 4205d83b6d
commit 52662c735d
3 changed files with 32 additions and 2 deletions

View File

@ -4,10 +4,12 @@ jest.mock("../connectivity/data_consistency", () => {
};
});
import { responseFulfilled } from "../interceptors";
import { responseFulfilled, isLocalRequest } from "../interceptors";
import { AxiosResponse } from "axios";
import { uuid } from "farmbot";
import { startTracking } from "../connectivity/data_consistency";
import { SafeError } from "../interceptor_support";
import { API } from "../api";
interface FakeProps {
uuid: string;
@ -39,3 +41,18 @@ describe("responseFulfilled", () => {
expect(startTracking).not.toHaveBeenCalled();
});
});
const fake =
(responseURL: string): Partial<SafeError> => ({ request: { responseURL } });
describe("isLocalRequest", () => {
it("determines if the URL is local vs. Github, Openfarm, etc...", () => {
API.setBaseUrl("http://localhost:3000");
const openfarm = fake("http://openfarm.cc/foo/bar") as SafeError;
expect(isLocalRequest(openfarm)).toBe(false);
const api = fake("http://localhost:3000/api/tools/1") as SafeError;
expect(isLocalRequest(api)).toBe(true);
});
});

View File

@ -32,6 +32,9 @@ export function inferUpdateId(url: string) {
* Sometimes it will be a real Axios error, other times it will not be.
*/
export interface SafeError {
request: {
responseURL: string;
},
response: {
status: number;
};

View File

@ -12,12 +12,20 @@ import { Content } from "./constants";
import { dispatchNetworkUp, dispatchNetworkDown } from "./connectivity/index";
import { Dictionary } from "farmbot";
import { outstandingRequests } from "./connectivity/data_consistency";
import { Session } from "./session";
export function responseFulfilled(input: AxiosResponse): AxiosResponse {
dispatchNetworkUp("user.api");
return input;
}
/** These will raise type errors if our _.get usage ever requires changing. */
const request: keyof SafeError = "request";
const responseUrl: keyof SafeError["request"] = "responseURL";
export const isLocalRequest = (x: SafeError) =>
_.get(x, [request, responseUrl], "").includes(API.current.baseUrl);
let ONLY_ONCE = true;
export function responseRejected(x: SafeError | undefined) {
if (x && isSafeError(x)) {
@ -26,7 +34,6 @@ export function responseRejected(x: SafeError | undefined) {
const b = x.response.status > 399;
// Openfarm API was sending too many 404's.
const c = !_.get(x, "response.config.url", "").includes("openfarm.cc/");
if (a && b && c) {
setTimeout(() => {
// Explicitly throw error so error reporting tool will save it.
@ -35,6 +42,9 @@ export function responseRejected(x: SafeError | undefined) {
}, 1);
}
switch (x.response.status) {
case 401:
isLocalRequest(x) && Session.clear();
break;
case 404:
// Log 404's, but don't generate any noise for the user.
break;