From ab168311a90d8b650b7288fad77867ad67a19f38 Mon Sep 17 00:00:00 2001 From: Alex <$(pass /github/email)> Date: Thu, 20 Feb 2025 09:06:27 +0100 Subject: [PATCH] new routes --- .../routes/auth/about/[id]/+page.server.js | 2 +- .../routes/auth/admin/users/+layout.server.js | 5 +- .../routes/auth/admin/users/+page.server.js | 6 +- .../src/routes/auth/logout/+page.server.js | 87 +++++++++---------- internal/routes/routes.go | 12 +-- 5 files changed, 54 insertions(+), 58 deletions(-) diff --git a/frontend/src/routes/auth/about/[id]/+page.server.js b/frontend/src/routes/auth/about/[id]/+page.server.js index 162bd7e..1c37709 100644 --- a/frontend/src/routes/auth/about/[id]/+page.server.js +++ b/frontend/src/routes/auth/about/[id]/+page.server.js @@ -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 = { diff --git a/frontend/src/routes/auth/admin/users/+layout.server.js b/frontend/src/routes/auth/admin/users/+layout.server.js index 42bf31a..0970cf1 100644 --- a/frontend/src/routes/auth/admin/users/+layout.server.js +++ b/frontend/src/routes/auth/admin/users/+layout.server.js @@ -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/'); } } diff --git a/frontend/src/routes/auth/admin/users/+page.server.js b/frontend/src/routes/auth/admin/users/+page.server.js index 0c42adf..37c49c2 100644 --- a/frontend/src/routes/auth/admin/users/+page.server.js +++ b/frontend/src/routes/auth/admin/users/+page.server.js @@ -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 = { diff --git a/frontend/src/routes/auth/logout/+page.server.js b/frontend/src/routes/auth/logout/+page.server.js index 043ec40..de54396 100644 --- a/frontend/src/routes/auth/logout/+page.server.js +++ b/frontend/src/routes/auth/logout/+page.server.js @@ -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'); + } }; diff --git a/internal/routes/routes.go b/internal/routes/routes.go index bb340dc..6ab4d04 100644 --- a/internal/routes/routes.go +++ b/internal/routes/routes.go @@ -21,15 +21,15 @@ func RegisterRoutes(router *gin.Engine, userController *controllers.UserControll // apiRouter.POST("/v1/subscription", membershipcontroller.RegisterSubscription) // } - userRouter := router.Group("/backend/users") + userRouter := router.Group("/backend") userRouter.Use(middlewares.AuthMiddleware()) { - userRouter.GET("/current", userController.CurrentUserHandler) + userRouter.GET("/users/current", userController.CurrentUserHandler) userRouter.POST("/logout", userController.LogoutHandler) - userRouter.PATCH("/upsert", userController.UpdateHandler) - userRouter.POST("/upsert", userController.RegisterUser) - userRouter.GET("/all", userController.GetAllUsers) - userRouter.DELETE("/delete", userController.DeleteUser) + userRouter.PATCH("/users", userController.UpdateHandler) + userRouter.POST("/users", userController.RegisterUser) + userRouter.GET("/users/all", userController.GetAllUsers) + userRouter.DELETE("/users", userController.DeleteUser) } membershipRouter := router.Group("/backend/membership")