edit:module:user | add middlewware for unauthenticated only
This commit is contained in:
@ -3,13 +3,12 @@ import { getCookie } from "../../helpers/http/userHeader/cookies/getCookies";
|
|||||||
import { returnErrorResponse } from "../../helpers/callback/httpResponse";
|
import { returnErrorResponse } from "../../helpers/callback/httpResponse";
|
||||||
|
|
||||||
export const unautenticatedMiddleware = (ctx: Context) => {
|
export const unautenticatedMiddleware = (ctx: Context) => {
|
||||||
|
// 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 {
|
try {
|
||||||
const cookies = getCookie(ctx);
|
const cookie = getCookie(ctx);
|
||||||
if (cookies.auth_token)
|
if (cookie && cookie.auth_token)
|
||||||
return returnErrorResponse(ctx.set, 403, "User already aunthenticated");
|
return returnErrorResponse(ctx.set, 401, "You are already logged in. ");
|
||||||
|
} catch (_) {
|
||||||
// pass
|
// Pass
|
||||||
} catch (error) {
|
|
||||||
// pass
|
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|||||||
@ -7,7 +7,6 @@ import { Context } from "elysia";
|
|||||||
import { createUserService } from "../services/createUser.service";
|
import { createUserService } from "../services/createUser.service";
|
||||||
import { mainErrorHandler } from "../../../helpers/error/handler";
|
import { mainErrorHandler } from "../../../helpers/error/handler";
|
||||||
import { createUserSchema } from "../schemas/createUser.schema";
|
import { createUserSchema } from "../schemas/createUser.schema";
|
||||||
import { getCookie } from "../../../helpers/http/userHeader/cookies/getCookies";
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @function createUser
|
* @function createUser
|
||||||
@ -31,19 +30,6 @@ import { getCookie } from "../../../helpers/http/userHeader/cookies/getCookies";
|
|||||||
export const createUserController = async (
|
export const createUserController = async (
|
||||||
ctx: Context & { body: Prisma.UserCreateInput }
|
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
|
// Validate the user input using a validation schema
|
||||||
const { error } = createUserSchema.validate(ctx.body);
|
const { error } = createUserSchema.validate(ctx.body);
|
||||||
if (error)
|
if (error)
|
||||||
|
|||||||
@ -2,8 +2,11 @@ import Elysia from "elysia";
|
|||||||
import { getAllUserController } from "./controller/getAllUser.controller";
|
import { getAllUserController } from "./controller/getAllUser.controller";
|
||||||
import { createUserController } from "./controller/createUser.controller";
|
import { createUserController } from "./controller/createUser.controller";
|
||||||
import { editUserController } from "./controller/editUser.controller";
|
import { editUserController } from "./controller/editUser.controller";
|
||||||
|
import { unautenticatedMiddleware } from "../../middleware/auth/unauthenticated.middleware";
|
||||||
|
|
||||||
export const userModule = new Elysia({ prefix: "/users" })
|
export const userModule = new Elysia({ prefix: "/users" })
|
||||||
.get("/", getAllUserController)
|
.get("/", getAllUserController)
|
||||||
.post("/", createUserController)
|
.put("/", editUserController)
|
||||||
.put("/", editUserController);
|
.group("", (app) =>
|
||||||
|
app.onBeforeHandle(unautenticatedMiddleware).post("/", createUserController)
|
||||||
|
);
|
||||||
|
|||||||
Reference in New Issue
Block a user