Farmbot-Web-App/frontend/auth/actions.ts

47 lines
1.7 KiB
TypeScript
Raw Permalink Normal View History

import axios from "axios";
2018-03-07 20:42:34 -07:00
import {
2020-02-15 11:30:23 -07:00
fetchReleases, fetchMinOsFeatureData,
2020-02-28 09:35:32 -07:00
fetchLatestGHBetaRelease,
2020-03-13 15:06:40 -06:00
fetchOsReleaseNotes,
2018-03-07 20:42:34 -07:00
} from "../devices/actions";
2017-06-29 12:54:02 -06:00
import { AuthState } from "./interfaces";
import { ReduxAction } from "../redux/interfaces";
2017-06-29 12:54:02 -06:00
import * as Sync from "../sync/actions";
import { API } from "../api";
import {
responseFulfilled,
responseRejected,
2020-02-28 09:35:32 -07:00
requestFulfilled,
2017-06-29 12:54:02 -06:00
} from "../interceptors";
import { Actions } from "../constants";
import { connectDevice } from "../connectivity/connect_device";
2018-02-14 22:07:36 -07:00
import { getFirstPartyFarmwareList } from "../farmware/actions";
import { readOnlyInterceptor } from "../read_only_mode";
2017-06-29 12:54:02 -06:00
export function didLogin(authState: AuthState, dispatch: Function) {
API.setBaseUrl(authState.token.unencoded.iss);
2018-01-10 17:37:36 -07:00
const { os_update_server, beta_os_update_server } = authState.token.unencoded;
dispatch(fetchReleases(os_update_server));
2018-01-11 17:29:01 -07:00
beta_os_update_server && beta_os_update_server != "NOT_SET" &&
2019-01-04 14:26:52 -07:00
dispatch(fetchLatestGHBetaRelease(beta_os_update_server));
2018-02-14 22:07:36 -07:00
dispatch(getFirstPartyFarmwareList());
2020-03-13 15:06:40 -06:00
dispatch(fetchMinOsFeatureData());
dispatch(fetchOsReleaseNotes());
2017-10-12 14:28:19 -06:00
dispatch(setToken(authState));
2017-06-29 12:54:02 -06:00
Sync.fetchSyncData(dispatch);
dispatch(connectDevice(authState));
}
2017-06-29 12:54:02 -06:00
/** Very important. Once called, all outbound HTTP requests will
* have a JSON Web Token attached to their "Authorization" header,
* thereby granting access to the API. */
2017-10-12 14:28:19 -06:00
export function setToken(auth: AuthState): ReduxAction<AuthState> {
2019-07-30 12:12:55 -06:00
axios.interceptors.request.use(readOnlyInterceptor);
axios.interceptors.request.use(requestFulfilled(auth));
axios.interceptors.response.use(responseFulfilled, responseRejected);
2017-06-29 12:54:02 -06:00
return {
2017-10-12 14:28:19 -06:00
type: Actions.REPLACE_TOKEN,
2017-06-29 12:54:02 -06:00
payload: auth
};
}