2024-10-03 02:47:29 +05:30
|
|
|
import React, { useState, useEffect } from 'react';
|
|
|
|
|
import {
|
|
|
|
|
Box,
|
|
|
|
|
Button,
|
2024-10-03 04:52:29 +05:30
|
|
|
TextField,
|
2024-10-03 02:47:29 +05:30
|
|
|
Typography,
|
|
|
|
|
IconButton,
|
2024-10-03 04:52:29 +05:30
|
|
|
InputAdornment,
|
2024-10-03 04:15:57 +05:30
|
|
|
Tooltip,
|
2024-10-03 04:52:29 +05:30
|
|
|
CircularProgress,
|
2024-10-03 02:47:29 +05:30
|
|
|
} from '@mui/material';
|
2024-10-03 04:52:29 +05:30
|
|
|
import { ContentCopy } from '@mui/icons-material';
|
2024-10-03 02:47:29 +05:30
|
|
|
import styled from 'styled-components';
|
|
|
|
|
import axios from 'axios';
|
|
|
|
|
import { useGlobalInfoStore } from '../../context/globalInfo';
|
|
|
|
|
|
|
|
|
|
const Container = styled(Box)`
|
|
|
|
|
display: flex;
|
|
|
|
|
flex-direction: column;
|
|
|
|
|
align-items: center;
|
|
|
|
|
margin-top: 50px;
|
|
|
|
|
`;
|
|
|
|
|
|
2024-10-03 04:52:29 +05:30
|
|
|
const ApiKeyField = styled(TextField)`
|
|
|
|
|
width: 400px;
|
|
|
|
|
margin: 20px 0;
|
|
|
|
|
`;
|
|
|
|
|
|
|
|
|
|
const HiddenText = styled(Typography)`
|
|
|
|
|
color: #888;
|
|
|
|
|
font-style: italic;
|
|
|
|
|
`;
|
|
|
|
|
|
|
|
|
|
const CenteredContent = styled(Box)`
|
|
|
|
|
display: flex;
|
|
|
|
|
flex-direction: column;
|
|
|
|
|
align-items: center;
|
|
|
|
|
justify-content: center;
|
|
|
|
|
margin-top: 50px; /* Add top margin for spacing */
|
|
|
|
|
`;
|
|
|
|
|
|
2024-10-03 03:29:38 +05:30
|
|
|
const ApiKeyManager = () => {
|
2024-10-03 02:47:29 +05:30
|
|
|
const [apiKey, setApiKey] = useState<string | null>(null);
|
|
|
|
|
const [loading, setLoading] = useState<boolean>(true);
|
|
|
|
|
const [copySuccess, setCopySuccess] = useState<boolean>(false);
|
2024-10-03 04:52:29 +05:30
|
|
|
|
2024-10-03 02:51:08 +05:30
|
|
|
const { notify } = useGlobalInfoStore();
|
2024-10-03 02:47:29 +05:30
|
|
|
|
|
|
|
|
useEffect(() => {
|
|
|
|
|
const fetchApiKey = async () => {
|
|
|
|
|
try {
|
|
|
|
|
const { data } = await axios.get('http://localhost:8080/auth/api-key');
|
|
|
|
|
setApiKey(data.api_key);
|
|
|
|
|
} catch (error) {
|
|
|
|
|
console.error('Error fetching API key', error);
|
|
|
|
|
} finally {
|
|
|
|
|
setLoading(false);
|
|
|
|
|
}
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
fetchApiKey();
|
|
|
|
|
}, []);
|
|
|
|
|
|
|
|
|
|
const generateApiKey = async () => {
|
|
|
|
|
setLoading(true);
|
|
|
|
|
try {
|
|
|
|
|
const { data } = await axios.post('http://localhost:8080/auth/generate-api-key');
|
|
|
|
|
setApiKey(data.api_key);
|
2024-10-03 03:29:38 +05:30
|
|
|
notify('info', `Generated API Key: ${data.api_key}`);
|
2024-10-03 02:47:29 +05:30
|
|
|
} catch (error) {
|
|
|
|
|
console.error('Error generating API key', error);
|
|
|
|
|
} finally {
|
|
|
|
|
setLoading(false);
|
|
|
|
|
}
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
const copyToClipboard = () => {
|
|
|
|
|
if (apiKey) {
|
|
|
|
|
navigator.clipboard.writeText(apiKey);
|
|
|
|
|
setCopySuccess(true);
|
|
|
|
|
setTimeout(() => setCopySuccess(false), 2000);
|
|
|
|
|
}
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
if (loading) return <CircularProgress />;
|
|
|
|
|
|
2024-10-03 01:56:52 +05:30
|
|
|
return (
|
2024-10-03 02:47:29 +05:30
|
|
|
<Container>
|
|
|
|
|
<Typography variant="h5">Manage Your API Key</Typography>
|
|
|
|
|
|
|
|
|
|
{apiKey ? (
|
2024-10-03 04:52:29 +05:30
|
|
|
<>
|
|
|
|
|
<ApiKeyField
|
|
|
|
|
label="Your API Key"
|
|
|
|
|
variant="outlined"
|
|
|
|
|
value="**** **** **** ****"
|
|
|
|
|
InputProps={{
|
|
|
|
|
readOnly: true,
|
|
|
|
|
endAdornment: (
|
|
|
|
|
<InputAdornment position="end">
|
|
|
|
|
<IconButton onClick={copyToClipboard} edge="end">
|
|
|
|
|
<ContentCopy />
|
|
|
|
|
</IconButton>
|
|
|
|
|
</InputAdornment>
|
|
|
|
|
),
|
|
|
|
|
}}
|
|
|
|
|
/>
|
|
|
|
|
|
2024-10-03 04:15:57 +05:30
|
|
|
{copySuccess && (
|
2024-10-03 04:52:29 +05:30
|
|
|
<Tooltip title="Copied!" open={copySuccess} placement="right">
|
|
|
|
|
<Typography variant="caption" color="primary">
|
|
|
|
|
Copied to Clipboard
|
|
|
|
|
</Typography>
|
|
|
|
|
</Tooltip>
|
2024-10-03 04:15:57 +05:30
|
|
|
)}
|
2024-10-03 04:52:29 +05:30
|
|
|
</>
|
2024-10-03 02:47:29 +05:30
|
|
|
) : (
|
2024-10-03 04:52:29 +05:30
|
|
|
<CenteredContent>
|
|
|
|
|
<HiddenText>You haven't generated an API key yet.</HiddenText>
|
|
|
|
|
<Button
|
|
|
|
|
onClick={generateApiKey}
|
|
|
|
|
variant="contained"
|
|
|
|
|
color="primary"
|
|
|
|
|
style={{ marginTop: '20px' }}
|
|
|
|
|
>
|
2024-10-03 02:47:29 +05:30
|
|
|
Generate API Key
|
|
|
|
|
</Button>
|
2024-10-03 04:52:29 +05:30
|
|
|
</CenteredContent>
|
2024-10-03 02:47:29 +05:30
|
|
|
)}
|
|
|
|
|
</Container>
|
|
|
|
|
);
|
|
|
|
|
};
|
2024-10-03 01:56:52 +05:30
|
|
|
|
2024-10-03 03:29:38 +05:30
|
|
|
export default ApiKeyManager;
|
2024-10-03 04:52:29 +05:30
|
|
|
|