MM-13859 set the auth token when fetching images from the server (#2537)
* MM-13859 set the auth token when fetching images from the server * set the image_cache siteURL from redux * HH suggestion Co-Authored-By: enahum <nahumhbl@gmail.com> * Removing comment
This commit is contained in:
parent
2f997f631a
commit
2ec7a6bba6
2 changed files with 27 additions and 1 deletions
|
|
@ -8,12 +8,14 @@ import {createTransform, persistStore} from 'redux-persist';
|
|||
|
||||
import {ErrorTypes, GeneralTypes} from 'mattermost-redux/action_types';
|
||||
import {General, RequestStatus} from 'mattermost-redux/constants';
|
||||
import {getConfig} from 'mattermost-redux/selectors/entities/general';
|
||||
import configureStore from 'mattermost-redux/store';
|
||||
import EventEmitter from 'mattermost-redux/utils/event_emitter';
|
||||
|
||||
import {NavigationTypes, ViewTypes} from 'app/constants';
|
||||
import appReducer from 'app/reducers';
|
||||
import {throttle} from 'app/utils/general';
|
||||
import {getSiteUrl, setSiteUrl} from 'app/utils/image_cache_manager';
|
||||
import {createSentryMiddleware} from 'app/utils/sentry/middleware';
|
||||
|
||||
import mattermostBucket from 'app/mattermost_bucket';
|
||||
|
|
@ -187,6 +189,12 @@ export default function configureAppStore(initialState) {
|
|||
// check to see if the logout request was successful
|
||||
store.subscribe(async () => {
|
||||
const state = store.getState();
|
||||
const config = getConfig(state);
|
||||
|
||||
if (getSiteUrl() !== config?.SiteURL) {
|
||||
setSiteUrl(config.SiteURL);
|
||||
}
|
||||
|
||||
if ((state.requests.users.logout.status === RequestStatus.SUCCESS || state.requests.users.logout.status === RequestStatus.FAILURE) && !purging) {
|
||||
purging = true;
|
||||
|
||||
|
|
@ -211,6 +219,7 @@ export default function configureAppStore(initialState) {
|
|||
mattermostBucket.removePreference('cert', Config.AppGroupId);
|
||||
mattermostBucket.removePreference('emm', Config.AppGroupId);
|
||||
mattermostBucket.removeFile('entities', Config.AppGroupId);
|
||||
setSiteUrl(null);
|
||||
|
||||
setTimeout(() => {
|
||||
purging = false;
|
||||
|
|
|
|||
|
|
@ -6,12 +6,15 @@
|
|||
import {Platform} from 'react-native';
|
||||
import RNFetchBlob from 'rn-fetch-blob';
|
||||
|
||||
import {Client4} from 'mattermost-redux/client';
|
||||
|
||||
import {DeviceTypes} from 'app/constants';
|
||||
import mattermostBucket from 'app/mattermost_bucket';
|
||||
|
||||
import LocalConfig from 'assets/config';
|
||||
|
||||
const {IMAGES_PATH} = DeviceTypes;
|
||||
let siteUrl;
|
||||
|
||||
export default class ImageCacheManager {
|
||||
static listeners = {};
|
||||
|
|
@ -41,7 +44,13 @@ export default class ImageCacheManager {
|
|||
certificate,
|
||||
};
|
||||
|
||||
this.downloadTask = await RNFetchBlob.config(options).fetch('GET', uri);
|
||||
const headers = {};
|
||||
if (uri.includes(Client4.getUrl()) || uri.includes(siteUrl)) {
|
||||
headers.Authorization = `Bearer ${Client4.getToken()}`;
|
||||
headers['X-Requested-With'] = 'XMLHttpRequest';
|
||||
}
|
||||
|
||||
this.downloadTask = await RNFetchBlob.config(options).fetch('GET', uri, headers);
|
||||
if (this.downloadTask.respInfo.respType === 'text') {
|
||||
throw new Error();
|
||||
}
|
||||
|
|
@ -79,6 +88,14 @@ export const getCacheFile = async (name, uri) => {
|
|||
return {exists, path};
|
||||
};
|
||||
|
||||
export const getSiteUrl = () => {
|
||||
return siteUrl;
|
||||
};
|
||||
|
||||
export const setSiteUrl = (url) => {
|
||||
siteUrl = url;
|
||||
};
|
||||
|
||||
const isDownloading = (uri) => Boolean(ImageCacheManager.listeners[uri]);
|
||||
|
||||
const addListener = (uri, listener) => {
|
||||
|
|
|
|||
Loading…
Reference in a new issue