mirror of
https://github.com/R0m1k3/CollectFlow.git
synced 2026-10-21 22:18:08 +02:00
feat: Implement NextAuth for credentials-based authentication and add initial admin user seeding logic.
This commit is contained in:
1 parent
cfd079fbf5
commit
a03fd64b5d
2 files changed
+25
-7
No files matched your search
@@ -1,18 +1,34 @@
|
|||||||
import { db } from "@/db";
|
import { db } from "@/db";
|
||||||
import { users } from "@/db/schema";
|
import { users } from "@/db/schema";
|
||||||
import { hashPassword } from "./auth-logic";
|
import { hashPassword } from "./auth-logic";
|
||||||
import { count } from "drizzle-orm";
|
import { count, eq, sql } from "drizzle-orm";
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Checks if the users table is empty.
|
* Checks if the users table is empty.
|
||||||
* If so, creates a default admin account (admin/admin).
|
* If so, creates a default admin account (admin/admin).
|
||||||
|
* Also ensures the table exists (auto-repair).
|
||||||
*/
|
*/
|
||||||
export async function ensureAdminExists() {
|
export async function ensureAdminExists() {
|
||||||
try {
|
try {
|
||||||
const [userCount] = await db.select({ value: count() }).from(users);
|
// 1. Auto-repair: Ensure table exists
|
||||||
|
await db.execute(sql`
|
||||||
|
CREATE TABLE IF NOT EXISTS users (
|
||||||
|
id SERIAL PRIMARY KEY,
|
||||||
|
username VARCHAR(50) NOT NULL UNIQUE,
|
||||||
|
password_hash TEXT NOT NULL,
|
||||||
|
role VARCHAR(20) NOT NULL DEFAULT 'user',
|
||||||
|
created_at TIMESTAMP DEFAULT NOW()
|
||||||
|
);
|
||||||
|
`);
|
||||||
|
|
||||||
if (userCount.value === 0) {
|
// 2. Check if admin specifically exists
|
||||||
console.log("BMAD: Initializing default admin account (admin/admin)...");
|
const adminUser = await db.select()
|
||||||
|
.from(users)
|
||||||
|
.where(eq(users.username, "admin"))
|
||||||
|
.limit(1);
|
||||||
|
|
||||||
|
if (adminUser.length === 0) {
|
||||||
|
console.log("BMAD: Default admin account not found. Initializing (admin/admin)...");
|
||||||
await db.insert(users).values({
|
await db.insert(users).values({
|
||||||
username: "admin",
|
username: "admin",
|
||||||
passwordHash: hashPassword("admin"),
|
passwordHash: hashPassword("admin"),
|
||||||
@@ -21,9 +37,7 @@ export async function ensureAdminExists() {
|
|||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
} catch (err) {
|
} catch (err) {
|
||||||
// If table doesn't exist, we might need auto-repair like session_snapshots
|
console.error("BMAD: CRITICAL Error checking/seeding users table.", err);
|
||||||
// But for users, it's better to let drizzle handled or handle it here if needed.
|
|
||||||
console.error("BMAD: Error checking/seeding users table.", err);
|
|
||||||
}
|
}
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
@@ -26,9 +26,13 @@ export const { handlers, auth, signIn, signOut } = NextAuth({
|
|||||||
.from(users)
|
.from(users)
|
||||||
.where(eq(users.username, credentials.username as string));
|
.where(eq(users.username, credentials.username as string));
|
||||||
|
|
||||||
|
console.log(`BMAD: Auth attempt for "${credentials.username}". User found: ${!!user}`);
|
||||||
|
|
||||||
if (!user) return null;
|
if (!user) return null;
|
||||||
|
|
||||||
const isValid = verifyPassword(credentials.password as string, user.passwordHash);
|
const isValid = verifyPassword(credentials.password as string, user.passwordHash);
|
||||||
|
console.log(`BMAD: Password valid for "${credentials.username}": ${isValid}`);
|
||||||
|
|
||||||
if (!isValid) return null;
|
if (!isValid) return null;
|
||||||
|
|
||||||
return {
|
return {
|
||||||
|
|||||||
Reference in new issue
Block a user