middleware fixed, yet some work have to be done

This commit is contained in:
Saimon8420 2025-02-04 18:38:55 +06:00
parent 86e38900fc
commit 90f67dfa03
6 changed files with 20 additions and 18 deletions

View file

@ -114,7 +114,7 @@ export const verifyToken = async (context: any) => {
try { try {
// if token is in cookie, verify it // if token is in cookie, verify it
// const token_cookie = context.cookie.access_token.value; // const token_cookie = context.cookie.access_token.value;
const verify = await verifyAuth(context.cookie, context.request); const verify = await verifyAuth(context.cookie);
return verify; return verify;

View file

@ -61,7 +61,7 @@ export const createProject = async (userId: string) => {
console.log(error.message); console.log(error.message);
return { status: 500, message: "An error occurred while creating projects" } return { status: 500, message: "An error occurred while creating projects" }
} }
} };
export const updateProject = async (id: string, body: any) => { export const updateProject = async (id: string, body: any) => {
try { try {

View file

@ -8,12 +8,9 @@ export const projectRoutes = new Elysia({
detail: { detail: {
description: "Routes for managing projects", description: "Routes for managing projects",
} }
}).derive(async ({ cookie, request }) => { }).derive(async ({ cookie }) => {
const authData = await verifyAuth(cookie, request); const authData = await verifyAuth(cookie);
if (authData.status !== 200) { return { authData }; // Inject into context
return { authData };
}
return { userId: authData.userId }; // Inject into context
}); });
projectRoutes.get("/each/:project_id", ({ params: { project_id } }) => getEachProjects(project_id), { 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), { projectRoutes.get("/", async ({ authData }: any) => {
body: t.Object({ if (authData.status !== 200)
userId: t.String() return authData;
}) else {
const userId = authData.userId;
const response = await getAllProjects(userId);
return response;
}
}); });
projectRoutes.post("/create", ({ userId }: any) => createProject(userId)); projectRoutes.post("/create", ({ userId }: any) => createProject(userId));

View file

@ -8,8 +8,8 @@ export const uploadRoutes = new Elysia({
detail: { detail: {
description: "Routes for uploading and managing photos", description: "Routes for uploading and managing photos",
} }
}).derive(async ({ cookie, request }) => { }).derive(async ({ cookie }) => {
const authData = await verifyAuth(cookie, request); const authData = await verifyAuth(cookie);
if (authData.status !== 200) { if (authData.status !== 200) {
return { authData }; return { authData };
} }

View file

@ -41,6 +41,7 @@ const app = new Elysia()
.onError(({ code, error }) => { .onError(({ code, error }) => {
if (code === 'NOT_FOUND') if (code === 'NOT_FOUND')
return 'Not Found :('; return 'Not Found :(';
console.log("hello");
console.error(error) console.error(error)
}); });

View file

@ -5,7 +5,7 @@ import { users } from "../db/schema";
import { db } from "../db"; import { db } from "../db";
import { eq } from "drizzle-orm"; import { eq } from "drizzle-orm";
export const verifyAuth = async (cookie: any, request: Request) => { export const verifyAuth = async (cookie: any) => {
try { try {
const access_cookie = cookie?.access_token?.value; const access_cookie = cookie?.access_token?.value;
const refresh_cookie = cookie?.refresh_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 }; return { status: 200, message: "Token verified successfully", userId: findUser[0].id };
} }
else { 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 // Query the user from the database
const findUser = await db.select().from(users).where(eq(users.id, verify_cookie.userId)); const findUser = await db.select().from(users).where(eq(users.id, verify_cookie.userId));
if (findUser.length === 0 || findUser[0].refresh_token !== refresh_cookie) { if (findUser.length === 0 || findUser[0].refresh_token !== refresh_cookie) {
throw { status: 401, message: "Unauthorized" }; return { status: 401, message: "Unauthorized" };
} }
else { else {
// generate access token // generate access token
@ -50,7 +50,7 @@ export const verifyAuth = async (cookie: any, request: Request) => {
} }
else { else {
throw { status: 401, message: "No token provided" }; return { status: 401, message: "Unauthorized" };
} }
} catch (error: any) { } catch (error: any) {