From 6b2b6dc6dde4db980b5bd1360493061b95e922a0 Mon Sep 17 00:00:00 2001 From: arc-alex Date: Sat, 18 Nov 2023 17:53:08 +0100 Subject: [PATCH] auth flow --- apps/client/src/common/api/ontimeApi.ts | 17 ++++++++---- .../modals/sheets-modal/SheetsModal.tsx | 26 ++++++++++++++----- apps/server/src/routes/ontimeRouter.ts | 8 ++++++ apps/server/src/utils/sheetsAuth.ts | 1 + 4 files changed, 41 insertions(+), 11 deletions(-) diff --git a/apps/client/src/common/api/ontimeApi.ts b/apps/client/src/common/api/ontimeApi.ts index d73de7448..df375519d 100644 --- a/apps/client/src/common/api/ontimeApi.ts +++ b/apps/client/src/common/api/ontimeApi.ts @@ -232,12 +232,9 @@ export async function postNew(initialData: Partial) { * @description sheet Client File * @return {Promise} */ -export const uploadSheetClientFile = async ( - file: File -) => { +export const uploadSheetClientFile = async (file: File) => { const formData = new FormData(); formData.append('userFile', file); - await axios .post(`${ontimeURL}/sheet-clientsecrect`, formData, { headers: { @@ -248,4 +245,14 @@ export const uploadSheetClientFile = async ( }, }) .then((response) => response.data.id); -}; \ No newline at end of file +}; + +export const getSheetsAuthStatus = async () => { + const res = await axios.get(`${ontimeURL}/sheet-authstatus`); + return res.data; +}; + +export const getSheetsAuthUrl = async () => { + const res = await axios.get(`${ontimeURL}/sheet-authurl`); + return res.data; +}; diff --git a/apps/client/src/features/modals/sheets-modal/SheetsModal.tsx b/apps/client/src/features/modals/sheets-modal/SheetsModal.tsx index 2de43576a..8ae65f121 100644 --- a/apps/client/src/features/modals/sheets-modal/SheetsModal.tsx +++ b/apps/client/src/features/modals/sheets-modal/SheetsModal.tsx @@ -10,7 +10,7 @@ import { ModalHeader, ModalOverlay, } from '@chakra-ui/react'; -import { uploadSheetClientFile } from '../../../common/api/ontimeApi'; +import { getSheetsAuthStatus, getSheetsAuthUrl, uploadSheetClientFile } from '../../../common/api/ontimeApi'; interface SheetsModalProps { onClose: () => void; @@ -21,6 +21,7 @@ export default function SheetsModal(props: SheetsModalProps) { const { isOpen, onClose } = props; const fileInputRef = useRef(null); const [file, setFile] = useState(null); + const [authState, setAuthState] = useState(true); const handleClose = () => onClose(); const handleClick = () => { @@ -33,13 +34,26 @@ export default function SheetsModal(props: SheetsModalProps) { setFile(null); return; } else { - uploadSheetClientFile(selectedFile) + uploadSheetClientFile(selectedFile); } setFile(selectedFile); }; - const handleAuthenticate = () => {} - const handlePullData = () => {} + //TODO: do smoething better here + getSheetsAuthStatus().then((data) => { + setAuthState(data); + }); + + const handleAuthenticate = () => { + getSheetsAuthUrl().then((data) => { + console.log(data); + if (data == 'bad') { + } else { + window.open(data, '_blank', 'noreferrer'); + } + }); + }; + const handlePullData = () => {}; return ( - {file &&
We got a file
} -
You are authenticated
+ {authState &&
You are authenticated
} + {!authState &&
You are not authenticated
}