edit:module:user | add middlewware for authenticated only
This commit is contained in:
@ -3,11 +3,11 @@ import { getCookie } from "../../helpers/http/userHeader/cookies/getCookies";
|
|||||||
import { returnErrorResponse } from "../../helpers/callback/httpResponse";
|
import { returnErrorResponse } from "../../helpers/callback/httpResponse";
|
||||||
import { mainErrorHandler } from "../../helpers/error/handler";
|
import { mainErrorHandler } from "../../helpers/error/handler";
|
||||||
|
|
||||||
export const authMiddleware = (ctx: Context) => {
|
export const authenticatedMiddleware = (ctx: Context) => {
|
||||||
try {
|
try {
|
||||||
const cookie = getCookie(ctx);
|
const cookie = getCookie(ctx);
|
||||||
if (!cookie.auth_token)
|
if (!cookie.auth_token)
|
||||||
return returnErrorResponse(ctx.set, 401, "User Unauthorized");
|
return returnErrorResponse(ctx.set, 401, "User Unauthenticated");
|
||||||
|
|
||||||
// pass
|
// pass
|
||||||
} catch (error) {
|
} catch (error) {
|
||||||
|
|||||||
@ -64,9 +64,7 @@ export const editUserController = async (
|
|||||||
try {
|
try {
|
||||||
// Get the user JWT token from cookies, if the token is not found, return an error response
|
// Get the user JWT token from cookies, if the token is not found, return an error response
|
||||||
const userCookie = getCookie(ctx);
|
const userCookie = getCookie(ctx);
|
||||||
const auth_token = userCookie.auth_token;
|
const auth_token = userCookie.auth_token!;
|
||||||
if (!auth_token)
|
|
||||||
return returnErrorResponse(ctx.set, 401, "User Unauthenticated");
|
|
||||||
|
|
||||||
// Get user browser header information from the context
|
// Get user browser header information from the context
|
||||||
const userHeaderInfo = getUserHeaderInformation(ctx);
|
const userHeaderInfo = getUserHeaderInformation(ctx);
|
||||||
|
|||||||
@ -3,10 +3,13 @@ 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";
|
import { unautenticatedMiddleware } from "../../middleware/auth/unauthenticated.middleware";
|
||||||
|
import { authenticatedMiddleware } from "../../middleware/auth/authenticated.middleware";
|
||||||
|
|
||||||
export const userModule = new Elysia({ prefix: "/users" })
|
export const userModule = new Elysia({ prefix: "/users" })
|
||||||
.get("/", getAllUserController)
|
.get("/", getAllUserController)
|
||||||
.put("/", editUserController)
|
|
||||||
.group("", (app) =>
|
.group("", (app) =>
|
||||||
app.onBeforeHandle(unautenticatedMiddleware).post("/", createUserController)
|
app.onBeforeHandle(unautenticatedMiddleware).post("/", createUserController)
|
||||||
|
)
|
||||||
|
.group("", (app) =>
|
||||||
|
app.onBeforeHandle(authenticatedMiddleware).put("/", editUserController)
|
||||||
);
|
);
|
||||||
|
|||||||
Reference in New Issue
Block a user