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

@ -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 });