finishing auth controller verification
This commit is contained in:
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