complete middleware
This commit is contained in:
@ -1,10 +1,14 @@
|
|||||||
import { Context } from "elysia";
|
import { Context } from "elysia";
|
||||||
|
import { getCookie } from "../helpers/http/userHeader/cookies/getCookies";
|
||||||
|
import { mainErrorHandler } from "../helpers/error/handler";
|
||||||
|
import { returnErrorResponse } from "../helpers/callback/httpResponse";
|
||||||
|
|
||||||
export const authMiddleware = (ctx: Context) => {
|
export const authMiddleware = (ctx: Context) => {
|
||||||
const token = ctx.cookie.auth_token;
|
try {
|
||||||
|
const cookie = getCookie(ctx);
|
||||||
if (!token) {
|
if (!cookie.auth_token)
|
||||||
ctx.set.status = 401;
|
return returnErrorResponse(ctx.set, 401, "User Unauthorized");
|
||||||
throw "Unauthorized: Token missing";
|
} catch (error) {
|
||||||
|
return mainErrorHandler(ctx.set, error);
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|||||||
@ -5,13 +5,6 @@ import { authVerification } from "./controller/authVerification.controller";
|
|||||||
|
|
||||||
export const authModule = new Elysia({ prefix: "/auth" })
|
export const authModule = new Elysia({ prefix: "/auth" })
|
||||||
.post("/legacy", loginWithPassword)
|
.post("/legacy", loginWithPassword)
|
||||||
.post("/verification", authVerification)
|
.post("/verification", authVerification, {
|
||||||
.get(
|
|
||||||
"/test",
|
|
||||||
() => {
|
|
||||||
return "PASSED";
|
|
||||||
},
|
|
||||||
{
|
|
||||||
beforeHandle: authMiddleware,
|
beforeHandle: authMiddleware,
|
||||||
}
|
});
|
||||||
);
|
|
||||||
|
|||||||
@ -1,6 +1,5 @@
|
|||||||
import { AppError } from "../../../helpers/error/instances/app";
|
import { AppError } from "../../../helpers/error/instances/app";
|
||||||
import { jwtDecode } from "../../../helpers/http/jwt/decode";
|
import { jwtDecode } from "../../../helpers/http/jwt/decode";
|
||||||
import { jwtEncode } from "../../../helpers/http/jwt/encode";
|
|
||||||
|
|
||||||
export const authVerificationService = (cookie: string) => {
|
export const authVerificationService = (cookie: string) => {
|
||||||
try {
|
try {
|
||||||
|
|||||||
Reference in New Issue
Block a user