File tree Expand file tree Collapse file tree 1 file changed +24
-25
lines changed Expand file tree Collapse file tree 1 file changed +24
-25
lines changed Original file line number Diff line number Diff line change 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 )}
You can’t perform that action at this time.
0 commit comments