Compare commits
9 changed files with 618 additions and 939 deletions
|
|
@ -1,2 +1,2 @@
|
||||||
NEXT_PUBLIC_FRONTEND_URL=
|
FRONTEND_URL=
|
||||||
BACKEND_URL=
|
BACKEND_URL=
|
||||||
|
|
@ -1,24 +1,7 @@
|
||||||
import { useState } from "react";
|
import UnderConstruction from "@/components/static_pages/UnderConstruction";
|
||||||
|
|
||||||
import { login, signup } from "@/lib/server_functions/userAuth";
|
|
||||||
export default function Login() {
|
export default function Login() {
|
||||||
const [createAccount, setCreateAccount] = useState(false);
|
|
||||||
|
|
||||||
return (
|
return (
|
||||||
<div>
|
<UnderConstruction />
|
||||||
<form action={createAccount ? login : signup}>
|
)
|
||||||
<input type="text" name="username" placeholder="Username" required />
|
|
||||||
<input
|
|
||||||
type="password"
|
|
||||||
name="password"
|
|
||||||
placeholder="Password"
|
|
||||||
required
|
|
||||||
/>
|
|
||||||
<button type="submit">{createAccount ? "Login" : "Signup"}</button>
|
|
||||||
</form>
|
|
||||||
<button onClick={() => setCreateAccount(!createAccount)}>
|
|
||||||
{createAccount ? "Don't have an account?" : "Already have an account?"}
|
|
||||||
</button>
|
|
||||||
</div>
|
|
||||||
);
|
|
||||||
}
|
}
|
||||||
|
|
@ -1,11 +0,0 @@
|
||||||
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,
|
|
||||||
});
|
|
||||||
}
|
|
||||||
|
|
@ -4,9 +4,7 @@ export async function GET() {
|
||||||
const response = await fetch(`${BACKEND_URL}/users`);
|
const response = await fetch(`${BACKEND_URL}/users`);
|
||||||
|
|
||||||
if (!response.ok) {
|
if (!response.ok) {
|
||||||
return new Response("Request failed in backend", {
|
return new Response("Request failed in backend", { status: response.status, });
|
||||||
status: response.status,
|
|
||||||
});
|
|
||||||
}
|
}
|
||||||
|
|
||||||
const data = await response.json();
|
const data = await response.json();
|
||||||
|
|
@ -15,7 +13,16 @@ export async function GET() {
|
||||||
}
|
}
|
||||||
|
|
||||||
export async function POST(request: Request) {
|
export async function POST(request: Request) {
|
||||||
const response = await fetch(`${BACKEND_URL}/users`, request);
|
|
||||||
|
const body = await request.json();
|
||||||
|
|
||||||
|
const response = await fetch(`${BACKEND_URL}/users`, {
|
||||||
|
method: "POST",
|
||||||
|
headers: {
|
||||||
|
"Content-Type": "application/json",
|
||||||
|
},
|
||||||
|
body: JSON.stringify(body),
|
||||||
|
});
|
||||||
|
|
||||||
const data = await response.json();
|
const data = await response.json();
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -1,6 +1,6 @@
|
||||||
import { RequestInit } from "next/dist/server/web/spec-extension/request";
|
import { RequestInit } from "next/dist/server/web/spec-extension/request";
|
||||||
|
|
||||||
const FRONTEND_URL = process.env.NEXT_PUBLIC_FRONTEND_URL;
|
const FRONTEND_URL = process.env.FRONTEND_URL;
|
||||||
|
|
||||||
export async function cmFetch<T>(
|
export async function cmFetch<T>(
|
||||||
bffUrl: string | URL,
|
bffUrl: string | URL,
|
||||||
|
|
|
||||||
|
|
@ -1,24 +1,18 @@
|
||||||
import { cmFetch } from "../cmFetch";
|
|
||||||
import { Token, User } from "../types";
|
|
||||||
|
|
||||||
export async function getUsers() {
|
export async function getUsers() {
|
||||||
return cmFetch<User[]>(`/api/users`);
|
const response = await fetch("/api/users");
|
||||||
|
if (!response.ok) {
|
||||||
|
throw new Error(`Response status: ${response.status}`);
|
||||||
|
}
|
||||||
|
|
||||||
|
return response.json();
|
||||||
}
|
}
|
||||||
|
|
||||||
export async function getUser(id: number) {
|
export async function getUser(id: number) {
|
||||||
return cmFetch<User>(`/api/users/${id}`);
|
const response = await fetch(`/api/users/${id}`);
|
||||||
}
|
|
||||||
|
|
||||||
export async function loginUser(user: FormData) {
|
if (!response.ok) {
|
||||||
return cmFetch<Token>(`/api/users/login`, {
|
throw new Error(`Response status: ${response.status}`);
|
||||||
method: "POST",
|
}
|
||||||
body: user,
|
|
||||||
});
|
|
||||||
}
|
|
||||||
|
|
||||||
export async function createUser(user: FormData) {
|
return response.json();
|
||||||
return cmFetch<Token>(`/api/users`, {
|
|
||||||
method: "POST",
|
|
||||||
body: user,
|
|
||||||
});
|
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -6,12 +6,6 @@ export interface User {
|
||||||
authority: number;
|
authority: number;
|
||||||
}
|
}
|
||||||
|
|
||||||
export interface Token {
|
|
||||||
access_token: string;
|
|
||||||
|
|
||||||
token_type: string;
|
|
||||||
}
|
|
||||||
|
|
||||||
export interface Service {
|
export interface Service {
|
||||||
id: number | null;
|
id: number | null;
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -1,32 +0,0 @@
|
||||||
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: "/",
|
|
||||||
});
|
|
||||||
}
|
|
||||||
1416
package-lock.json
generated
1416
package-lock.json
generated
File diff suppressed because it is too large
Load diff
Loading…
Reference in a new issue