↗ added redirect on deletion of logged in api key

This commit is contained in:
Chirag Bhalotia 2023-06-24 11:58:26 +05:30
parent 5afe2907f1
commit 7a69008bbe
No known key found for this signature in database
GPG Key ID: F7F1F1FBFFD40427
1 changed files with 12 additions and 2 deletions

View File

@ -2,6 +2,8 @@
import api from '@/api';
import Button from '@/components/ui/Button';
import Cookies from 'js-cookie';
import { useRouter } from 'next/navigation';
import React, { useState } from 'react';
import { toast } from 'react-hot-toast';
@ -11,9 +13,17 @@ interface ApiKeyListProps {
function ApiKeyList({ data }: ApiKeyListProps) {
const [apiKeys, setApiKeys] = useState(data);
const onDisableApiKey = async (id: string) => {
const router = useRouter();
const onDisableApiKey = async (id: string, key: string) => {
try {
const apiKey = Cookies.get().apiKey as string;
await api.apiKeys.disableApiKey(id);
if (apiKey.endsWith(key.substring(9))) {
Cookies.remove('apiKey');
Cookies.remove('masterKey');
Cookies.remove('instanceUrl');
router.replace('/');
}
setApiKeys(old => old.filter(key => key.keyID !== id));
} catch (err) {
console.error('Error Deleting api key');
@ -49,7 +59,7 @@ function ApiKeyList({ data }: ApiKeyListProps) {
aria-label="Disable Api Key"
title="Disable Api Key"
className="rounded-full p-2 bg-red-100 text-red-600"
onClick={() => onDisableApiKey(keyID)}
onClick={() => onDisableApiKey(keyID, key)}
>
Disable
</Button>