twifeur/lib/auth.ts
2024-05-29 06:09:50 +02:00

24 lines
888 B
TypeScript

import { Context } from "https://deno.land/x/hono@v4.3.10/mod.ts";
import { db, User } from "./storage.ts";
import { FeurEnv } from "../main.ts";
import { BlankInput } from "https://deno.land/x/hono@v4.3.10/types.ts";
export async function login(login: string, password: string) {
for await (const user of db.all("user")) {
if (await user.get("username") !== login) continue;
if (await user.get("password") === password) return user;
return null;
}
return null;
}
export async function get_user(context: Context<FeurEnv, string, BlankInput>) {
const id = context.get("session").get("user");
if (typeof id !== "string") return null;
return await db.get("user", id);
}
export function set_user(context: Context<FeurEnv, string, BlankInput>, user: User | null) {
if (user === null) context.get("session").set("user", "");
else context.get("session").set("user", user.id);
}