🌟 Fix download button (#210)
* Add auth cookie to avoid using auth token in the url * Fix all download button * Refactor some of the places where we download file
This commit is contained in:
@@ -19,6 +19,7 @@ import UserAPIClient from '../../features/users/api/user-api-client';
|
||||
import Application from '@features/applications/services/application-service';
|
||||
import LocalStorage from '@features/global/framework/local-storage-service';
|
||||
import Globals from '@features/global/services/globals-tdrive-app-service';
|
||||
import { Cookies } from 'react-cookie';
|
||||
|
||||
type AccountType = 'remote' | 'internal';
|
||||
export type LoginState = '' | 'app' | 'error' | 'signin' | 'logged_out' | 'logout';
|
||||
@@ -26,11 +27,16 @@ type InitState = '' | 'initializing' | 'initialized';
|
||||
|
||||
@TdriveService('AuthService')
|
||||
class AuthService {
|
||||
|
||||
public static AUTH_TOKEN_COOKIE = "X-AuthToken";
|
||||
|
||||
private provider: AuthProvider<any, any, any> | null = null;
|
||||
private logger: Logger.Logger;
|
||||
private initState: InitState = '';
|
||||
private cookies: Cookies = new Cookies();
|
||||
currentUserId = '';
|
||||
|
||||
|
||||
constructor() {
|
||||
this.logger = Logger.getLogger('AuthService');
|
||||
}
|
||||
@@ -125,7 +131,9 @@ class AuthService {
|
||||
|
||||
onNewToken(token?: JWTDataType): void {
|
||||
if (token) {
|
||||
console.log("Save auth token to storage and cookie")
|
||||
JWT.updateJWT(token);
|
||||
this.cookies.set(AuthService.AUTH_TOKEN_COOKIE, JWT.getJWT(), { path: "/" });
|
||||
// TODO: Update the user from API?
|
||||
// this.updateUser();
|
||||
}
|
||||
@@ -220,6 +228,7 @@ class AuthService {
|
||||
this.resetCurrentUser();
|
||||
LocalStorage.clear();
|
||||
JWT.clear();
|
||||
this.cookies.remove(AuthService.AUTH_TOKEN_COOKIE);
|
||||
}
|
||||
|
||||
setCurrentUser(user: UserType) {
|
||||
|
||||
@@ -134,7 +134,7 @@ class JWTStorage {
|
||||
this.logger.debug('authenticateCall: Updating user because the access token expired');
|
||||
this.renew()
|
||||
.then(() => {
|
||||
LoginService.updateUser(callback);
|
||||
return LoginService.updateUser(callback);
|
||||
})
|
||||
.catch(async () => {
|
||||
this.clear();
|
||||
|
||||
@@ -11,8 +11,6 @@ import Application from '../applications/services/application-service';
|
||||
import { UserType } from '@features/users/types/user';
|
||||
import { Cookies } from 'react-cookie';
|
||||
import InitService from '../global/services/init-service';
|
||||
import { useRecoilState } from "recoil";
|
||||
import { CurrentUserState } from "features/users/state/atoms/current-user";
|
||||
|
||||
class Login extends Observable {
|
||||
|
||||
@@ -39,6 +37,7 @@ class Login extends Observable {
|
||||
error_code: any;
|
||||
cookies: Cookies;
|
||||
|
||||
|
||||
// eslint-disable-next-line @typescript-eslint/no-unused-vars
|
||||
recoilUpdateUser = (user: UserType | undefined) => {};
|
||||
|
||||
@@ -224,18 +223,6 @@ class Login extends Observable {
|
||||
this.userIsSet = new Promise(resolve => (this.resolveUser = resolve));
|
||||
}
|
||||
|
||||
getIsPublicAccess() {
|
||||
let publicAccess = false;
|
||||
const viewParameter = WindowState.findGetParameter('view') || '';
|
||||
if (
|
||||
(viewParameter && ['drive_publicAccess'].indexOf(viewParameter) >= 0) ||
|
||||
Globals.store_public_access_get_data
|
||||
) {
|
||||
publicAccess = true;
|
||||
Globals.store_public_access_get_data = WindowState.allGetParameter();
|
||||
}
|
||||
return publicAccess;
|
||||
}
|
||||
}
|
||||
|
||||
export default new Login();
|
||||
|
||||
@@ -111,22 +111,10 @@ export class DriveApiClient {
|
||||
);
|
||||
}
|
||||
|
||||
static async getDownloadToken(companyId: string, ids: string[], versionId?: string) {
|
||||
return Api.get<{ token: string }>(
|
||||
`/internal/services/documents/v1/companies/${companyId}/item/download/token` +
|
||||
`?items=${ids.join(',')}&version_id=${versionId}` +
|
||||
appendTdriveToken(true),
|
||||
);
|
||||
}
|
||||
|
||||
static async getDownloadUrl(companyId: string, id: string, versionId?: string) {
|
||||
static getDownloadUrl(companyId: string, id: string, versionId?: string) {
|
||||
if (versionId)
|
||||
return Api.route(
|
||||
`/internal/services/files/v1/companies/${companyId}/files/${id}/download?version_id=${versionId}`,
|
||||
);
|
||||
return Api.route(
|
||||
`/internal/services/files/v1/companies/${companyId}/files/${id}/download`,
|
||||
);
|
||||
return Api.route(`/internal/services/documents/v1/companies/${companyId}/item/${id}/download?version_id=${versionId}`);
|
||||
return Api.route(`/internal/services/documents/v1/companies/${companyId}/item/${id}/download`);
|
||||
}
|
||||
|
||||
static async getDownloadZipUrl(companyId: string, ids: string[]) {
|
||||
|
||||
@@ -21,27 +21,6 @@ export const useUpload = () => {
|
||||
if (companyId) FileUploadService.deleteOneFile({ companyId, fileId: id });
|
||||
};
|
||||
|
||||
const downloadOneFile = ({
|
||||
companyId,
|
||||
fileId,
|
||||
blob,
|
||||
}: {
|
||||
companyId: string;
|
||||
fileId: string;
|
||||
blob?: boolean;
|
||||
}) => {
|
||||
if (blob) {
|
||||
return FileUploadService.download({ companyId, fileId });
|
||||
}
|
||||
|
||||
const url = FileUploadService.getDownloadRoute({
|
||||
companyId,
|
||||
fileId,
|
||||
});
|
||||
|
||||
url && (window.location.href = url);
|
||||
};
|
||||
|
||||
const retryUpload = (id: string) => FileUploadService.retry(id);
|
||||
|
||||
return {
|
||||
@@ -51,7 +30,6 @@ export const useUpload = () => {
|
||||
getOnePendingFile,
|
||||
currentTask,
|
||||
deleteOneFile,
|
||||
downloadOneFile,
|
||||
retryUpload,
|
||||
};
|
||||
};
|
||||
|
||||
@@ -31,12 +31,10 @@ class Requests {
|
||||
.then(response => {
|
||||
if (options.withBlob) {
|
||||
response.blob().then(blob => {
|
||||
this.retrieveJWTToken(JSON.stringify(blob));
|
||||
callback && callback(blob);
|
||||
});
|
||||
} else {
|
||||
response.text().then(text => {
|
||||
if (text) this.retrieveJWTToken(text);
|
||||
callback && callback(text);
|
||||
});
|
||||
}
|
||||
@@ -54,17 +52,6 @@ class Requests {
|
||||
this.request(type, route, data, callback, options);
|
||||
});
|
||||
}
|
||||
|
||||
retrieveJWTToken(rawBody: string) {
|
||||
try {
|
||||
const body = JSON.parse(rawBody);
|
||||
if (body.access_token) {
|
||||
JWTStorage.updateJWT(body.access_token);
|
||||
}
|
||||
} catch (err) {
|
||||
console.error('Error while reading jwt tokens from: ' + rawBody, err);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
export default new Requests();
|
||||
|
||||
Reference in New Issue
Block a user