chore: use auth macro instead of checking it on every path

This commit is contained in:
C4illin
2025-10-02 17:29:58 +02:00
parent 13d9ce09a4
commit c9b65c7652
9 changed files with 69 additions and 113 deletions

View File

@@ -74,7 +74,7 @@ export const convert = new Elysia().use(userService).post(
db.query("UPDATE jobs SET status = 'completed' WHERE id = ?1").run(jobId.value);
}
// delete all uploaded files in userUploadsDir
// Delete all uploaded files in userUploadsDir
// rmSync(userUploadsDir, { recursive: true, force: true });
})
.catch((error) => {
@@ -89,5 +89,6 @@ export const convert = new Elysia().use(userService).post(
convert_to: t.String(),
file_names: t.String(),
}),
auth: true,
},
);

View File

@@ -7,16 +7,7 @@ import { userService } from "./user";
export const deleteFile = new Elysia().use(userService).post(
"/delete",
async ({ body, redirect, jwt, cookie: { auth, jobId } }) => {
if (!auth?.value) {
return redirect(`${WEBROOT}/login`, 302);
}
const user = await jwt.verify(auth.value);
if (!user) {
return redirect(`${WEBROOT}/login`, 302);
}
async ({ body, redirect, cookie: { jobId }, user }) => {
if (!jobId?.value) {
return redirect(`${WEBROOT}/`, 302);
}
@@ -37,5 +28,5 @@ export const deleteFile = new Elysia().use(userService).post(
message: "File deleted successfully.",
};
},
{ body: t.Object({ filename: t.String() }) },
{ body: t.Object({ filename: t.String() }), auth: true },
);

View File

@@ -1,5 +1,5 @@
import path from "node:path";
import { Elysia } from "elysia";
import { Elysia, t } from 'elysia'
import sanitize from "sanitize-filename";
import * as tar from "tar";
import { outputDir } from "..";
@@ -11,16 +11,7 @@ export const download = new Elysia()
.use(userService)
.get(
"/download/:userId/:jobId/:fileName",
async ({ params, jwt, redirect, cookie: { auth } }) => {
if (!auth?.value) {
return redirect(`${WEBROOT}/login`, 302);
}
const user = await jwt.verify(auth.value);
if (!user) {
return redirect(`${WEBROOT}/login`, 302);
}
async ({ params, redirect, user }) => {
const job = await db
.query("SELECT * FROM jobs WHERE user_id = ? AND id = ?")
.get(user.id, params.jobId);
@@ -28,7 +19,7 @@ export const download = new Elysia()
if (!job) {
return redirect(`${WEBROOT}/results`, 302);
}
// parse from url encoded string
// parse from URL encoded string
const userId = decodeURIComponent(params.userId);
const jobId = decodeURIComponent(params.jobId);
const fileName = sanitize(decodeURIComponent(params.fileName));
@@ -36,17 +27,11 @@ export const download = new Elysia()
const filePath = `${outputDir}${userId}/${jobId}/${fileName}`;
return Bun.file(filePath);
},
{
auth: true,
}
)
.get("/archive/:userId/:jobId", async ({ params, jwt, redirect, cookie: { auth } }) => {
if (!auth?.value) {
return redirect(`${WEBROOT}/login`, 302);
}
const user = await jwt.verify(auth.value);
if (!user) {
return redirect(`${WEBROOT}/login`, 302);
}
.get("/archive/:userId/:jobId", async ({ params, redirect, user }) => {
const job = await db
.query("SELECT * FROM jobs WHERE user_id = ? AND id = ?")
.get(user.id, params.jobId);
@@ -71,4 +56,6 @@ export const download = new Elysia()
["."],
);
return Bun.file(outputTar);
}, {
auth: true,
});

View File

@@ -9,16 +9,11 @@ import { userService } from "./user";
export const history = new Elysia()
.use(userService)
.get("/history", async ({ jwt, redirect, cookie: { auth } }) => {
.get("/history", async ({ jwt, redirect, user }) => {
if (HIDE_HISTORY) {
return redirect(`${WEBROOT}/`, 302);
}
if (!auth?.value) {
return redirect(`${WEBROOT}/login`, 302);
}
const user = await jwt.verify(auth.value);
if (!user) {
return redirect(`${WEBROOT}/login`, 302);
}
@@ -32,7 +27,7 @@ export const history = new Elysia()
job.files_detailed = files;
}
// filter out jobs with no files
// Filter out jobs with no files
userJobs = userJobs.filter((job) => job.num_files > 0);
return (
@@ -213,4 +208,6 @@ export const history = new Elysia()
</>
</BaseHtml>
);
}, {
auth: true
});

View File

@@ -8,16 +8,7 @@ import { userService } from "./user";
export const listConverters = new Elysia()
.use(userService)
.get("/converters", async ({ jwt, redirect, cookie: { auth } }) => {
if (!auth?.value) {
return redirect(`${WEBROOT}/login`, 302);
}
const user = await jwt.verify(auth.value);
if (!user) {
return redirect(`${WEBROOT}/login`, 302);
}
.get("/converters", async () => {
return (
<BaseHtml webroot={WEBROOT} title="ConvertX | Converters">
<>
@@ -77,4 +68,6 @@ export const listConverters = new Elysia()
</>
</BaseHtml>
);
}, {
auth: true
});

View File

@@ -136,21 +136,12 @@ function ResultsArticle({
export const results = new Elysia()
.use(userService)
.get("/results/:jobId", async ({ params, jwt, set, redirect, cookie: { auth, job_id } }) => {
if (!auth?.value) {
return redirect(`${WEBROOT}/login`, 302);
}
.get("/results/:jobId", async ({ params, jwt, set, redirect, cookie: { job_id }, user }) => {
if (job_id?.value) {
// clear the job_id cookie since we are viewing the results
// Clear the job_id cookie since we are viewing the results
job_id.remove();
}
const user = await jwt.verify(auth.value);
if (!user) {
return redirect(`${WEBROOT}/login`, 302);
}
const job = db
.query("SELECT * FROM jobs WHERE user_id = ? AND id = ?")
.as(Jobs)
@@ -186,22 +177,13 @@ export const results = new Elysia()
</>
</BaseHtml>
);
})
.post("/progress/:jobId", async ({ jwt, set, params, redirect, cookie: { auth, job_id } }) => {
if (!auth?.value) {
return redirect(`${WEBROOT}/login`, 302);
}
}, { auth: true })
.post("/progress/:jobId", async ({ jwt, set, params, cookie: { job_id }, user }) => {
if (job_id?.value) {
// clear the job_id cookie since we are viewing the results
// Clear the job_id cookie since we are viewing the results
job_id.remove();
}
const user = await jwt.verify(auth.value);
if (!user) {
return redirect(`${WEBROOT}/login`, 302);
}
const job = db
.query("SELECT * FROM jobs WHERE user_id = ? AND id = ?")
.as(Jobs)
@@ -222,4 +204,4 @@ export const results = new Elysia()
.all(params.jobId);
return <ResultsArticle user={user} job={job} files={files} outputPath={outputPath} />;
});
}, { auth: true });

View File

@@ -1,7 +1,7 @@
import { randomInt } from "node:crypto";
import { Html } from "@elysiajs/html";
import { JWTPayloadSpec } from "@elysiajs/jwt";
import { Elysia } from "elysia";
import { Elysia, t } from "elysia";
import { BaseHtml } from "../components/base";
import { Header } from "../components/header";
import { getAllTargets } from "../converters/main";
@@ -65,7 +65,7 @@ export const root = new Elysia()
user.id &&
(Number.parseInt(user.id) < 2 ** 24 || !ALLOW_UNAUTHENTICATED)
) {
// make sure user exists in db
// Make sure user exists in db
const existingUser = db.query("SELECT * FROM users WHERE id = ?").as(User).get(user.id);
if (!existingUser) {
@@ -240,4 +240,9 @@ export const root = new Elysia()
</>
</BaseHtml>
);
}, {
cookie: t.Cookie({
auth: t.Optional(t.String()),
jobId: t.Optional(t.String()),
})
});

View File

@@ -6,16 +6,7 @@ import { userService } from "./user";
export const upload = new Elysia().use(userService).post(
"/upload",
async ({ body, redirect, jwt, cookie: { auth, jobId } }) => {
if (!auth?.value) {
return redirect(`${WEBROOT}/login`, 302);
}
const user = await jwt.verify(auth.value);
if (!user) {
return redirect(`${WEBROOT}/login`, 302);
}
async ({ body, redirect, user, cookie: { jobId } }) => {
if (!jobId?.value) {
return redirect(`${WEBROOT}/`, 302);
}
@@ -44,5 +35,5 @@ export const upload = new Elysia().use(userService).post(
message: "Files uploaded successfully.",
};
},
{ body: t.Object({ file: t.Files() }) },
{ body: t.Object({ file: t.Files() }), auth: true },
);

View File

@@ -32,28 +32,37 @@ export const userService = new Elysia({ name: "user/service" })
email: t.String(),
password: t.String(),
}),
session: t.Cookie({
auth: t.String(),
jobId: t.Optional(t.String()),
}),
optionalSession: t.Cookie({
auth: t.Optional(t.String()),
jobId: t.Optional(t.String()),
})
})
.macro({
isSignIn(enabled: boolean) {
if (!enabled) return;
.macro("auth", {
cookie: "session", async resolve({
status, jwt, cookie: { auth }
}) {
if (!auth.value) {
return status(401, {
success: false,
message: 'Unauthorized'
})
}
const user = await jwt.verify(auth.value);
if (!user) {
return status(401, {
success: false,
message: 'Unauthorized'
})
}
return {
async beforeHandle({ status, jwt, cookie: { auth } }) {
if (auth?.value) {
const user = await jwt.verify(auth.value);
return {
success: true,
user,
};
}
return status(401, {
success: false,
message: "Unauthorized",
});
},
success: true,
user
};
},
}
});
export const user = new Elysia()
@@ -303,7 +312,8 @@ export const user = new Elysia()
</>
</BaseHtml>
);
})
}, { body: "signIn", cookie: "optionalSession" }
)
.post(
"/login",
async function handler({ body, set, redirect, jwt, cookie: { auth } }) {
@@ -363,11 +373,7 @@ export const user = new Elysia()
return redirect(`${WEBROOT}/login`, 302);
})
.get("/account", async ({ jwt, redirect, cookie: { auth } }) => {
if (!auth?.value) {
return redirect(`${WEBROOT}/`);
}
const user = await jwt.verify(auth.value);
.get("/account", async ({ user, redirect }) => {
if (!user) {
return redirect(`${WEBROOT}/`, 302);
@@ -441,6 +447,8 @@ export const user = new Elysia()
</>
</BaseHtml>
);
}, {
auth: true
})
.post(
"/account",
@@ -505,5 +513,6 @@ export const user = new Elysia()
newPassword: t.MaybeEmpty(t.String()),
password: t.String(),
}),
cookie: "session"
},
);