add:module:auth:*logout | add logout module and clean all session in system

This commit is contained in:
rafiarrafif
2025-06-19 17:16:54 +07:00
parent fdfafcd2e0
commit ac82676505
15 changed files with 130 additions and 5 deletions

View File

@ -7,6 +7,7 @@ import { Context } from "elysia";
import { createUserService } from "../services/createUser.service";
import { mainErrorHandler } from "../../../helpers/error/handler";
import { createUserSchema } from "../schemas/createUser.schema";
import { getCookie } from "../../../helpers/http/userHeader/cookies/getCookies";
/**
* @function createUser
@ -30,6 +31,19 @@ import { createUserSchema } from "../schemas/createUser.schema";
export const createUserController = async (
ctx: Context & { body: Prisma.UserCreateInput }
) => {
// Check if the user is already logged in by checking the auth token in cookies. If the user is logged in, return an error response
try {
const cookie = getCookie(ctx);
if (cookie && cookie.auth_token)
return returnErrorResponse(
ctx.set,
401,
"You are already logged in. Please log out first if you want to create a new account."
);
} catch (_) {
// Pass
}
// Validate the user input using a validation schema
const { error } = createUserSchema.validate(ctx.body);
if (error)

View File

@ -25,6 +25,7 @@ export const editUserService = async (
"The username or email has already taken by another user."
);
// Prepare the fields to update, only include fields that are provided in the payload
const fieldsToUpdate: Partial<Prisma.UserUpdateInput> = {
...(payload.username && payload.username !== jwtSession.user.username
? { username: payload.username }
@ -53,6 +54,7 @@ export const editUserService = async (
: {}),
};
// Update the user in the database, use username from the JWT session to find the user
const updateUser = await updateUserRepo(
jwtSession.user.username,
fieldsToUpdate