feat: add authentication

This commit is contained in:
2024-03-16 07:52:42 +07:00
parent ed5c715429
commit 1e8252d9da
28 changed files with 8519 additions and 138 deletions
+14
View File
@@ -0,0 +1,14 @@
import * as jwt from "hono/jwt";
const JWT_SECRET =
process.env.JWT_SECRET || "75396f4ba17e0012d4511b8d4a5bae11c51008a3";
export const generateToken = async (data: any) => {
return jwt.sign(data, JWT_SECRET);
};
export const verifyToken = async (token: string) => {
return jwt.verify(token, JWT_SECRET);
};
export const authMiddleware = jwt.jwt({ secret: JWT_SECRET });
+15
View File
@@ -0,0 +1,15 @@
export const formatBytes = (bytes: number) => {
const sizes = ["Bytes", "KB", "MB", "GB", "TB"];
if (bytes === 0) return "n/a";
const i = parseInt(String(Math.floor(Math.log(bytes) / Math.log(1024))), 10);
if (i === 0) return `${bytes} ${sizes[i]}`;
return `${(bytes / 1024 ** i).toFixed(2)} ${sizes[i]}`;
};
export const secondsToTime = (seconds: number) => {
const d = Math.floor(seconds / (3600 * 24));
const h = Math.floor((seconds % (3600 * 24)) / 3600);
const m = Math.floor((seconds % 3600) / 60);
// const s = Math.floor(seconds % 60);
return `${d}d ${h}h ${m}m`;
};