WIP: Major Update 0.2 #3
6 changed files with 101 additions and 36 deletions
Add user login, signup, and session.
commit
f5466faa70
|
|
@ -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() {
|
export default function Login() {
|
||||||
|
const [createAccount, setCreateAccount] = useState(false);
|
||||||
|
|
||||||
return (
|
return (
|
||||||
<UnderConstruction />
|
<div>
|
||||||
)
|
<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>
|
||||||
|
);
|
||||||
}
|
}
|
||||||
11
app/api/users/login/route.ts
Normal file
11
app/api/users/login/route.ts
Normal file
|
|
@ -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,
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
@ -4,7 +4,9 @@ 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", { status: response.status, });
|
return new Response("Request failed in backend", {
|
||||||
|
status: response.status,
|
||||||
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
const data = await response.json();
|
const data = await response.json();
|
||||||
|
|
@ -13,16 +15,7 @@ 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,18 +1,24 @@
|
||||||
export async function getUsers() {
|
import { cmFetch } from "../cmFetch";
|
||||||
const response = await fetch("/api/users");
|
import { Token, User } from "../types";
|
||||||
if (!response.ok) {
|
|
||||||
throw new Error(`Response status: ${response.status}`);
|
|
||||||
}
|
|
||||||
|
|
||||||
return response.json();
|
export async function getUsers() {
|
||||||
|
return cmFetch<User[]>(`/api/users`);
|
||||||
}
|
}
|
||||||
|
|
||||||
export async function getUser(id: number) {
|
export async function getUser(id: number) {
|
||||||
const response = await fetch(`/api/users/${id}`);
|
return cmFetch<User>(`/api/users/${id}`);
|
||||||
|
|
||||||
if (!response.ok) {
|
|
||||||
throw new Error(`Response status: ${response.status}`);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
return response.json();
|
export async function loginUser(user: FormData) {
|
||||||
|
return cmFetch<Token>(`/api/users/login`, {
|
||||||
|
method: "POST",
|
||||||
|
body: user,
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
|
export async function createUser(user: FormData) {
|
||||||
|
return cmFetch<Token>(`/api/users`, {
|
||||||
|
method: "POST",
|
||||||
|
body: user,
|
||||||
|
});
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -6,6 +6,12 @@ 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;
|
||||||
|
|
||||||
|
|
|
||||||
32
lib/server_functions/userAuth.ts
Normal file
32
lib/server_functions/userAuth.ts
Normal file
|
|
@ -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: "/",
|
||||||
|
});
|
||||||
|
}
|
||||||
Loading…
Reference in a new issue