2019-07-30 13:47:32 -06:00
|
|
|
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";
|
2018-08-08 08:44:12 -06:00
|
|
|
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";
|
2017-10-10 08:47:49 -06:00
|
|
|
import { connectDevice } from "../connectivity/connect_device";
|
2018-02-14 22:07:36 -07:00
|
|
|
import { getFirstPartyFarmwareList } from "../farmware/actions";
|
2019-07-30 13:47:32 -06:00
|
|
|
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);
|
2017-10-10 15:07:23 -06:00
|
|
|
dispatch(connectDevice(authState));
|
2017-07-02 18:43:12 -06:00
|
|
|
}
|
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);
|
2017-07-05 14:33:06 -06:00
|
|
|
axios.interceptors.request.use(requestFulfilled(auth));
|
2017-11-12 21:03:41 -07:00
|
|
|
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
|
|
|
|
};
|
|
|
|
}
|