Skip to content

Commit

Permalink
authorize.ts throw instead of returning null
Browse files Browse the repository at this point in the history
  • Loading branch information
rgerum committed Jan 28, 2025
1 parent 3a81dba commit 6686c8a
Showing 1 changed file with 9 additions and 4 deletions.
13 changes: 9 additions & 4 deletions src/authorize.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,18 +5,23 @@ import { phpbb_check_hash } from "@/lib/auth/hash_functions2";
export default async function authorize(
credentials: Partial<Record<"password" | "username", unknown>>,
) {
if (typeof credentials.password !== "string") return null;
if (typeof credentials.username !== "string") return null;
if (typeof credentials.password !== "string")
throw new Error("Invalid credentials.");
if (typeof credentials.username !== "string")
throw new Error("Invalid credentials.");

let res2 =
await sql`SELECT * FROM users WHERE LOWER(name) = ${credentials.username.toLowerCase() || ""} AND activated`;

if (res2.length === 0) {
return null;
throw new Error("Invalid credentials.");
}
let user = res2[0];

let correct = phpbb_check_hash(credentials.password, user.password);

if (!correct) {
return null;
throw new Error("Invalid credentials.");
}

return {
Expand Down

0 comments on commit 6686c8a

Please sign in to comment.