diff --git a/app/Admin/Login/page.tsx b/app/Admin/Login/page.tsx
index a02d945..f96e411 100644
--- a/app/Admin/Login/page.tsx
+++ b/app/Admin/Login/page.tsx
@@ -1,7 +1,24 @@
-import UnderConstruction from "@/components/static_pages/UnderConstruction";
+import { useState } from "react";
+import { login, signup } from "@/lib/server_functions/userAuth";
export default function Login() {
+ const [createAccount, setCreateAccount] = useState(false);
+
return (
-
- )
-}
\ No newline at end of file
+
+
+
+
+ );
+}
diff --git a/app/api/users/login/route.ts b/app/api/users/login/route.ts
new file mode 100644
index 0000000..dbf2277
--- /dev/null
+++ b/app/api/users/login/route.ts
@@ -0,0 +1,11 @@
+const BACKEND_URL = process.env.BACKEND_URL;
+
+export async function POST(request: Request) {
+ const response = await fetch(`${BACKEND_URL}/users/token`, request);
+
+ const data = await response.json();
+
+ return Response.json(data, {
+ status: response.status,
+ });
+}
diff --git a/app/api/users/route.ts b/app/api/users/route.ts
index 15eaa43..b961291 100644
--- a/app/api/users/route.ts
+++ b/app/api/users/route.ts
@@ -1,32 +1,25 @@
const BACKEND_URL = process.env.BACKEND_URL;
export async function GET() {
- const response = await fetch(`${BACKEND_URL}/users`);
+ const response = await fetch(`${BACKEND_URL}/users`);
- if (!response.ok) {
- return new Response("Request failed in backend", { status: response.status, });
- }
+ if (!response.ok) {
+ return new Response("Request failed in backend", {
+ status: response.status,
+ });
+ }
- const data = await response.json();
+ const data = await response.json();
- return Response.json(data);
+ return Response.json(data);
}
export async function POST(request: Request) {
+ const response = await fetch(`${BACKEND_URL}/users`, request);
- const body = await request.json();
+ const data = await response.json();
- const response = await fetch(`${BACKEND_URL}/users`, {
- method: "POST",
- headers: {
- "Content-Type": "application/json",
- },
- body: JSON.stringify(body),
- });
-
- const data = await response.json();
-
- return Response.json(data, {
- status: response.status,
- });
-}
\ No newline at end of file
+ return Response.json(data, {
+ status: response.status,
+ });
+}
diff --git a/lib/api/helpers/users.ts b/lib/api/helpers/users.ts
index 3665dd1..1c8dc9d 100644
--- a/lib/api/helpers/users.ts
+++ b/lib/api/helpers/users.ts
@@ -1,18 +1,24 @@
-export async function getUsers() {
- const response = await fetch("/api/users");
- if (!response.ok) {
- throw new Error(`Response status: ${response.status}`);
- }
+import { cmFetch } from "../cmFetch";
+import { Token, User } from "../types";
- return response.json();
+export async function getUsers() {
+ return cmFetch(`/api/users`);
}
export async function getUser(id: number) {
- const response = await fetch(`/api/users/${id}`);
-
- if (!response.ok) {
- throw new Error(`Response status: ${response.status}`);
- }
-
- return response.json();
+ return cmFetch(`/api/users/${id}`);
+}
+
+export async function loginUser(user: FormData) {
+ return cmFetch(`/api/users/login`, {
+ method: "POST",
+ body: user,
+ });
+}
+
+export async function createUser(user: FormData) {
+ return cmFetch(`/api/users`, {
+ method: "POST",
+ body: user,
+ });
}
diff --git a/lib/api/types.ts b/lib/api/types.ts
index 31589ca..dead8d5 100644
--- a/lib/api/types.ts
+++ b/lib/api/types.ts
@@ -6,6 +6,12 @@ export interface User {
authority: number;
}
+export interface Token {
+ access_token: string;
+
+ token_type: string;
+}
+
export interface Service {
id: number | null;
diff --git a/lib/server_functions/userAuth.ts b/lib/server_functions/userAuth.ts
new file mode 100644
index 0000000..d2fcb19
--- /dev/null
+++ b/lib/server_functions/userAuth.ts
@@ -0,0 +1,32 @@
+import { cookies } from "next/headers";
+import { redirect } from "next/navigation";
+
+import { createUser, loginUser } from "../api/helpers/users";
+import { Token } from "../api/types";
+
+export async function login(formData: FormData) {
+ "use server";
+ const token = await loginUser(formData);
+ await createSession(token);
+ redirect("/admin/overview");
+}
+
+export async function signup(formData: FormData) {
+ // Technically same code as login for now but will do different stuff with it later so separating it early on
+ "use server";
+ const token = await createUser(formData);
+ await createSession(token);
+ redirect("/admin/overview");
+}
+
+export async function createSession(token: Token) {
+ const access_token = token.access_token;
+ const cookieStore = await cookies();
+
+ cookieStore.set("access_token", access_token, {
+ httpOnly: true,
+ secure: true,
+ sameSite: "lax",
+ path: "/",
+ });
+}