mirror of
https://github.com/Sonny93/my-links.git
synced 2025-12-08 22:53:25 +00:00
refactor: use adonis's access tokens instead of creating custom (and unsecured) logic
This commit is contained in:
@@ -1,25 +1,33 @@
|
||||
import { usePage } from '@inertiajs/react';
|
||||
import {
|
||||
ActionIcon,
|
||||
Badge,
|
||||
Button,
|
||||
Card,
|
||||
CopyButton,
|
||||
Group,
|
||||
Stack,
|
||||
Text,
|
||||
Tooltip,
|
||||
} from '@mantine/core';
|
||||
import { modals } from '@mantine/modals';
|
||||
import { DateTime } from 'luxon';
|
||||
import { useTranslation } from 'react-i18next';
|
||||
import { TbCheck, TbCopy, TbPlus, TbTrash } from 'react-icons/tb';
|
||||
import { TbPlus, TbTrash } from 'react-icons/tb';
|
||||
import { SimpleTable } from '~/components/common/simple_table/simple_table';
|
||||
import { useApiTokens } from '~/hooks/use_api_tokens';
|
||||
import { ApiToken } from '~/types/app';
|
||||
import { CreateTokenModal } from './create_token_modal';
|
||||
|
||||
const useGetCreatedToken = () => {
|
||||
const newlyCreatedToken = usePage<{
|
||||
token?: ApiToken;
|
||||
}>().props.token;
|
||||
return newlyCreatedToken;
|
||||
};
|
||||
|
||||
export function ApiTokens() {
|
||||
const { t } = useTranslation();
|
||||
const { tokens, createToken, revokeToken } = useApiTokens();
|
||||
|
||||
const newlyCreatedToken = useGetCreatedToken();
|
||||
|
||||
const handleCreateTokenModal = () => {
|
||||
modals.open({
|
||||
title: t('api-tokens.create-new'),
|
||||
@@ -32,32 +40,67 @@ export function ApiTokens() {
|
||||
});
|
||||
};
|
||||
|
||||
const handleRevokeToken = async (tokenId: number, tokenName: string) => {
|
||||
const handleRevokeToken = async (tokenId: number) => {
|
||||
const token = tokens.find((t) => t.identifier === tokenId);
|
||||
if (!token) return;
|
||||
|
||||
modals.openConfirmModal({
|
||||
title: t('api-tokens.revoke'),
|
||||
children: (
|
||||
<Text size="sm">
|
||||
{t('api-tokens.confirm-revoke')} <strong>{tokenName}</strong>?
|
||||
</Text>
|
||||
title: (
|
||||
<>
|
||||
{t('api-tokens.revoke')} "<strong>{token.name}</strong>"
|
||||
</>
|
||||
),
|
||||
children: <Text size="sm">{t('api-tokens.confirm-revoke')}</Text>,
|
||||
labels: {
|
||||
confirm: t('api-tokens.revoke'),
|
||||
cancel: t('common.cancel'),
|
||||
cancel: t('cancel'),
|
||||
},
|
||||
confirmProps: { color: 'red' },
|
||||
onConfirm: () => revokeToken(tokenId),
|
||||
});
|
||||
};
|
||||
|
||||
const formatDate = (dateString: string | null) => {
|
||||
if (!dateString) return t('api-tokens.never');
|
||||
return DateTime.fromISO(dateString).toRelative();
|
||||
};
|
||||
const generateTokenRow = (token: ApiToken) =>
|
||||
newlyCreatedToken?.identifier === token.identifier && (
|
||||
<>
|
||||
<Text c="green" size="sm">
|
||||
{t('api-tokens.new-token')}{' '}
|
||||
{newlyCreatedToken.token && (
|
||||
<CopyButton value={newlyCreatedToken.token}>
|
||||
{({ copied, copy }) => (
|
||||
<Button
|
||||
color={copied ? 'teal' : 'blue'}
|
||||
onClick={copy}
|
||||
size="xs"
|
||||
variant="light"
|
||||
>
|
||||
{copied ? t('copied') : t('copy')}
|
||||
</Button>
|
||||
)}
|
||||
</CopyButton>
|
||||
)}
|
||||
</Text>
|
||||
</>
|
||||
);
|
||||
|
||||
const isExpired = (expiresAt: string | null) => {
|
||||
if (!expiresAt) return false;
|
||||
return DateTime.fromISO(expiresAt) < DateTime.now();
|
||||
};
|
||||
const generateRow = (token: ApiToken) => ({
|
||||
key: token.identifier.toString(),
|
||||
name: token.name,
|
||||
token: generateTokenRow(token) || undefined,
|
||||
expiresAt: token.expiresAt,
|
||||
lastUsedAt: token.lastUsedAt,
|
||||
actions: [
|
||||
<ActionIcon
|
||||
color="red"
|
||||
variant="subtle"
|
||||
onClick={() => handleRevokeToken(token.identifier)}
|
||||
>
|
||||
<TbTrash size={16} />
|
||||
</ActionIcon>,
|
||||
],
|
||||
});
|
||||
|
||||
const rows = tokens.map(generateRow);
|
||||
|
||||
return (
|
||||
<Card withBorder>
|
||||
@@ -73,87 +116,13 @@ export function ApiTokens() {
|
||||
</Button>
|
||||
</Group>
|
||||
|
||||
<Stack gap="sm">
|
||||
{tokens.length === 0 ? (
|
||||
<Text c="dimmed" ta="center" py="xl">
|
||||
{t('api-tokens.no-tokens')}
|
||||
</Text>
|
||||
) : (
|
||||
tokens.map((token) => (
|
||||
<Card
|
||||
key={token.id}
|
||||
withBorder
|
||||
p="sm"
|
||||
opacity={token.isActive ? 1 : 0.5}
|
||||
>
|
||||
<Group justify="space-between" align="flex-start">
|
||||
<Stack gap="xs" style={{ flex: 1 }}>
|
||||
<Group gap="xs">
|
||||
<Text fw={500}>{token.name}</Text>
|
||||
{isExpired(token.expiresAt) && (
|
||||
<Badge color="red" variant="light" size="xs">
|
||||
{t('api-tokens.expired')}
|
||||
</Badge>
|
||||
)}
|
||||
{!token.isActive && (
|
||||
<Badge color="gray" variant="light" size="xs">
|
||||
{t('api-tokens.revoked')}
|
||||
</Badge>
|
||||
)}
|
||||
</Group>
|
||||
<Text size="xs" c="dimmed">
|
||||
{t('api-tokens.created')}: {formatDate(token.createdAt)}
|
||||
</Text>
|
||||
<Text size="xs" c="dimmed">
|
||||
{t('api-tokens.last-used')}: {formatDate(token.lastUsedAt)}
|
||||
</Text>
|
||||
{token.expiresAt && (
|
||||
<Text size="xs" c="dimmed">
|
||||
{t('api-tokens.expires')}: {formatDate(token.expiresAt)}
|
||||
</Text>
|
||||
)}
|
||||
</Stack>
|
||||
{token.isActive && (
|
||||
<Group gap="xs">
|
||||
<CopyButton value={token.token} timeout={2000}>
|
||||
{({ copied, copy }) => (
|
||||
<Tooltip
|
||||
label={
|
||||
copied
|
||||
? t('api-tokens.copied')
|
||||
: t('api-tokens.copy')
|
||||
}
|
||||
>
|
||||
<ActionIcon
|
||||
color={copied ? 'teal' : 'blue'}
|
||||
onClick={copy}
|
||||
variant="subtle"
|
||||
>
|
||||
{copied ? (
|
||||
<TbCheck size={16} />
|
||||
) : (
|
||||
<TbCopy size={16} />
|
||||
)}
|
||||
</ActionIcon>
|
||||
</Tooltip>
|
||||
)}
|
||||
</CopyButton>
|
||||
<Tooltip label={t('api-tokens.revoke')}>
|
||||
<ActionIcon
|
||||
color="red"
|
||||
variant="subtle"
|
||||
onClick={() => handleRevokeToken(token.id, token.name)}
|
||||
>
|
||||
<TbTrash size={16} />
|
||||
</ActionIcon>
|
||||
</Tooltip>
|
||||
</Group>
|
||||
)}
|
||||
</Group>
|
||||
</Card>
|
||||
))
|
||||
)}
|
||||
</Stack>
|
||||
{tokens.length === 0 && (
|
||||
<Text c="dimmed" ta="center" py="xl">
|
||||
{t('api-tokens.no-tokens')}
|
||||
</Text>
|
||||
)}
|
||||
|
||||
{tokens.length > 0 && <SimpleTable data={rows} />}
|
||||
</Card>
|
||||
);
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user