This commit is contained in:
NotII
2025-03-03 21:13:07 +00:00
parent 5260978cb8
commit f5382d82f6
4 changed files with 125 additions and 18 deletions

View File

@@ -11,6 +11,7 @@ import { formatDistanceToNow } from "date-fns";
import axios from "axios";
import { toast } from "sonner";
import { ArrowLeft, Send, RefreshCw } from "lucide-react";
import { getCookie } from "@/lib/client-utils";
interface Message {
_id: string;
@@ -44,7 +45,24 @@ export default function ChatDetail({ chatId }: { chatId: string }) {
// Fetch chat data
const fetchChat = async () => {
try {
const response = await axios.get(`/api/chats/${chatId}`);
// Get auth token from cookies
const authToken = getCookie("Authorization");
if (!authToken) {
toast.error("You need to be logged in");
router.push("/auth/login");
return;
}
// Set up axios with the auth token
const authAxios = axios.create({
baseURL: process.env.NEXT_PUBLIC_API_URL,
headers: {
Authorization: `Bearer ${authToken}`
}
});
const response = await authAxios.get(`/chats/${chatId}`);
setChat(response.data);
setLoading(false);
} catch (error) {
@@ -76,7 +94,24 @@ export default function ChatDetail({ chatId }: { chatId: string }) {
setSending(true);
try {
await axios.post(`/api/chats/${chatId}/message`, {
// Get auth token from cookies
const authToken = getCookie("Authorization");
if (!authToken) {
toast.error("You need to be logged in");
router.push("/auth/login");
return;
}
// Set up axios with the auth token
const authAxios = axios.create({
baseURL: process.env.NEXT_PUBLIC_API_URL,
headers: {
Authorization: `Bearer ${authToken}`
}
});
await authAxios.post(`/chats/${chatId}/message`, {
content: message
});