finishing auth controller verification
This commit is contained in:
@ -1,29 +1,14 @@
|
||||
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 => {
|
||||
const cookiePayload = ctx.request.headers.get("Cookie");
|
||||
if (!cookiePayload)
|
||||
throw returnErrorResponse(ctx.set, 400, "Bad Request", "No cookies found");
|
||||
|
||||
const cookies = parse(cookiePayload);
|
||||
const cookiesToken = cookies.auth_token!;
|
||||
export const jwtDecode = (payload: string) => {
|
||||
// return payload;
|
||||
if (!payload) throw "JWT decode payload not found";
|
||||
const JWTKey = process.env.JWT_SECRET!;
|
||||
|
||||
try {
|
||||
const decodedToken = jwt.verify(
|
||||
cookiesToken,
|
||||
process.env.JWT_SECRET!
|
||||
) as JWTAuthToken;
|
||||
return decodedToken;
|
||||
const decodedPayload = jwt.verify(payload, JWTKey);
|
||||
return decodedPayload;
|
||||
} catch (error) {
|
||||
throw returnErrorResponse(
|
||||
ctx.set,
|
||||
401,
|
||||
"Unauthorized",
|
||||
"Invalid or expired token"
|
||||
);
|
||||
throw "JWT expired or not valid";
|
||||
}
|
||||
};
|
||||
|
||||
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 { 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(
|
||||
"/legacy",
|
||||
loginWithPassword
|
||||
export const authModule = new Elysia({ prefix: "/auth" })
|
||||
.post("/legacy", 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,
|
||||
} from "../../../helpers/callback/httpResponse";
|
||||
import { createUserRoleService } from "../services/createUserRole.service";
|
||||
import { JWTDecodeToken } from "../../../helpers/http/jwt/decode";
|
||||
import { mainErrorHandler } from "../../../helpers/error/handler";
|
||||
import { createUserRoleSchema } from "../schemas/createUserRole.schema";
|
||||
|
||||
@ -51,7 +50,7 @@ export const createUserRole = async (
|
||||
|
||||
const formData: Prisma.UserRoleUncheckedCreateInput = {
|
||||
...ctx.body,
|
||||
createdBy: JWTDecodeToken(ctx).user.id,
|
||||
createdBy: "daw",
|
||||
};
|
||||
|
||||
try {
|
||||
|
||||
Reference in New Issue
Block a user