diff --git a/src/pages/convert.tsx b/src/pages/convert.tsx
index fe4c325..6ae9825 100644
--- a/src/pages/convert.tsx
+++ b/src/pages/convert.tsx
@@ -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,
},
);
diff --git a/src/pages/deleteFile.tsx b/src/pages/deleteFile.tsx
index 665d802..9599bec 100644
--- a/src/pages/deleteFile.tsx
+++ b/src/pages/deleteFile.tsx
@@ -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 },
);
diff --git a/src/pages/download.tsx b/src/pages/download.tsx
index e5d7576..f8c2b90 100644
--- a/src/pages/download.tsx
+++ b/src/pages/download.tsx
@@ -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,
});
diff --git a/src/pages/history.tsx b/src/pages/history.tsx
index b6188a6..1b85461 100644
--- a/src/pages/history.tsx
+++ b/src/pages/history.tsx
@@ -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()
>
);
+ }, {
+ auth: true
});
diff --git a/src/pages/listConverters.tsx b/src/pages/listConverters.tsx
index a2b99be..4d507bb 100644
--- a/src/pages/listConverters.tsx
+++ b/src/pages/listConverters.tsx
@@ -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 (
<>
@@ -77,4 +68,6 @@ export const listConverters = new Elysia()
>
);
+ }, {
+ auth: true
});
diff --git a/src/pages/results.tsx b/src/pages/results.tsx
index c881f55..aff0256 100644
--- a/src/pages/results.tsx
+++ b/src/pages/results.tsx
@@ -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()
>
);
- })
- .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 ;
- });
+ }, { auth: true });
diff --git a/src/pages/root.tsx b/src/pages/root.tsx
index 3231cc7..7640dc1 100644
--- a/src/pages/root.tsx
+++ b/src/pages/root.tsx
@@ -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()
>
);
+ }, {
+ cookie: t.Cookie({
+ auth: t.Optional(t.String()),
+ jobId: t.Optional(t.String()),
+ })
});
diff --git a/src/pages/upload.tsx b/src/pages/upload.tsx
index 1a2e634..eeaae1c 100644
--- a/src/pages/upload.tsx
+++ b/src/pages/upload.tsx
@@ -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 },
);
diff --git a/src/pages/user.tsx b/src/pages/user.tsx
index 3e34827..76d289e 100644
--- a/src/pages/user.tsx
+++ b/src/pages/user.tsx
@@ -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()
>
);
- })
+ }, { 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()
>
);
+ }, {
+ auth: true
})
.post(
"/account",
@@ -505,5 +513,6 @@ export const user = new Elysia()
newPassword: t.MaybeEmpty(t.String()),
password: t.String(),
}),
+ cookie: "session"
},
);