diff --git a/app/Authentication/Login/page.tsx b/app/Admin/Login/page.tsx similarity index 50% rename from app/Authentication/Login/page.tsx rename to app/Admin/Login/page.tsx index 89264a1..c176301 100644 --- a/app/Authentication/Login/page.tsx +++ b/app/Admin/Login/page.tsx @@ -2,16 +2,12 @@ import { useState } from "react"; import { login, signup } from "@/lib/server_functions/userAuth"; -import Form from "@/components/ui/Form"; export default function Login() { const [createAccount, setCreateAccount] = useState(false); return ( -
-
+
+ - - + + +
); } diff --git a/app/Admin/layout.tsx b/app/Admin/layout.tsx index a59f214..696a445 100644 --- a/app/Admin/layout.tsx +++ b/app/Admin/layout.tsx @@ -1,13 +1,11 @@ import React from "react"; -import ProtectedRoute from "@/components/wrappers/ProtectedRoute"; - -export default async function AdminLayout({ - children, -}: { - children: React.ReactNode; -}) { +export default function AdminLayout({children}: {children: React.ReactNode}) { // TODO: Put user state management here //redirect("wherever state says") <= stateRouteManager() - return {children}; -} + return ( +
+ {children} +
+ ) + } \ No newline at end of file diff --git a/app/Admin/page.tsx b/app/Admin/page.tsx index 05169a4..c19c6e1 100644 --- a/app/Admin/page.tsx +++ b/app/Admin/page.tsx @@ -1,5 +1,6 @@ -import UnderConstruction from "@/components/static_pages/UnderConstruction"; +import { redirect } from "next/navigation"; export default function Admin() { - return ; + // TODO: Replace with Layout State Manager + redirect("/Admin/Login"); } diff --git a/app/api/users/login/route.ts b/app/api/users/login/route.ts index 99226cf..4270a0b 100644 --- a/app/api/users/login/route.ts +++ b/app/api/users/login/route.ts @@ -1,12 +1,7 @@ const BACKEND_URL = process.env.BACKEND_URL; export async function POST(request: Request) { - const response = await fetch(`${BACKEND_URL}/users/auth`, { - method: "POST", - headers: request.headers, - body: request.body, - duplex: "half", - }); + const response = await fetch(`${BACKEND_URL}/users/auth`, request); const data = await response.json(); diff --git a/app/api/users/route.ts b/app/api/users/route.ts index 399e135..b961291 100644 --- a/app/api/users/route.ts +++ b/app/api/users/route.ts @@ -15,12 +15,7 @@ export async function GET() { } export async function POST(request: Request) { - const response = await fetch(`${BACKEND_URL}/users`, { - method: "POST", - headers: request.headers, - body: request.body, - duplex: "half", - }); + const response = await fetch(`${BACKEND_URL}/users`, request); const data = await response.json(); diff --git a/app/api/users/user/route.ts b/app/api/users/user/route.ts index 6969a22..9f0e902 100644 --- a/app/api/users/user/route.ts +++ b/app/api/users/user/route.ts @@ -1,13 +1,7 @@ const BACKEND_URL = process.env.BACKEND_URL; -export async function GET(request: Request) { - const authorization = request.headers.get("authorization"); - - const response = await fetch(`${BACKEND_URL}/users/user`, { - headers: { - Authorization: authorization ?? "", - }, - }); +export async function GET() { + const response = await fetch(`${BACKEND_URL}/users/user`); if (!response.ok) { return new Response("Request failed in backend", { diff --git a/components/ui/Form.tsx b/components/ui/Form.tsx deleted file mode 100644 index 7f0e323..0000000 --- a/components/ui/Form.tsx +++ /dev/null @@ -1,25 +0,0 @@ -import React from "react"; - -import RoundedContainer from "./RoundedContainer"; - -export default function Form({ - action, - children, - submitText, -}: { - // eslint-disable-next-line no-unused-vars - action?: string | ((formData: FormData) => void | Promise); - children: React.ReactNode; - submitText: string; -}) { - return ( -
-
- - {children} - - -
-
- ); -} diff --git a/components/wrappers/ProtectedRoute.tsx b/components/wrappers/ProtectedRoute.tsx deleted file mode 100644 index 2c75bbc..0000000 --- a/components/wrappers/ProtectedRoute.tsx +++ /dev/null @@ -1,17 +0,0 @@ -import React from "react"; -import { redirect } from "next/navigation"; - -import { verifySession } from "@/lib/server_functions/dataAccessLayer"; - -export default async function ProtectedRoute({ - children, -}: { - children: React.ReactNode; -}) { - const session = await verifySession(); - if (!session.isAuthed) { - redirect("/Authentication/Login"); - } - - return
{children}
; -} diff --git a/lib/api/cmFetch.ts b/lib/api/cmFetch.ts index 79446c4..544b23d 100644 --- a/lib/api/cmFetch.ts +++ b/lib/api/cmFetch.ts @@ -14,18 +14,3 @@ export async function cmFetch( return response.json(); } - -export async function cmFetchRaw( - bffUrl: string | URL, - options?: RequestInit, - ignoreStatuses?: number[], -): Promise { - const response = await fetch(`${FRONTEND_URL}/${bffUrl}`, options); - - if (!response.ok && !ignoreStatuses?.includes(response.status)) { - throw new Error("Request failed"); - } - console.log(response); - - return response; -} diff --git a/lib/api/helpers/users.ts b/lib/api/helpers/users.ts index fc925f4..1c8dc9d 100644 --- a/lib/api/helpers/users.ts +++ b/lib/api/helpers/users.ts @@ -1,49 +1,24 @@ -import { getSessionToken } from "@/lib/server_functions/userAuth"; - -import { cmFetch, cmFetchRaw } from "../cmFetch"; +import { cmFetch } from "../cmFetch"; import { Token, User } from "../types"; export async function getUsers() { return cmFetch(`/api/users`); } -export async function getCurrentUser() { - return cmFetchRaw( - `/api/users/user`, - { headers: { Authorization: `Bearer ${await getSessionToken()}` } }, - [401], - ); -} - export async function getUser(id: number) { return cmFetch(`/api/users/${id}`); } export async function loginUser(user: FormData) { - const body = { - username: user.get("username"), - password: user.get("password"), - }; return cmFetch(`/api/users/login`, { method: "POST", - headers: { - "Content-Type": "application/json", - }, - body: JSON.stringify(body), + body: user, }); } export async function createUser(user: FormData) { - const body = { - username: user.get("username"), - password: user.get("password"), - authority: 0, - }; return cmFetch(`/api/users`, { method: "POST", - headers: { - "Content-Type": "application/json", - }, - body: JSON.stringify(body), + body: user, }); } diff --git a/lib/server_functions/dataAccessLayer.ts b/lib/server_functions/dataAccessLayer.ts deleted file mode 100644 index 98af205..0000000 --- a/lib/server_functions/dataAccessLayer.ts +++ /dev/null @@ -1,23 +0,0 @@ -import { cookies } from "next/headers"; -import { cache } from "react"; - -import "server-only"; -import { getCurrentUser } from "../api/helpers/users"; -import { User } from "../api/types"; - -export const verifySession = cache(async () => { - const cookie = (await cookies()).get("access_token")?.value; - if (!cookie) { - return { isAuthed: false }; - } - const response = await getCurrentUser(); - if (response.status == 401) { - return { isAuthed: false }; - } - const user = (await response.json()) as User; - return { - isAuthed: true, - surface_auth_level: user.authority, - username: user.username, - }; -}); diff --git a/lib/server_functions/userAuth.ts b/lib/server_functions/userAuth.ts index 538c665..3871280 100644 --- a/lib/server_functions/userAuth.ts +++ b/lib/server_functions/userAuth.ts @@ -9,7 +9,7 @@ export async function login(formData: FormData) { "use server"; const token = await loginUser(formData); await createSession(token); - redirect("/Admin/Overview"); + redirect("/admin/overview"); } export async function signup(formData: FormData) { @@ -17,7 +17,7 @@ export async function signup(formData: FormData) { "use server"; const token = await createUser(formData); await createSession(token); - redirect("/Admin/Overview"); + redirect("/admin/overview"); } export async function createSession(token: Token) { @@ -26,16 +26,8 @@ export async function createSession(token: Token) { cookieStore.set("access_token", access_token, { httpOnly: true, - secure: process.env.NODE_ENV === "production", + secure: true, sameSite: "lax", path: "/", }); } - -export async function getSessionToken() { - const cookieStore = await cookies(); - console.log(cookieStore.getAll()); - const token = cookieStore.get("access_token")?.value; - - return token ? token : null; -}