Add Auth for WebPage and APIs

This commit is contained in:
Raymond Zhou 2025-03-09 12:19:58 +08:00
parent e6b87f89ec
commit 5e6d0e0ee6
27 changed files with 15384 additions and 1720 deletions

View file

@ -1,4 +1,4 @@
import { Trash } from 'lucide-react';
import { TrashIcon } from 'lucide-react';
import {
Description,
Dialog,
@ -11,6 +11,8 @@ import {
import { Fragment, useState } from 'react';
import { toast } from 'sonner';
import { Chat } from '@/app/library/page';
import { useRouter } from 'next/navigation';
import { getApiUrl, del } from '@/lib/api';
const DeleteChat = ({
chatId,
@ -25,36 +27,27 @@ const DeleteChat = ({
}) => {
const [confirmationDialogOpen, setConfirmationDialogOpen] = useState(false);
const [loading, setLoading] = useState(false);
const router = useRouter();
const handleDelete = async () => {
setLoading(true);
try {
const res = await fetch(
`${process.env.NEXT_PUBLIC_API_URL}/chats/${chatId}`,
{
method: 'DELETE',
headers: {
'Content-Type': 'application/json',
},
},
);
if (res.status != 200) {
throw new Error('Failed to delete chat');
}
const newChats = chats.filter((chat) => chat.id !== chatId);
setChats(newChats);
await del(getApiUrl(`/chats/${chatId}`));
// 从列表中移除聊天
setChats(chats.filter((chat) => chat.id !== chatId));
if (redirect) {
window.location.href = '/';
router.push('/');
}
} catch (err: any) {
toast.error(err.message);
toast.success('聊天已删除');
} catch (error) {
console.error('删除聊天失败:', error);
toast.error('删除聊天失败');
} finally {
setConfirmationDialogOpen(false);
setLoading(false);
setConfirmationDialogOpen(false);
}
};
@ -66,7 +59,7 @@ const DeleteChat = ({
}}
className="bg-transparent text-red-400 hover:scale-105 transition duration-200"
>
<Trash size={17} />
<TrashIcon size={17} />
</button>
<Transition appear show={confirmationDialogOpen} as={Fragment}>
<Dialog