Skip to content

Commit cc3f214

Browse files
author
Uttam Singh
committed
Fix bcrypt 72-byte password limit for admin
1 parent 2d5b73b commit cc3f214

File tree

1 file changed

+24
-25
lines changed

1 file changed

+24
-25
lines changed

backend/seed/admin.py.py

Lines changed: 24 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -1,27 +1,26 @@
1-
import os
2-
os.environ.setdefault("DATABASE_URL", "sqlite:///./app.db")
1+
# ✅ Seed Admin User (One-Time Setup)
2+
@router.post("/seed-admin")
3+
def seed_admin(db: Session = Depends(get_db)):
4+
try:
5+
6+
existing = db.query(User).filter(User.email == email).first()
7+
if existing:
8+
return {"ok": True, "note": "Admin already exists"}
39

4-
from app.main import SessionLocal # uses the same engine/session
5-
from app.models.user import User
6-
from passlib.hash import bcrypt
10+
raw_password = "Edme@123"
11+
if len(raw_password.encode("utf-8")) > 72:
12+
raw_password = raw_password[:72]
713

8-
db = SessionLocal()
9-
10-
# delete old admin if exists
11-
old = db.query(User).filter(User.email == "[email protected]").first()
12-
if old:
13-
db.delete(old)
14-
db.commit()
15-
16-
admin = User(
17-
name="Admin",
18-
19-
hashed_password=bcrypt.hash("Edme@123"),
20-
department="Finance",
21-
role="admin",
22-
manager_email=None
23-
)
24-
db.add(admin)
25-
db.commit()
26-
db.close()
27-
print("✅ Admin created: [email protected] / Edme@123")
14+
admin = User(
15+
name="Admin",
16+
email=email,
17+
hashed_password=bcrypt.hash(raw_password),
18+
department="Finance",
19+
role="admin",
20+
manager_email=None,
21+
)
22+
db.add(admin)
23+
db.commit()
24+
return {"ok": True, "note": "Admin created"}
25+
except Exception as e:
26+
return {"ok": False, "error": str(e)}

0 commit comments

Comments
 (0)