add:module:auth:*logout | add logout module and clean all session in system
This commit is contained in:
@ -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)
|
||||
|
||||
@ -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
|
||||
|
||||
Reference in New Issue
Block a user