diff --git a/src/api/auth/auth.controller.ts b/src/api/auth/auth.controller.ts index ea1e26d..4cebe85 100644 --- a/src/api/auth/auth.controller.ts +++ b/src/api/auth/auth.controller.ts @@ -114,7 +114,7 @@ export const verifyToken = async (context: any) => { try { // if token is in cookie, verify it // const token_cookie = context.cookie.access_token.value; - const verify = await verifyAuth(context.cookie, context.request); + const verify = await verifyAuth(context.cookie); return verify; diff --git a/src/api/project/project.controller.ts b/src/api/project/project.controller.ts index 423019b..8bab7da 100644 --- a/src/api/project/project.controller.ts +++ b/src/api/project/project.controller.ts @@ -61,7 +61,7 @@ export const createProject = async (userId: string) => { console.log(error.message); return { status: 500, message: "An error occurred while creating projects" } } -} +}; export const updateProject = async (id: string, body: any) => { try { diff --git a/src/api/project/project.route.ts b/src/api/project/project.route.ts index f1d9f3b..21454ce 100644 --- a/src/api/project/project.route.ts +++ b/src/api/project/project.route.ts @@ -8,12 +8,9 @@ export const projectRoutes = new Elysia({ detail: { description: "Routes for managing projects", } -}).derive(async ({ cookie, request }) => { - const authData = await verifyAuth(cookie, request); - if (authData.status !== 200) { - return { authData }; - } - return { userId: authData.userId }; // Inject into context +}).derive(async ({ cookie }) => { + const authData = await verifyAuth(cookie); + return { authData }; // Inject into context }); projectRoutes.get("/each/:project_id", ({ params: { project_id } }) => getEachProjects(project_id), { @@ -22,10 +19,14 @@ projectRoutes.get("/each/:project_id", ({ params: { project_id } }) => getEachPr }) }); -projectRoutes.get("/", ({ userId }: any) => getAllProjects(userId), { - body: t.Object({ - userId: t.String() - }) +projectRoutes.get("/", async ({ authData }: any) => { + if (authData.status !== 200) + return authData; + else { + const userId = authData.userId; + const response = await getAllProjects(userId); + return response; + } }); projectRoutes.post("/create", ({ userId }: any) => createProject(userId)); diff --git a/src/api/upload/upload.route.ts b/src/api/upload/upload.route.ts index 3da6d14..4cd3d70 100644 --- a/src/api/upload/upload.route.ts +++ b/src/api/upload/upload.route.ts @@ -8,8 +8,8 @@ export const uploadRoutes = new Elysia({ detail: { description: "Routes for uploading and managing photos", } -}).derive(async ({ cookie, request }) => { - const authData = await verifyAuth(cookie, request); +}).derive(async ({ cookie }) => { + const authData = await verifyAuth(cookie); if (authData.status !== 200) { return { authData }; } diff --git a/src/app.ts b/src/app.ts index 1f9bd79..ea6c69f 100644 --- a/src/app.ts +++ b/src/app.ts @@ -41,6 +41,7 @@ const app = new Elysia() .onError(({ code, error }) => { if (code === 'NOT_FOUND') return 'Not Found :('; + console.log("hello"); console.error(error) }); diff --git a/src/middlewares/auth.middlewares.ts b/src/middlewares/auth.middlewares.ts index d7c7664..41cf6ef 100644 --- a/src/middlewares/auth.middlewares.ts +++ b/src/middlewares/auth.middlewares.ts @@ -5,7 +5,7 @@ import { users } from "../db/schema"; import { db } from "../db"; import { eq } from "drizzle-orm"; -export const verifyAuth = async (cookie: any, request: Request) => { +export const verifyAuth = async (cookie: any) => { try { const access_cookie = cookie?.access_token?.value; const refresh_cookie = cookie?.refresh_token?.value; @@ -19,7 +19,7 @@ export const verifyAuth = async (cookie: any, request: Request) => { return { status: 200, message: "Token verified successfully", userId: findUser[0].id }; } else { - throw { status: 401, message: "Unauthorized" }; + return { status: 401, message: "Unauthorized" }; } } @@ -30,7 +30,7 @@ export const verifyAuth = async (cookie: any, request: Request) => { // Query the user from the database const findUser = await db.select().from(users).where(eq(users.id, verify_cookie.userId)); if (findUser.length === 0 || findUser[0].refresh_token !== refresh_cookie) { - throw { status: 401, message: "Unauthorized" }; + return { status: 401, message: "Unauthorized" }; } else { // generate access token @@ -50,7 +50,7 @@ export const verifyAuth = async (cookie: any, request: Request) => { } else { - throw { status: 401, message: "No token provided" }; + return { status: 401, message: "Unauthorized" }; } } catch (error: any) {