Spaces:
Sleeping
Sleeping
Commit
·
95ea68c
1
Parent(s):
022479c
helper function create user
Browse files
main.py
CHANGED
|
@@ -14,7 +14,7 @@ from typing import Optional
|
|
| 14 |
load_dotenv()
|
| 15 |
|
| 16 |
SERVER_NAME = "Nexus Authentication Service"
|
| 17 |
-
VERSION = "1.0.
|
| 18 |
|
| 19 |
# Supabase Configuration
|
| 20 |
SUPABASE_URL = os.getenv("SUPABASE_URL")
|
|
@@ -109,22 +109,43 @@ def create_device_token(username: str, user_agent: str) -> str:
|
|
| 109 |
def is_token_expired(expiration_time: datetime) -> bool:
|
| 110 |
return datetime.now(timezone.utc) > expiration_time
|
| 111 |
|
|
|
|
|
|
|
|
|
|
| 112 |
|
| 113 |
-
|
| 114 |
-
|
| 115 |
-
|
| 116 |
-
|
| 117 |
-
|
| 118 |
-
|
| 119 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 120 |
"date_joined": datetime.now(timezone.utc).isoformat(),
|
| 121 |
-
"access_level": "
|
| 122 |
}
|
| 123 |
-
|
| 124 |
-
|
| 125 |
-
|
| 126 |
-
|
| 127 |
-
|
|
|
|
|
|
|
| 128 |
|
| 129 |
|
| 130 |
@root_router.get("/", status_code=status.HTTP_200_OK)
|
|
@@ -135,24 +156,20 @@ async def root():
|
|
| 135 |
# Authentication Routes
|
| 136 |
@auth_router.post("/signup", status_code=status.HTTP_201_CREATED)
|
| 137 |
async def signup(request: SignupRequest):
|
| 138 |
-
|
| 139 |
-
|
| 140 |
-
if existing_user.data:
|
| 141 |
-
raise HTTPException(
|
| 142 |
-
status_code=status.HTTP_400_BAD_REQUEST, detail="Username already exists"
|
| 143 |
-
)
|
| 144 |
|
| 145 |
-
|
| 146 |
-
|
| 147 |
-
|
| 148 |
-
|
| 149 |
-
|
| 150 |
-
|
| 151 |
-
|
| 152 |
-
|
| 153 |
-
|
| 154 |
-
|
| 155 |
-
return {"message": "User created successfully"}
|
| 156 |
|
| 157 |
from datetime import datetime, timedelta, timezone
|
| 158 |
|
|
|
|
| 14 |
load_dotenv()
|
| 15 |
|
| 16 |
SERVER_NAME = "Nexus Authentication Service"
|
| 17 |
+
VERSION = "1.0.5 debug"
|
| 18 |
|
| 19 |
# Supabase Configuration
|
| 20 |
SUPABASE_URL = os.getenv("SUPABASE_URL")
|
|
|
|
| 109 |
def is_token_expired(expiration_time: datetime) -> bool:
|
| 110 |
return datetime.now(timezone.utc) > expiration_time
|
| 111 |
|
| 112 |
+
async def create_user(username: str, password: str, email: Optional[str] = None):
|
| 113 |
+
"""
|
| 114 |
+
Creates a new user in the database.
|
| 115 |
|
| 116 |
+
Args:
|
| 117 |
+
username: The username of the new user.
|
| 118 |
+
password: The password of the new user (hashed).
|
| 119 |
+
email: The email address of the new user (optional).
|
| 120 |
+
|
| 121 |
+
Returns:
|
| 122 |
+
The created user object.
|
| 123 |
+
|
| 124 |
+
Raises:
|
| 125 |
+
HTTPException: If the username already exists.
|
| 126 |
+
"""
|
| 127 |
+
|
| 128 |
+
existing_user = supabase.table("users").select("*").eq("username", username).execute()
|
| 129 |
+
if existing_user.data:
|
| 130 |
+
raise HTTPException(
|
| 131 |
+
status_code=status.HTTP_400_BAD_REQUEST, detail="Username already exists"
|
| 132 |
+
)
|
| 133 |
+
|
| 134 |
+
user_data = {
|
| 135 |
+
"user_id": await generate_numeric_user_id(),
|
| 136 |
+
"username": username,
|
| 137 |
+
"password": password,
|
| 138 |
+
"email": email,
|
| 139 |
"date_joined": datetime.now(timezone.utc).isoformat(),
|
| 140 |
+
"access_level": "default",
|
| 141 |
}
|
| 142 |
+
|
| 143 |
+
inserted_user = supabase.table("users").insert(user_data).execute()
|
| 144 |
+
return inserted_user.data[0]
|
| 145 |
+
|
| 146 |
+
# Initialize system user
|
| 147 |
+
async def init_system_user():
|
| 148 |
+
await create_user(SYSTEM_USER, hash_password(SYSTEM_PASSWORD), None)
|
| 149 |
|
| 150 |
|
| 151 |
@root_router.get("/", status_code=status.HTTP_200_OK)
|
|
|
|
| 156 |
# Authentication Routes
|
| 157 |
@auth_router.post("/signup", status_code=status.HTTP_201_CREATED)
|
| 158 |
async def signup(request: SignupRequest):
|
| 159 |
+
"""
|
| 160 |
+
Signup route handler.
|
|
|
|
|
|
|
|
|
|
|
|
|
| 161 |
|
| 162 |
+
Uses the create_user helper function to create a new user.
|
| 163 |
+
|
| 164 |
+
Args:
|
| 165 |
+
request: Signup request data.
|
| 166 |
+
|
| 167 |
+
Returns:
|
| 168 |
+
A JSON response with a success message.
|
| 169 |
+
"""
|
| 170 |
+
|
| 171 |
+
created_user = await create_user(request.username, hash_password(request.password), request.email)
|
| 172 |
+
return {"message": "User created successfully", "user": created_user}
|
| 173 |
|
| 174 |
from datetime import datetime, timedelta, timezone
|
| 175 |
|