🛠 Fix JWT loading for shared view (#76)

* Fix JWT loading
* Use a different local storage for shared view
This commit is contained in:
Romaric Mourgues
2023-06-12 11:00:20 +02:00
committed by GitHub
parent 723b77cb55
commit b7b9abbc97
4 changed files with 29 additions and 13 deletions
@@ -1,6 +1,11 @@
export default class LocalStorage {
static prefix = 'tdrive:';
//For shared views we will use another prefix
static setPrefix(prefix: string) {
LocalStorage.prefix = prefix;
}
static setItem(key: string, value: unknown) {
window.localStorage.setItem(`${LocalStorage.prefix}${key}`, JSON.stringify(value));
}
@@ -17,9 +17,6 @@ export default ({
context?: EmbedContext;
inPublicSharing?: boolean;
}) => {
//Preload applications mainly for shared view
useCompanyApplications();
return (
<>
<SelectorModal />
@@ -1,3 +1,9 @@
import Avatar from '@atoms/avatar';
import A from '@atoms/link';
import { Modal, ModalContent } from '@atoms/modal';
import { Base } from '@atoms/text';
import { useCompanyApplications } from '@features/applications/hooks/use-company-applications';
import { Application } from '@features/applications/types/application';
import { Transition } from '@headlessui/react';
import {
ChevronLeftIcon,
@@ -6,12 +12,6 @@ import {
FolderDownloadIcon,
LinkIcon,
} from '@heroicons/react/outline';
import Avatar from '@atoms/avatar';
import A from '@atoms/link';
import { Modal, ModalContent } from '@atoms/modal';
import { Base } from '@atoms/text';
import { useCompanyApplications } from '@features/applications/hooks/use-company-applications';
import { Application } from '@features/applications/types/application';
import { ReactNode } from 'react';
import { atom, useRecoilState } from 'recoil';
import { slideXTransition, slideXTransitionReverted } from 'src/utils/transitions';
@@ -22,12 +22,18 @@ import {
} from '../../../../features/drive/api-client/api-client';
import useRouterCompany from '../../../../features/router/hooks/use-router-company';
import { CreateModalWithUploadZones } from '../../side-bar/actions';
import { useRecoilState } from 'recoil';
import { DriveCurrentFolderAtom } from './browser';
import { useCompanyApplications } from 'app/features/applications/hooks/use-company-applications';
import LocalStorage from 'app/features/global/framework/local-storage-service';
export default () => {
const companyId = useRouterCompany();
//Create a different local storage for shared view
useEffect(() => {
LocalStorage.setPrefix('tdrive-shared:');
LocalStorage.clear();
}, []);
const [state, setState] = useState({ group: { logo: '', name: '' } });
useEffect(() => {
const routeState = RouterService.getStateFromRoute();
@@ -97,6 +103,9 @@ const AccessChecker = ({
const { details, loading, refresh } = useDriveItem(folderId);
const companyId = useRouterCompany();
const [password, setPassword] = useState((token || '').split('+')[1] || '');
const [accessGranted, setAccessGranted] = useState(false);
//Preload applications mainly for shared view
const { refresh: refreshApplications } = useCompanyApplications();
const setPublicToken = async (token: string, password?: string) => {
try {
@@ -114,16 +123,21 @@ const AccessChecker = ({
}
JWTStorage.updateJWT(access_token);
//Everything alright, load the drive
setAccessGranted(true);
refresh(folderId);
refreshApplications();
} catch (e) {
console.error(e);
ToasterService.error('Unable to access documents: ' + e);
setAccessGranted(false);
}
};
useEffect(() => {
(async () => {
await setPublicToken(token || '');
refresh(folderId);
})();
}, []);
@@ -131,7 +145,7 @@ const AccessChecker = ({
return <></>;
}
if (!details?.item?.id && !loading) {
if ((!details?.item?.id && !loading) || !accessGranted) {
return (
<div className="text-center">
<div style={{ height: '20vh' }} />