mirror of
https://github.com/khairul169/home-lab.git
synced 2026-10-02 17:13:57 +07:00
feat: add authentication
This commit is contained in:
@@ -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 });
|
||||
@@ -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`;
|
||||
};
|
||||
Reference in New Issue
Block a user