Spaces:
Sleeping
Sleeping
Commit
·
d022d1e
1
Parent(s):
616cbd3
Ensure the generated user_id is unique
Browse files
main.py
CHANGED
|
@@ -113,7 +113,12 @@ def create_device_token(username: str, user_agent: str) -> str:
|
|
| 113 |
def is_token_expired(expiration_time: datetime) -> bool:
|
| 114 |
return datetime.now(timezone.utc) > expiration_time
|
| 115 |
|
| 116 |
-
async def create_user(
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 117 |
"""
|
| 118 |
Creates a new user in the database.
|
| 119 |
|
|
@@ -121,6 +126,7 @@ async def create_user(username: str, password: str, email: Optional[str] = None,
|
|
| 121 |
username: The username of the new user.
|
| 122 |
password: The password of the new user (hashed).
|
| 123 |
email: The email address of the new user (optional).
|
|
|
|
| 124 |
|
| 125 |
Returns:
|
| 126 |
The created user object.
|
|
@@ -129,14 +135,23 @@ async def create_user(username: str, password: str, email: Optional[str] = None,
|
|
| 129 |
HTTPException: If the username already exists.
|
| 130 |
"""
|
| 131 |
|
|
|
|
| 132 |
existing_user = supabase.table("users").select("*").eq("username", username).execute()
|
| 133 |
if existing_user.data:
|
| 134 |
raise HTTPException(
|
| 135 |
status_code=status.HTTP_400_BAD_REQUEST, detail="Username already exists"
|
| 136 |
)
|
| 137 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 138 |
user_data = {
|
| 139 |
-
"user_id":
|
| 140 |
"username": username,
|
| 141 |
"password": password,
|
| 142 |
"email": email,
|
|
@@ -144,6 +159,7 @@ async def create_user(username: str, password: str, email: Optional[str] = None,
|
|
| 144 |
"access_level": access_level or "default",
|
| 145 |
}
|
| 146 |
|
|
|
|
| 147 |
inserted_user = supabase.table("users").insert(user_data).execute()
|
| 148 |
return inserted_user.data[0]
|
| 149 |
|
|
|
|
| 113 |
def is_token_expired(expiration_time: datetime) -> bool:
|
| 114 |
return datetime.now(timezone.utc) > expiration_time
|
| 115 |
|
| 116 |
+
async def create_user(
|
| 117 |
+
username: str,
|
| 118 |
+
password: str,
|
| 119 |
+
email: Optional[str] = None,
|
| 120 |
+
access_level: Optional[str] = None,
|
| 121 |
+
):
|
| 122 |
"""
|
| 123 |
Creates a new user in the database.
|
| 124 |
|
|
|
|
| 126 |
username: The username of the new user.
|
| 127 |
password: The password of the new user (hashed).
|
| 128 |
email: The email address of the new user (optional).
|
| 129 |
+
access_level: The access level of the new user (optional).
|
| 130 |
|
| 131 |
Returns:
|
| 132 |
The created user object.
|
|
|
|
| 135 |
HTTPException: If the username already exists.
|
| 136 |
"""
|
| 137 |
|
| 138 |
+
# Check if the username already exists
|
| 139 |
existing_user = supabase.table("users").select("*").eq("username", username).execute()
|
| 140 |
if existing_user.data:
|
| 141 |
raise HTTPException(
|
| 142 |
status_code=status.HTTP_400_BAD_REQUEST, detail="Username already exists"
|
| 143 |
)
|
| 144 |
|
| 145 |
+
# Ensure the generated user_id is unique
|
| 146 |
+
while True:
|
| 147 |
+
user_id = generate_numeric_user_id()
|
| 148 |
+
existing_user_id = supabase.table("users").select("*").eq("user_id", user_id).execute()
|
| 149 |
+
if not existing_user_id.data:
|
| 150 |
+
break
|
| 151 |
+
|
| 152 |
+
# Prepare user data
|
| 153 |
user_data = {
|
| 154 |
+
"user_id": user_id,
|
| 155 |
"username": username,
|
| 156 |
"password": password,
|
| 157 |
"email": email,
|
|
|
|
| 159 |
"access_level": access_level or "default",
|
| 160 |
}
|
| 161 |
|
| 162 |
+
# Insert the user into the database
|
| 163 |
inserted_user = supabase.table("users").insert(user_data).execute()
|
| 164 |
return inserted_user.data[0]
|
| 165 |
|