new routes
This commit is contained in:
@@ -36,7 +36,7 @@ export const actions = {
|
|||||||
console.log('Is creating: ', isCreating);
|
console.log('Is creating: ', isCreating);
|
||||||
// console.dir(formData);
|
// console.dir(formData);
|
||||||
console.dir(processedData.user.membership);
|
console.dir(processedData.user.membership);
|
||||||
const apiURL = `${BASE_API_URI}/backend/users/upsert/`;
|
const apiURL = `${BASE_API_URI}/backend/users/`;
|
||||||
|
|
||||||
/** @type {RequestInit} */
|
/** @type {RequestInit} */
|
||||||
const requestUpdateOptions = {
|
const requestUpdateOptions = {
|
||||||
|
|||||||
@@ -6,7 +6,6 @@ import { userDatesFromRFC3339, refreshCookie } from '$lib/utils/helpers';
|
|||||||
export async function load({ cookies, fetch, locals }) {
|
export async function load({ cookies, fetch, locals }) {
|
||||||
const jwt = cookies.get('jwt');
|
const jwt = cookies.get('jwt');
|
||||||
try {
|
try {
|
||||||
// Fetch user data, subscriptions, and licence categories in parallel
|
|
||||||
const response = await fetch(`${BASE_API_URI}/backend/users/all`, {
|
const response = await fetch(`${BASE_API_URI}/backend/users/all`, {
|
||||||
credentials: 'include',
|
credentials: 'include',
|
||||||
headers: {
|
headers: {
|
||||||
@@ -16,7 +15,7 @@ export async function load({ cookies, fetch, locals }) {
|
|||||||
if (!response.ok) {
|
if (!response.ok) {
|
||||||
// Clear the invalid JWT cookie
|
// Clear the invalid JWT cookie
|
||||||
cookies.delete('jwt', { path: '/' });
|
cookies.delete('jwt', { path: '/' });
|
||||||
throw redirect(302, '/auth/login?next=/');
|
throw redirect(302, '/auth/login?next=admin/users/');
|
||||||
}
|
}
|
||||||
|
|
||||||
const data = await response.json();
|
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
|
// In case of any error, clear the JWT cookie
|
||||||
cookies.delete('jwt', { path: '/' });
|
cookies.delete('jwt', { path: '/' });
|
||||||
|
|
||||||
throw redirect(302, '/auth/login?next=/');
|
throw redirect(302, '/auth/login?next=admin/users/');
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -15,7 +15,7 @@ import {
|
|||||||
export async function load({ locals }) {
|
export async function load({ locals }) {
|
||||||
// redirect user if not logged in
|
// redirect user if not logged in
|
||||||
if (!locals.user) {
|
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);
|
console.dir(processedData.user.membership);
|
||||||
const isCreating = !processedData.user.id || processedData.user.id === 0;
|
const isCreating = !processedData.user.id || processedData.user.id === 0;
|
||||||
console.log('Is creating: ', isCreating);
|
console.log('Is creating: ', isCreating);
|
||||||
const apiURL = `${BASE_API_URI}/backend/users/upsert`;
|
const apiURL = `${BASE_API_URI}/backend/users`;
|
||||||
|
|
||||||
/** @type {RequestInit} */
|
/** @type {RequestInit} */
|
||||||
const requestOptions = {
|
const requestOptions = {
|
||||||
@@ -122,7 +122,7 @@ export const actions = {
|
|||||||
const rawData = formDataToObject(formData);
|
const rawData = formDataToObject(formData);
|
||||||
const processedData = processUserFormData(rawData);
|
const processedData = processUserFormData(rawData);
|
||||||
|
|
||||||
const apiURL = `${BASE_API_URI}/backend/users/delete`;
|
const apiURL = `${BASE_API_URI}/backend/users`;
|
||||||
|
|
||||||
/** @type {RequestInit} */
|
/** @type {RequestInit} */
|
||||||
const requestOptions = {
|
const requestOptions = {
|
||||||
|
|||||||
@@ -1,57 +1,54 @@
|
|||||||
import { BASE_API_URI } from "$lib/utils/constants";
|
import { BASE_API_URI } from '$lib/utils/constants';
|
||||||
import { fail, redirect } from "@sveltejs/kit";
|
import { fail, redirect } from '@sveltejs/kit';
|
||||||
|
|
||||||
/** @type {import('./$types').PageServerLoad} */
|
/** @type {import('./$types').PageServerLoad} */
|
||||||
export async function load({ locals }) {
|
export async function load({ locals }) {
|
||||||
// redirect user if not logged in
|
// redirect user if not logged in
|
||||||
if (!locals.user) {
|
if (!locals.user) {
|
||||||
throw redirect(302, `/auth/login?next=/`);
|
throw redirect(302, `/auth/login?next=/`);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
/** @type {import('./$types').Actions} */
|
/** @type {import('./$types').Actions} */
|
||||||
export const actions = {
|
export const actions = {
|
||||||
default: async ({ fetch, cookies }) => {
|
default: async ({ fetch, cookies }) => {
|
||||||
/** @type {RequestInit} */
|
/** @type {RequestInit} */
|
||||||
const requestInitOptions = {
|
const requestInitOptions = {
|
||||||
method: "POST",
|
method: 'POST',
|
||||||
credentials: "include",
|
credentials: 'include',
|
||||||
headers: {
|
headers: {
|
||||||
"Content-Type": "application/json",
|
'Content-Type': 'application/json',
|
||||||
Cookie: `jwt=${cookies.get("jwt")}`,
|
Cookie: `jwt=${cookies.get('jwt')}`
|
||||||
},
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
const res = await fetch(
|
const res = await fetch(`${BASE_API_URI}/backend/logout/`, requestInitOptions);
|
||||||
`${BASE_API_URI}/backend/users/logout/`,
|
|
||||||
requestInitOptions
|
|
||||||
);
|
|
||||||
|
|
||||||
if (!res.ok) {
|
if (!res.ok) {
|
||||||
const response = await res.json();
|
const response = await res.json();
|
||||||
const errors = [];
|
const errors = [];
|
||||||
errors.push({ error: response.error, id: 0 });
|
errors.push({ error: response.error, id: 0 });
|
||||||
return fail(400, { errors: errors });
|
return fail(400, { errors: errors });
|
||||||
}
|
}
|
||||||
|
|
||||||
// eat the cookie
|
// eat the cookie
|
||||||
cookies.delete("jwt", { path: "/" });
|
cookies.delete('jwt', { path: '/' });
|
||||||
|
|
||||||
// The server should clear the cookie, so we don't need to handle it here
|
// 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
|
// Just check if the cookie is cleared in the response
|
||||||
const setCookieHeader = res.headers.get("set-cookie");
|
const setCookieHeader = res.headers.get('set-cookie');
|
||||||
if (!setCookieHeader || !setCookieHeader.includes("jwt=;")) {
|
if (!setCookieHeader || !setCookieHeader.includes('jwt=;')) {
|
||||||
console.error("JWT cookie not cleared in response");
|
console.error('JWT cookie not cleared in response');
|
||||||
return fail(500, {
|
return fail(500, {
|
||||||
errors: [
|
errors: [
|
||||||
{
|
{
|
||||||
error: "Server error: Failed to clear authentication token",
|
error: 'Server error: Failed to clear authentication token',
|
||||||
id: Date.now(),
|
id: Date.now()
|
||||||
},
|
}
|
||||||
],
|
]
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
// redirect the user
|
// redirect the user
|
||||||
throw redirect(302, "/auth/login");
|
throw redirect(302, '/auth/login');
|
||||||
},
|
}
|
||||||
};
|
};
|
||||||
|
|||||||
@@ -21,15 +21,15 @@ func RegisterRoutes(router *gin.Engine, userController *controllers.UserControll
|
|||||||
// apiRouter.POST("/v1/subscription", membershipcontroller.RegisterSubscription)
|
// apiRouter.POST("/v1/subscription", membershipcontroller.RegisterSubscription)
|
||||||
// }
|
// }
|
||||||
|
|
||||||
userRouter := router.Group("/backend/users")
|
userRouter := router.Group("/backend")
|
||||||
userRouter.Use(middlewares.AuthMiddleware())
|
userRouter.Use(middlewares.AuthMiddleware())
|
||||||
{
|
{
|
||||||
userRouter.GET("/current", userController.CurrentUserHandler)
|
userRouter.GET("/users/current", userController.CurrentUserHandler)
|
||||||
userRouter.POST("/logout", userController.LogoutHandler)
|
userRouter.POST("/logout", userController.LogoutHandler)
|
||||||
userRouter.PATCH("/upsert", userController.UpdateHandler)
|
userRouter.PATCH("/users", userController.UpdateHandler)
|
||||||
userRouter.POST("/upsert", userController.RegisterUser)
|
userRouter.POST("/users", userController.RegisterUser)
|
||||||
userRouter.GET("/all", userController.GetAllUsers)
|
userRouter.GET("/users/all", userController.GetAllUsers)
|
||||||
userRouter.DELETE("/delete", userController.DeleteUser)
|
userRouter.DELETE("/users", userController.DeleteUser)
|
||||||
}
|
}
|
||||||
|
|
||||||
membershipRouter := router.Group("/backend/membership")
|
membershipRouter := router.Group("/backend/membership")
|
||||||
|
|||||||
Reference in New Issue
Block a user