feur
This commit is contained in:
parent
8b6a86f17a
commit
3aea126342
14 changed files with 205 additions and 67 deletions
24
lib/auth.ts
Normal file
24
lib/auth.ts
Normal file
|
@ -0,0 +1,24 @@
|
|||
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);
|
||||
}
|
|
@ -1,45 +1,36 @@
|
|||
import { z } from "https://deno.land/x/zod@v3.23.8/mod.ts";
|
||||
import {
|
||||
EntryFor,
|
||||
Schema,
|
||||
Store,
|
||||
} from "https://git.barnulf.net/mb/debilus/raw/commit/fc701bec680dd73be29c72164f47ee87fac540c7/mod.ts";
|
||||
import { project_root } from "./utils.ts";
|
||||
|
||||
class Store<D, Schema> {
|
||||
kv;
|
||||
export const db = await Store.open(
|
||||
new Schema({
|
||||
user: {
|
||||
username: "string",
|
||||
password: "string",
|
||||
upvoted_posts: ["many", "post"],
|
||||
upvoted_comments: ["many", "comment"],
|
||||
posts: ["many", "post"],
|
||||
},
|
||||
post: {
|
||||
title: "string",
|
||||
content: "string",
|
||||
upvotes: "number",
|
||||
author: ["one", "user"],
|
||||
comments: ["many", "comment"],
|
||||
},
|
||||
comment: {
|
||||
content: "string",
|
||||
upvotes: "number",
|
||||
author: ["one", "user"],
|
||||
post: ["one", "post"],
|
||||
},
|
||||
}),
|
||||
`${project_root()}/local/db.kv`,
|
||||
);
|
||||
|
||||
private constructor(kv: Deno.Kv) {
|
||||
this.kv = kv;
|
||||
}
|
||||
}
|
||||
|
||||
class Schema<T extends TablesDescriptor<T>> {
|
||||
descriptor;
|
||||
|
||||
constructor(descriptor: T) {
|
||||
this.descriptor = descriptor;
|
||||
}
|
||||
}
|
||||
|
||||
type SchemaDescriptor<T extends TablesDescriptor<T>> = {
|
||||
tables: T;
|
||||
};
|
||||
|
||||
type TablesDescriptor<Self extends TablesDescriptor<Self>> = {
|
||||
[name: string]: TableDescr<Self, typeof name, { [key: string]: z.ZodType }>;
|
||||
};
|
||||
|
||||
type TableDescr<Tables extends TablesDescriptor<Tables>, Name extends keyof Tables, S extends z.ZodRawShape> = {
|
||||
structure: z.ZodObject<S>;
|
||||
relations: Relations<Tables, Name, S>;
|
||||
};
|
||||
|
||||
type Relations<Tables extends TablesDescriptor<Tables>, Name extends keyof Tables, S extends z.ZodRawShape> = {
|
||||
[name in Exclude<keyof Tables, Name>]?: [keyof S, keyof z.infer<Tables[name]["structure"]>];
|
||||
};
|
||||
|
||||
const store = new Schema({
|
||||
"user": {
|
||||
structure: z.object({ feur_id: z.string() }),
|
||||
relations: { "feur": ["_feur_id", "id"] },
|
||||
},
|
||||
"feur": {
|
||||
structure: z.object({ id: z.string(), content: z.string() }),
|
||||
relations: {},
|
||||
},
|
||||
});
|
||||
export type User = EntryFor<typeof db, "user">;
|
||||
export type Post = EntryFor<typeof db, "post">;
|
||||
export type Comment = EntryFor<typeof db, "comment">;
|
||||
|
|
11
lib/utils.ts
11
lib/utils.ts
|
@ -1,9 +1,14 @@
|
|||
import { dirname } from "https://deno.land/std@0.224.0/path/dirname.ts";
|
||||
|
||||
import { CSSProperties } from "https://deno.land/std@0.40.0/types/react.d.ts";
|
||||
|
||||
export function _css(prop: CSSProperties) {
|
||||
return prop;
|
||||
}
|
||||
|
||||
// type wizardry
|
||||
|
||||
export type KeysOfType<T, V> = keyof { [P in keyof T as T[P] extends V ? P : never]: P };
|
||||
export function project_root() {
|
||||
const this_url = new URL(import.meta.url);
|
||||
const this_abs_path = Deno.realPathSync(this_url.pathname);
|
||||
const lib_path = dirname(this_abs_path);
|
||||
return dirname(lib_path);
|
||||
}
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue