finishing auth controller verification
This commit is contained in:
@ -1,29 +1,14 @@
|
|||||||
import jwt from "jsonwebtoken";
|
import jwt from "jsonwebtoken";
|
||||||
import { Context } from "elysia";
|
|
||||||
import { JWTAuthToken } from "./types";
|
|
||||||
import { parse } from "cookie";
|
|
||||||
import { returnErrorResponse } from "../../../callback/httpResponse";
|
|
||||||
|
|
||||||
export const JWTDecodeToken = (ctx: Context): JWTAuthToken => {
|
export const jwtDecode = (payload: string) => {
|
||||||
const cookiePayload = ctx.request.headers.get("Cookie");
|
// return payload;
|
||||||
if (!cookiePayload)
|
if (!payload) throw "JWT decode payload not found";
|
||||||
throw returnErrorResponse(ctx.set, 400, "Bad Request", "No cookies found");
|
const JWTKey = process.env.JWT_SECRET!;
|
||||||
|
|
||||||
const cookies = parse(cookiePayload);
|
|
||||||
const cookiesToken = cookies.auth_token!;
|
|
||||||
|
|
||||||
try {
|
try {
|
||||||
const decodedToken = jwt.verify(
|
const decodedPayload = jwt.verify(payload, JWTKey);
|
||||||
cookiesToken,
|
return decodedPayload;
|
||||||
process.env.JWT_SECRET!
|
|
||||||
) as JWTAuthToken;
|
|
||||||
return decodedToken;
|
|
||||||
} catch (error) {
|
} catch (error) {
|
||||||
throw returnErrorResponse(
|
throw "JWT expired or not valid";
|
||||||
ctx.set,
|
|
||||||
401,
|
|
||||||
"Unauthorized",
|
|
||||||
"Invalid or expired token"
|
|
||||||
);
|
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|||||||
13
src/helpers/http/userHeader/cookies/getCookies.ts
Normal file
13
src/helpers/http/userHeader/cookies/getCookies.ts
Normal file
@ -0,0 +1,13 @@
|
|||||||
|
import { parse } from "cookie";
|
||||||
|
import { Context } from "elysia";
|
||||||
|
import { AppError } from "../../../error/instances/app";
|
||||||
|
|
||||||
|
export const getCookie = (ctx: Context) => {
|
||||||
|
try {
|
||||||
|
const cookiePayload = ctx.request.headers.get("Cookie");
|
||||||
|
const cookies = parse(cookiePayload!);
|
||||||
|
return cookies;
|
||||||
|
} catch (error) {
|
||||||
|
throw new AppError(401, "Cookie not found");
|
||||||
|
}
|
||||||
|
};
|
||||||
10
src/middleware/auth.middleware.ts
Normal file
10
src/middleware/auth.middleware.ts
Normal file
@ -0,0 +1,10 @@
|
|||||||
|
import { Context } from "elysia";
|
||||||
|
|
||||||
|
export const authMiddleware = (ctx: Context) => {
|
||||||
|
const token = ctx.cookie.auth_token;
|
||||||
|
|
||||||
|
if (!token) {
|
||||||
|
ctx.set.status = 401;
|
||||||
|
throw "Unauthorized: Token missing";
|
||||||
|
}
|
||||||
|
};
|
||||||
21
src/modules/auth/controller/authVerification.controller.ts
Normal file
21
src/modules/auth/controller/authVerification.controller.ts
Normal file
@ -0,0 +1,21 @@
|
|||||||
|
import { Context } from "elysia";
|
||||||
|
import { getCookie } from "../../../helpers/http/userHeader/cookies/getCookies";
|
||||||
|
import { authVerificationService } from "../services/authVerification.service";
|
||||||
|
import { mainErrorHandler } from "../../../helpers/error/handler";
|
||||||
|
import {
|
||||||
|
returnErrorResponse,
|
||||||
|
returnWriteResponse,
|
||||||
|
} from "../../../helpers/callback/httpResponse";
|
||||||
|
|
||||||
|
export const authVerification = async (ctx: Context) => {
|
||||||
|
try {
|
||||||
|
const cookie = getCookie(ctx);
|
||||||
|
if (!cookie.auth_token)
|
||||||
|
return returnErrorResponse(ctx.set, 401, "Auth token not found");
|
||||||
|
|
||||||
|
const authService = authVerificationService(cookie.auth_token);
|
||||||
|
return returnWriteResponse(ctx.set, 200, "User authenticated", authService);
|
||||||
|
} catch (error) {
|
||||||
|
return mainErrorHandler(ctx.set, error);
|
||||||
|
}
|
||||||
|
};
|
||||||
@ -1,7 +1,17 @@
|
|||||||
import Elysia from "elysia";
|
import Elysia from "elysia";
|
||||||
import { loginWithPassword } from "./controller/loginWithPassword.controller";
|
import { loginWithPassword } from "./controller/loginWithPassword.controller";
|
||||||
|
import { authMiddleware } from "../../middleware/auth.middleware";
|
||||||
|
import { authVerification } from "./controller/authVerification.controller";
|
||||||
|
|
||||||
export const authModule = new Elysia({ prefix: "/auth" }).post(
|
export const authModule = new Elysia({ prefix: "/auth" })
|
||||||
"/legacy",
|
.post("/legacy", loginWithPassword)
|
||||||
loginWithPassword
|
.post("/verification", authVerification)
|
||||||
);
|
.get(
|
||||||
|
"/test",
|
||||||
|
() => {
|
||||||
|
return "PASSED";
|
||||||
|
},
|
||||||
|
{
|
||||||
|
beforeHandle: authMiddleware,
|
||||||
|
}
|
||||||
|
);
|
||||||
|
|||||||
12
src/modules/auth/services/authVerification.service.ts
Normal file
12
src/modules/auth/services/authVerification.service.ts
Normal file
@ -0,0 +1,12 @@
|
|||||||
|
import { AppError } from "../../../helpers/error/instances/app";
|
||||||
|
import { jwtDecode } from "../../../helpers/http/jwt/decode";
|
||||||
|
import { jwtEncode } from "../../../helpers/http/jwt/encode";
|
||||||
|
|
||||||
|
export const authVerificationService = (cookie: string) => {
|
||||||
|
try {
|
||||||
|
const userToken = jwtDecode(cookie);
|
||||||
|
return userToken;
|
||||||
|
} catch (error) {
|
||||||
|
throw new AppError(401, "Token is invalid");
|
||||||
|
}
|
||||||
|
};
|
||||||
@ -5,7 +5,6 @@ import {
|
|||||||
returnWriteResponse,
|
returnWriteResponse,
|
||||||
} from "../../../helpers/callback/httpResponse";
|
} from "../../../helpers/callback/httpResponse";
|
||||||
import { createUserRoleService } from "../services/createUserRole.service";
|
import { createUserRoleService } from "../services/createUserRole.service";
|
||||||
import { JWTDecodeToken } from "../../../helpers/http/jwt/decode";
|
|
||||||
import { mainErrorHandler } from "../../../helpers/error/handler";
|
import { mainErrorHandler } from "../../../helpers/error/handler";
|
||||||
import { createUserRoleSchema } from "../schemas/createUserRole.schema";
|
import { createUserRoleSchema } from "../schemas/createUserRole.schema";
|
||||||
|
|
||||||
@ -51,7 +50,7 @@ export const createUserRole = async (
|
|||||||
|
|
||||||
const formData: Prisma.UserRoleUncheckedCreateInput = {
|
const formData: Prisma.UserRoleUncheckedCreateInput = {
|
||||||
...ctx.body,
|
...ctx.body,
|
||||||
createdBy: JWTDecodeToken(ctx).user.id,
|
createdBy: "daw",
|
||||||
};
|
};
|
||||||
|
|
||||||
try {
|
try {
|
||||||
|
|||||||
Reference in New Issue
Block a user