Skip to content

Commit a38dc19

Browse files
committed
More logging
1 parent 40dc5a0 commit a38dc19

File tree

1 file changed

+21
-16
lines changed

1 file changed

+21
-16
lines changed

app/core/security.py

Lines changed: 21 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,12 @@
1010

1111
security = HTTPBearer(auto_error=False) # Authentication is optional!
1212

13+
async def create_user(email: str, password: str):
14+
db.auth.sign_up({
15+
"email": email,
16+
"password": password
17+
})
18+
1319
async def authenticate_user(email: str, password: str):
1420
session = db.auth.sign_in_with_password({
1521
"email": email,
@@ -20,13 +26,15 @@ async def authenticate_user(email: str, password: str):
2026
raise HTTPException(status_code=401, detail="Email not verified. Please check your inpox & spam")
2127

2228
print("User authenticated:", user)
29+
user_id = user.id
30+
print("User ID:", user_id)
2331
# Check if user has credits entry
2432
credits = db.table('credits').select('*').eq('user_id', user.id).execute()
2533
print("User credits:", credits.data)
2634
if not credits.data:
2735
# Create initial credits entry if none exists
2836
db.rpc('add_credits', {
29-
'p_user_id': user.id,
37+
'p_user_id': user_id,
3038
'amount': settings.USER_DAILY_CREDITS
3139
}).execute()
3240
return user
@@ -39,14 +47,6 @@ async def verify_email_code(email: str, code: str):
3947

4048
except Exception as e:
4149
raise HTTPException(status_code=400, detail=str(e))
42-
43-
44-
async def create_user(email: str, password: str):
45-
db.auth.sign_up({
46-
"email": email,
47-
"password": password
48-
})
49-
5050

5151
def create_api_key(user) -> str:
5252
"""Create JWT token for API authentication"""
@@ -112,7 +112,9 @@ async def verify_token(request: Request, credentials: Optional[HTTPAuthorization
112112
"""Verify JWT token and return user info with credits"""
113113
try:
114114
if not credentials:
115+
print("No credentials supplied, continuing as guest...")
115116
user = generate_guest_id(request)
117+
user["email_verified"] = True
116118
else:
117119
decoded = jwt.decode(credentials.credentials, settings.SECRET_KEY, algorithms=["HS256"])
118120

@@ -124,15 +126,15 @@ async def verify_token(request: Request, credentials: Optional[HTTPAuthorization
124126
"email": decoded["email"],
125127
"email_verified": True # API keys are only created for verified users
126128
}
127-
129+
print("User data:", user)
130+
key_id = decoded["key_id"]
131+
print("Key: ", key_id)
128132
# Verify key hasn't been removed
129-
results = db.table('api_keys').select('*').eq('key_id', decoded["key_id"]).execute()
130-
print("Query results:", results.data)
131-
if results.data:
133+
results = db.table("api_keys").select('*').eq('key_id', key_id).execute()
134+
print("Query results:", results)
135+
if results.data and results.data[0]:
132136
key = results.data[0]
133137
print("Key data:", key)
134-
key_using_maybe_results = results.maybe_single()
135-
print("Key using maybe:", key_using_maybe_results)
136138
if not key:
137139
raise HTTPException(status_code=401, detail="API key not found (it may have been removed)")
138140
else:
@@ -142,8 +144,11 @@ async def verify_token(request: Request, credentials: Optional[HTTPAuthorization
142144
if not user["email_verified"]:
143145
raise HTTPException(status_code=403, detail="Email not verified. Please check your inbox & spam")
144146
try:
145-
credits = db.table('credits').select('balance').eq('user_id', user_id).maybe_single().execute()
147+
print("getting credits for user:", user_id)
148+
query = db.table('credits').select('balance').eq('user_id', user_id)
149+
credits = query.maybe_single().execute()
146150
except Exception as e:
151+
print('Exception getting credits:', str(e))
147152
if not credentials:
148153
# if the user is not in the credits table, create it with the daily credit amount
149154
init_guest_data = {

0 commit comments

Comments
 (0)