2024-10-03 02:47:29 +05:30
|
|
|
import React, { useState, useEffect } from 'react';
|
|
|
|
|
import {
|
|
|
|
|
Box,
|
|
|
|
|
Button,
|
|
|
|
|
Typography,
|
|
|
|
|
IconButton,
|
2024-10-03 04:52:29 +05:30
|
|
|
CircularProgress,
|
2024-10-03 05:06:11 +05:30
|
|
|
Table,
|
|
|
|
|
TableBody,
|
|
|
|
|
TableCell,
|
|
|
|
|
TableContainer,
|
|
|
|
|
TableHead,
|
|
|
|
|
TableRow,
|
|
|
|
|
Tooltip,
|
2024-10-03 02:47:29 +05:30
|
|
|
} from '@mui/material';
|
2024-10-03 05:06:11 +05:30
|
|
|
import { ContentCopy, Visibility, Delete } 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 03:29:38 +05:30
|
|
|
const ApiKeyManager = () => {
|
2024-10-03 02:47:29 +05:30
|
|
|
const [apiKey, setApiKey] = useState<string | null>(null);
|
2024-10-03 05:06:11 +05:30
|
|
|
const [apiKeyName, setApiKeyName] = useState<string>('Maxun API Key');
|
2024-10-03 02:47:29 +05:30
|
|
|
const [loading, setLoading] = useState<boolean>(true);
|
2024-10-03 05:06:11 +05:30
|
|
|
const [showKey, setShowKey] = useState<boolean>(false);
|
2024-10-03 02:47:29 +05:30
|
|
|
const [copySuccess, setCopySuccess] = useState<boolean>(false);
|
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);
|
|
|
|
|
}
|
|
|
|
|
};
|
|
|
|
|
|
2024-10-03 05:06:11 +05:30
|
|
|
const deleteApiKey = async () => {
|
|
|
|
|
setLoading(true);
|
|
|
|
|
try {
|
|
|
|
|
await axios.delete('http://localhost:8080/auth/delete-api-key');
|
|
|
|
|
setApiKey(null);
|
|
|
|
|
notify('success', 'API Key deleted successfully');
|
|
|
|
|
} catch (error) {
|
|
|
|
|
console.error('Error deleting API key', error);
|
|
|
|
|
} finally {
|
|
|
|
|
setLoading(false);
|
|
|
|
|
}
|
|
|
|
|
};
|
|
|
|
|
|
2024-10-03 02:47:29 +05:30
|
|
|
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 05:06:11 +05:30
|
|
|
<TableContainer>
|
|
|
|
|
<Table>
|
|
|
|
|
<TableHead>
|
|
|
|
|
<TableRow>
|
|
|
|
|
<TableCell>API Key Name</TableCell>
|
|
|
|
|
<TableCell>API Key</TableCell>
|
|
|
|
|
<TableCell>Actions</TableCell>
|
|
|
|
|
</TableRow>
|
|
|
|
|
</TableHead>
|
|
|
|
|
<TableBody>
|
|
|
|
|
<TableRow>
|
|
|
|
|
<TableCell>{apiKeyName}</TableCell>
|
|
|
|
|
<TableCell>{showKey ? apiKey : '****************'}</TableCell>
|
|
|
|
|
<TableCell>
|
|
|
|
|
<Tooltip title="Copy API Key">
|
|
|
|
|
<IconButton onClick={copyToClipboard}>
|
|
|
|
|
<ContentCopy />
|
|
|
|
|
</IconButton>
|
|
|
|
|
</Tooltip>
|
|
|
|
|
<Tooltip title={showKey ? 'Hide API Key' : 'Show API Key'}>
|
|
|
|
|
<IconButton onClick={() => setShowKey(!showKey)}>
|
|
|
|
|
<Visibility />
|
|
|
|
|
</IconButton>
|
|
|
|
|
</Tooltip>
|
|
|
|
|
<Tooltip title="Delete API Key">
|
|
|
|
|
<IconButton onClick={deleteApiKey} color="error">
|
|
|
|
|
<Delete />
|
|
|
|
|
</IconButton>
|
|
|
|
|
</Tooltip>
|
|
|
|
|
</TableCell>
|
|
|
|
|
</TableRow>
|
|
|
|
|
</TableBody>
|
|
|
|
|
</Table>
|
2024-10-03 04:15:57 +05:30
|
|
|
{copySuccess && (
|
2024-10-03 05:06:11 +05:30
|
|
|
<Typography variant="caption" color="primary">
|
|
|
|
|
Copied to Clipboard
|
|
|
|
|
</Typography>
|
2024-10-03 04:15:57 +05:30
|
|
|
)}
|
2024-10-03 05:06:11 +05:30
|
|
|
</TableContainer>
|
2024-10-03 02:47:29 +05:30
|
|
|
) : (
|
2024-10-03 05:06:11 +05:30
|
|
|
<>
|
2024-10-03 04:53:28 +05:30
|
|
|
<Typography>You haven't generated an API key yet.</Typography>
|
2024-10-03 05:06:11 +05:30
|
|
|
<Button onClick={generateApiKey} variant="contained" color="primary">
|
2024-10-03 02:47:29 +05:30
|
|
|
Generate API Key
|
|
|
|
|
</Button>
|
2024-10-03 05:06:11 +05:30
|
|
|
</>
|
2024-10-03 02:47:29 +05:30
|
|
|
)}
|
|
|
|
|
</Container>
|
|
|
|
|
);
|
|
|
|
|
};
|
2024-10-03 01:56:52 +05:30
|
|
|
|
2024-10-03 05:06:31 +05:30
|
|
|
export default ApiKeyManager;
|