import { db } from "./db.ts";
import { issueToken } from "./auth.ts";

const [username, password] = process.argv.slice(2);

if (!username || !password) {
    console.error("Usage: bun run login <username> <password>");
    process.exit(1);
}

const [user] = await db`SELECT id, password_hash FROM users WHERE username = ${username}`;

if (!user || !await Bun.password.verify(password, user.password_hash as string)) {
    console.error("Invalid credentials");
    process.exit(1);
}

console.log(await issueToken(user.id as number));
