diff --git a/src/app/create-post/page.tsx b/src/app/create-post/page.tsx index 1c54262..d943024 100644 --- a/src/app/create-post/page.tsx +++ b/src/app/create-post/page.tsx @@ -1,6 +1,6 @@ "use client"; -import { getCookies, hasCookie } from "@/helpers/cookie"; +import { getCookie, hasCookie } from "@/helpers/cookie"; import { Button, Form, Input, Textarea } from "@nextui-org/react"; import { redirect } from "next/navigation"; import { useState } from "react"; @@ -41,7 +41,7 @@ export default function CreatePostPage() { return; } - if (!hasCookie()) { + if (!hasCookie("token")) { setErrors({ content: "You are not logged in" }); return; } @@ -54,12 +54,12 @@ export default function CreatePostPage() { body: JSON.stringify({ title: title, content: content, - username: getCookies().user, + username: getCookie("user"), }), method: "POST", headers: { "Content-Type": "application/json", - authorization: `Bearer ${getCookies().token}`, + authorization: `Bearer ${getCookie("token")}`, }, } ); diff --git a/src/components/navbar/index.tsx b/src/components/navbar/index.tsx index 1609b54..3593e08 100644 --- a/src/components/navbar/index.tsx +++ b/src/components/navbar/index.tsx @@ -22,7 +22,7 @@ import { import { SiDiscord, SiForgejo, SiGithub } from "@icons-pack/react-simple-icons"; import { LogInIcon, NotebookPen, SquarePen } from "lucide-react"; import { useEffect, useState } from "react"; -import { hasCookie, getCookies } from "@/helpers/cookie"; +import { hasCookie, getCookie } from "@/helpers/cookie"; import { usePathname } from "next/navigation"; import { UserType } from "@/types/UserType"; @@ -33,17 +33,17 @@ export default function Navbar() { useEffect(() => { loadUser(); async function loadUser() { - if (!hasCookie()) { + if (!hasCookie("token")) { setUser(undefined); return; } const response = await fetch( process.env.NEXT_PUBLIC_MODE === "PROD" - ? `https://d2jam.com/api/v1/self?username=${getCookies().user}` - : `http://localhost:3005/api/v1/self?username=${getCookies().user}`, + ? `https://d2jam.com/api/v1/self?username=${getCookie("user")}` + : `http://localhost:3005/api/v1/self?username=${getCookie("user")}`, { - headers: { authorization: `Bearer ${getCookies().token}` }, + headers: { authorization: `Bearer ${getCookie("token")}` }, } ); diff --git a/src/helpers/cookie.ts b/src/helpers/cookie.ts index 30903ad..f255031 100644 --- a/src/helpers/cookie.ts +++ b/src/helpers/cookie.ts @@ -8,11 +8,22 @@ export function getCookies() { return cookies; } -export function hasCookie() { +export function getCookie(cookie: string) { const pairs = document.cookie.split(";"); for (let i = 0; i < pairs.length; i++) { - const pair = pairs[i].split("="); - if (pair[0] == "token") { + const pair = pairs[i].trim().split("="); + if (pair[0] == cookie) { + return pair[1]; + } + } + return null; +} + +export function hasCookie(cookie: string) { + const pairs = document.cookie.split(";"); + for (let i = 0; i < pairs.length; i++) { + const pair = pairs[i].trim().split("="); + if (pair[0] == cookie) { return true; } }