Update user routing, added user authorization with JWT cookie management

This commit is contained in:
Hannah dagemark 2026-09-14 23:09:11 +02:00
commit f055e77eea
7 changed files with 97 additions and 10 deletions

View file

@ -1,24 +1,49 @@
import { cmFetch } from "../cmFetch";
import { getSessionToken } from "@/lib/server_functions/userAuth";
import { cmFetch, cmFetchRaw } from "../cmFetch";
import { Token, User } from "../types";
export async function getUsers() {
return cmFetch<User[]>(`/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<User>(`/api/users/${id}`);
}
export async function loginUser(user: FormData) {
const body = {
username: user.get("username"),
password: user.get("password"),
};
return cmFetch<Token>(`/api/users/login`, {
method: "POST",
body: user,
headers: {
"Content-Type": "application/json",
},
body: JSON.stringify(body),
});
}
export async function createUser(user: FormData) {
const body = {
username: user.get("username"),
password: user.get("password"),
authority: 0,
};
return cmFetch<Token>(`/api/users`, {
method: "POST",
body: user,
headers: {
"Content-Type": "application/json",
},
body: JSON.stringify(body),
});
}