-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcreate_admin.py
More file actions
49 lines (38 loc) · 1.44 KB
/
create_admin.py
File metadata and controls
49 lines (38 loc) · 1.44 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
import os
import sys
from getpass import getpass
from pymongo import MongoClient
from dotenv import load_dotenv
# Add the project root to the Python path to allow importing project modules
sys.path.append(os.path.dirname(os.path.dirname(os.path.abspath(__file__))))
# Now we can import from our project
from security import get_password_hash
# Load environment variables from .env file
load_dotenv()
MONGO_URL = os.getenv("MONGO_URL")
# Connect to the database
client = MongoClient(MONGO_URL)
db = client["memora_db"]
users_collection = db["users"]
def main():
print("--- Create Admin User ---")
# Get admin username and password from user input
username = input("Enter admin username: ").strip()
password = getpass("Enter admin password: ")
# Check if the user already exists
if users_collection.find_one({"username": username}):
print(f"Error: User '{username}' already exists.")
return
# Hash the password
hashed_password = get_password_hash(password)
# Create the new admin user document
admin_user = {
"username": username,
"hashed_password": hashed_password,
"is_admin": True
}
# Insert the new user into the database
result = users_collection.insert_one(admin_user)
print(f"Successfully created admin user '{username}' with ID: {result.inserted_id}")
if __name__ == "__main__":
main()