new routes

This commit is contained in:
Alex
2025-02-20 09:06:27 +01:00
parent 54faee731d
commit ab168311a9
5 changed files with 54 additions and 58 deletions

View File

@@ -36,7 +36,7 @@ export const actions = {
console.log('Is creating: ', isCreating);
// console.dir(formData);
console.dir(processedData.user.membership);
const apiURL = `${BASE_API_URI}/backend/users/upsert/`;
const apiURL = `${BASE_API_URI}/backend/users/`;
/** @type {RequestInit} */
const requestUpdateOptions = {

View File

@@ -6,7 +6,6 @@ import { userDatesFromRFC3339, refreshCookie } from '$lib/utils/helpers';
export async function load({ cookies, fetch, locals }) {
const jwt = cookies.get('jwt');
try {
// Fetch user data, subscriptions, and licence categories in parallel
const response = await fetch(`${BASE_API_URI}/backend/users/all`, {
credentials: 'include',
headers: {
@@ -16,7 +15,7 @@ export async function load({ cookies, fetch, locals }) {
if (!response.ok) {
// Clear the invalid JWT cookie
cookies.delete('jwt', { path: '/' });
throw redirect(302, '/auth/login?next=/');
throw redirect(302, '/auth/login?next=admin/users/');
}
const data = await response.json();
@@ -43,6 +42,6 @@ export async function load({ cookies, fetch, locals }) {
// In case of any error, clear the JWT cookie
cookies.delete('jwt', { path: '/' });
throw redirect(302, '/auth/login?next=/');
throw redirect(302, '/auth/login?next=admin/users/');
}
}

View File

@@ -15,7 +15,7 @@ import {
export async function load({ locals }) {
// redirect user if not logged in
if (!locals.user) {
throw redirect(302, `/auth/login?next=/auth/users`);
throw redirect(302, `/auth/login?next=/auth/admin/users`);
}
}
@@ -38,7 +38,7 @@ export const actions = {
console.dir(processedData.user.membership);
const isCreating = !processedData.user.id || processedData.user.id === 0;
console.log('Is creating: ', isCreating);
const apiURL = `${BASE_API_URI}/backend/users/upsert`;
const apiURL = `${BASE_API_URI}/backend/users`;
/** @type {RequestInit} */
const requestOptions = {
@@ -122,7 +122,7 @@ export const actions = {
const rawData = formDataToObject(formData);
const processedData = processUserFormData(rawData);
const apiURL = `${BASE_API_URI}/backend/users/delete`;
const apiURL = `${BASE_API_URI}/backend/users`;
/** @type {RequestInit} */
const requestOptions = {

View File

@@ -1,57 +1,54 @@
import { BASE_API_URI } from "$lib/utils/constants";
import { fail, redirect } from "@sveltejs/kit";
import { BASE_API_URI } from '$lib/utils/constants';
import { fail, redirect } from '@sveltejs/kit';
/** @type {import('./$types').PageServerLoad} */
export async function load({ locals }) {
// redirect user if not logged in
if (!locals.user) {
throw redirect(302, `/auth/login?next=/`);
}
// redirect user if not logged in
if (!locals.user) {
throw redirect(302, `/auth/login?next=/`);
}
}
/** @type {import('./$types').Actions} */
export const actions = {
default: async ({ fetch, cookies }) => {
/** @type {RequestInit} */
const requestInitOptions = {
method: "POST",
credentials: "include",
headers: {
"Content-Type": "application/json",
Cookie: `jwt=${cookies.get("jwt")}`,
},
};
default: async ({ fetch, cookies }) => {
/** @type {RequestInit} */
const requestInitOptions = {
method: 'POST',
credentials: 'include',
headers: {
'Content-Type': 'application/json',
Cookie: `jwt=${cookies.get('jwt')}`
}
};
const res = await fetch(
`${BASE_API_URI}/backend/users/logout/`,
requestInitOptions
);
const res = await fetch(`${BASE_API_URI}/backend/logout/`, requestInitOptions);
if (!res.ok) {
const response = await res.json();
const errors = [];
errors.push({ error: response.error, id: 0 });
return fail(400, { errors: errors });
}
if (!res.ok) {
const response = await res.json();
const errors = [];
errors.push({ error: response.error, id: 0 });
return fail(400, { errors: errors });
}
// eat the cookie
cookies.delete("jwt", { path: "/" });
// eat the cookie
cookies.delete('jwt', { path: '/' });
// The server should clear the cookie, so we don't need to handle it here
// Just check if the cookie is cleared in the response
const setCookieHeader = res.headers.get("set-cookie");
if (!setCookieHeader || !setCookieHeader.includes("jwt=;")) {
console.error("JWT cookie not cleared in response");
return fail(500, {
errors: [
{
error: "Server error: Failed to clear authentication token",
id: Date.now(),
},
],
});
}
// redirect the user
throw redirect(302, "/auth/login");
},
// The server should clear the cookie, so we don't need to handle it here
// Just check if the cookie is cleared in the response
const setCookieHeader = res.headers.get('set-cookie');
if (!setCookieHeader || !setCookieHeader.includes('jwt=;')) {
console.error('JWT cookie not cleared in response');
return fail(500, {
errors: [
{
error: 'Server error: Failed to clear authentication token',
id: Date.now()
}
]
});
}
// redirect the user
throw redirect(302, '/auth/login');
}
};