|
| 1 | +import type { Context } from "hono"; |
| 2 | +import { getSignedCookie, setSignedCookie } from "hono/cookie"; |
| 3 | +import { db } from "../../db/client.ts"; |
| 4 | +import { accounts, type SelectAccount } from "../../db/schema.ts"; |
| 5 | +import { HTTPException } from "hono/http-exception"; |
| 6 | +import { eq } from "drizzle-orm"; |
| 7 | +import type { CookieOptions } from "hono/utils/cookie"; |
| 8 | +import { env } from "../../utils/env.ts"; |
| 9 | + |
| 10 | +function GET_COOKIE_SIGN(c: Context): string { |
| 11 | + return env(c, "COOKIE_SIGN"); |
| 12 | +} |
| 13 | + |
| 14 | +const cookie_identifier__browser_id = "howmatch.browser_id"; |
| 15 | +// TODO: make it last forever or smth |
| 16 | +const cookieOptions: CookieOptions = { |
| 17 | + httpOnly: true, |
| 18 | + secure: false, |
| 19 | +}; |
| 20 | + |
| 21 | +type NoAuth = { |
| 22 | + name: string; |
| 23 | +}; |
| 24 | + |
| 25 | +// TODO: implement authentication |
| 26 | +type AuthInfo = { |
| 27 | + kind: "none"; |
| 28 | + info: NoAuth; |
| 29 | +}; |
| 30 | + |
| 31 | +export async function signup(c: Context, auth: AuthInfo): Promise<SelectAccount> { |
| 32 | + const browser_id = await getBrowserID(c); |
| 33 | + const prev = await _findAccount(c, auth); |
| 34 | + if (prev) { |
| 35 | + // already has an account |
| 36 | + return prev; |
| 37 | + } |
| 38 | + const account = ( |
| 39 | + await db(c) |
| 40 | + .insert(accounts) |
| 41 | + .values({ |
| 42 | + id: crypto.randomUUID(), |
| 43 | + browser_id, |
| 44 | + name: auth.info.name, |
| 45 | + }) |
| 46 | + .returning() |
| 47 | + )[0]; |
| 48 | + if (!account) throw new HTTPException(500, { message: "Failed to create account" }); |
| 49 | + return account; |
| 50 | +} |
| 51 | + |
| 52 | +// TODO: implement authentication |
| 53 | +export async function login(c: Context, auth: AuthInfo): Promise<SelectAccount> { |
| 54 | + const acc = await _findAccount(c, auth); |
| 55 | + if (acc) { |
| 56 | + setSignedCookie(c, cookie_identifier__browser_id, acc.browser_id, GET_COOKIE_SIGN(c), cookieOptions); |
| 57 | + return acc; |
| 58 | + } |
| 59 | + throw new HTTPException(404, { message: "account not found" }); |
| 60 | +} |
| 61 | + |
| 62 | +// creates new one if necessary. |
| 63 | +export async function getBrowserID(c: Context): Promise<string> { |
| 64 | + const cookie = await getSignedCookie(c, GET_COOKIE_SIGN(c), cookie_identifier__browser_id); |
| 65 | + if (cookie) { |
| 66 | + return cookie; |
| 67 | + } |
| 68 | + const browser_id = crypto.randomUUID(); |
| 69 | + await setSignedCookie(c, cookie_identifier__browser_id, browser_id, GET_COOKIE_SIGN(c), cookieOptions); |
| 70 | + return browser_id; |
| 71 | +} |
| 72 | + |
| 73 | +// TODO: implement authentication |
| 74 | +async function _findAccount(c: Context, auth: AuthInfo): Promise<SelectAccount | undefined> { |
| 75 | + switch (auth.kind) { |
| 76 | + case "none": { |
| 77 | + const accountsResult = await db(c) |
| 78 | + .select() |
| 79 | + .from(accounts) |
| 80 | + .where(eq(accounts.name, auth.info.name)) |
| 81 | + .limit(1); |
| 82 | + return accountsResult[0]; // findUnique をしたかった |
| 83 | + } |
| 84 | + default: |
| 85 | + auth.kind satisfies never; |
| 86 | + } |
| 87 | +} |
0 commit comments