auth flow

This commit is contained in:
arc-alex
2023-11-18 17:53:08 +01:00
parent 31b46bbb34
commit 6b2b6dc6dd
4 changed files with 41 additions and 11 deletions
+12 -5
View File
@@ -232,12 +232,9 @@ export async function postNew(initialData: Partial<ProjectData>) {
* @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);
};
};
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;
};
@@ -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<HTMLInputElement>(null);
const [file, setFile] = useState<File | null>(null);
const [authState, setAuthState] = useState<boolean>(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 (
<Modal
@@ -69,8 +83,8 @@ export default function SheetsModal(props: SheetsModalProps) {
<div>
<Button onClick={handleClick}>Upload Client Secrect</Button>
</div>
{file && <div>We got a file</div>}
<div>You are authenticated</div>
{authState && <div>You are authenticated</div>}
{!authState && <div>You are not authenticated</div>}
<div>
<Button variant='ontime-filled' padding='0 2em' onClick={handlePullData}>
Pull data
+8
View File
@@ -106,3 +106,11 @@ router.post('/new', projectSanitiser, postNew);
// create route between controller and '/ontime/sheet-client' endpoint
router.post('/sheet-clientsecrect', uploadFile, sheetClientFile);
// create route between controller and '/ontime/sheet-authstatus' endpoint
router.get('/sheet-authstatus', sheetAuthState);
// create route between controller and '/ontime/sheet-authstatus' endpoint
router.get('/sheet-authurl', sheetAuthUrl);
+1
View File
@@ -143,6 +143,7 @@ class sheet {
*/
public async openAuthServer(): Promise<string | false> {
//TODO: this only works on local networks
//FIXME: the authUrl is not returnd before the second call
if (sheet.authUrl) {
return sheet.authUrl;
}