Fix cookie checking

This commit is contained in:
Ategon 2025-01-16 02:24:18 -05:00
parent 047d630b14
commit 2d2ffa7d12
3 changed files with 23 additions and 12 deletions
src
app/create-post
components/navbar
helpers

View file

@ -1,6 +1,6 @@
"use client"; "use client";
import { getCookies, hasCookie } from "@/helpers/cookie"; import { getCookie, hasCookie } from "@/helpers/cookie";
import { Button, Form, Input, Textarea } from "@nextui-org/react"; import { Button, Form, Input, Textarea } from "@nextui-org/react";
import { redirect } from "next/navigation"; import { redirect } from "next/navigation";
import { useState } from "react"; import { useState } from "react";
@ -41,7 +41,7 @@ export default function CreatePostPage() {
return; return;
} }
if (!hasCookie()) { if (!hasCookie("token")) {
setErrors({ content: "You are not logged in" }); setErrors({ content: "You are not logged in" });
return; return;
} }
@ -54,12 +54,12 @@ export default function CreatePostPage() {
body: JSON.stringify({ body: JSON.stringify({
title: title, title: title,
content: content, content: content,
username: getCookies().user, username: getCookie("user"),
}), }),
method: "POST", method: "POST",
headers: { headers: {
"Content-Type": "application/json", "Content-Type": "application/json",
authorization: `Bearer ${getCookies().token}`, authorization: `Bearer ${getCookie("token")}`,
}, },
} }
); );

View file

@ -22,7 +22,7 @@ import {
import { SiDiscord, SiForgejo, SiGithub } from "@icons-pack/react-simple-icons"; import { SiDiscord, SiForgejo, SiGithub } from "@icons-pack/react-simple-icons";
import { LogInIcon, NotebookPen, SquarePen } from "lucide-react"; import { LogInIcon, NotebookPen, SquarePen } from "lucide-react";
import { useEffect, useState } from "react"; import { useEffect, useState } from "react";
import { hasCookie, getCookies } from "@/helpers/cookie"; import { hasCookie, getCookie } from "@/helpers/cookie";
import { usePathname } from "next/navigation"; import { usePathname } from "next/navigation";
import { UserType } from "@/types/UserType"; import { UserType } from "@/types/UserType";
@ -33,17 +33,17 @@ export default function Navbar() {
useEffect(() => { useEffect(() => {
loadUser(); loadUser();
async function loadUser() { async function loadUser() {
if (!hasCookie()) { if (!hasCookie("token")) {
setUser(undefined); setUser(undefined);
return; return;
} }
const response = await fetch( const response = await fetch(
process.env.NEXT_PUBLIC_MODE === "PROD" process.env.NEXT_PUBLIC_MODE === "PROD"
? `https://d2jam.com/api/v1/self?username=${getCookies().user}` ? `https://d2jam.com/api/v1/self?username=${getCookie("user")}`
: `http://localhost:3005/api/v1/self?username=${getCookies().user}`, : `http://localhost:3005/api/v1/self?username=${getCookie("user")}`,
{ {
headers: { authorization: `Bearer ${getCookies().token}` }, headers: { authorization: `Bearer ${getCookie("token")}` },
} }
); );

View file

@ -8,11 +8,22 @@ export function getCookies() {
return cookies; return cookies;
} }
export function hasCookie() { export function getCookie(cookie: string) {
const pairs = document.cookie.split(";"); const pairs = document.cookie.split(";");
for (let i = 0; i < pairs.length; i++) { for (let i = 0; i < pairs.length; i++) {
const pair = pairs[i].split("="); const pair = pairs[i].trim().split("=");
if (pair[0] == "token") { 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; return true;
} }
} }