all repos — stealth-developers @ 6c1094cd3d39e42163fcd5e1ca3a8522231057ed

pkgs/db/src/utils/index.ts (view raw)

 1
 2
 3
 4
 5
 6
 7
 8
 9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
import { eq } from "drizzle-orm";

import db, { apiKeys } from "..";

export * from "./guild";
export * from "./session";
export * from "./ticket";
export * from "./user";

export async function createToken(name: string) {
	const token = await crypto.subtle.digest("SHA-256", new TextEncoder().encode(name));
	const tokenString = Array.from(new Uint8Array(token))
		.map((b) => b.toString(16).padStart(2, "0"))
		.join("");
	const [insertedToken] = await db.insert(apiKeys).values({ name, token: tokenString }).returning();

	return insertedToken;
}

export async function getToken(token: string) {
	const [apiKey] = await db.select().from(apiKeys).where(eq(apiKeys.token, token));
	return apiKey;
}