Nattyboi commited on
Commit
5dbdab3
·
1 Parent(s): e96bd82

added user details

Browse files
Files changed (3) hide show
  1. app.py +20 -2
  2. tokenManagement.py +26 -21
  3. utils.py +36 -4
app.py CHANGED
@@ -283,7 +283,8 @@ Parameters:
283
  @app.post("/auth/login",tags=["Authentication"])
284
  def login(user:UserBody):
285
  user ={"email":user.email,"password":user.password,"firstName":user.firstName,"lastName":user.lastName}
286
- user_id= login_user(db_uri=MONGO_URI,db_name="crayonics",collection_name="Users",document=user)
 
287
 
288
  if user_id != False:
289
  refreshToken=create_refreshToken(db_uri=MONGO_URI,user_id=user_id)
@@ -337,7 +338,6 @@ def refresh_access_token(refresh_token:Token, authorization: str = Header(...)):
337
  # Here, you would validate the token (e.g., check with a JWT library)
338
  decoded_user_id,decoded_access_token = decode_jwt(token)
339
  is_valid = verify_access_token(db_uri=MONGO_URI, user_id=decoded_user_id, access_token=decoded_access_token)
340
- print(decoded_user_id,decoded_access_token)
341
  if is_valid != True: # Example check
342
  raise HTTPException(status_code=401, detail="Invalid token")
343
  new_access_token = create_accessToken(db_uri=MONGO_URI,user_id=decoded_user_id,refresh_token=refresh_token.refreshToken)
@@ -346,6 +346,21 @@ def refresh_access_token(refresh_token:Token, authorization: str = Header(...)):
346
  return {"accessToken":newly_encoded_access_token}
347
 
348
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
349
 
350
 
351
  @app.get("/protected-route")
@@ -360,3 +375,6 @@ def protected_route(authorization: str = Header(...)):
360
  raise HTTPException(status_code=401, detail="Invalid token")
361
 
362
  return {"message": "Access granted", "verification": "verified"}
 
 
 
 
283
  @app.post("/auth/login",tags=["Authentication"])
284
  def login(user:UserBody):
285
  user ={"email":user.email,"password":user.password,"firstName":user.firstName,"lastName":user.lastName}
286
+ print(user)
287
+ user_id= login_user(db_uri=MONGO_URI,db_name="crayonics",collection_name="users",document=user)
288
 
289
  if user_id != False:
290
  refreshToken=create_refreshToken(db_uri=MONGO_URI,user_id=user_id)
 
338
  # Here, you would validate the token (e.g., check with a JWT library)
339
  decoded_user_id,decoded_access_token = decode_jwt(token)
340
  is_valid = verify_access_token(db_uri=MONGO_URI, user_id=decoded_user_id, access_token=decoded_access_token)
 
341
  if is_valid != True: # Example check
342
  raise HTTPException(status_code=401, detail="Invalid token")
343
  new_access_token = create_accessToken(db_uri=MONGO_URI,user_id=decoded_user_id,refresh_token=refresh_token.refreshToken)
 
346
  return {"accessToken":newly_encoded_access_token}
347
 
348
 
349
+ @app.get("/user/user-details",tags=["user"])
350
+ def get_user_details(authorization: str = Header(...)):
351
+ # Extract the token from the Authorization header (Bearer token)
352
+ token = authorization.split("Bearer ")[-1]
353
+
354
+ # Here, you would validate the token (e.g., check with a JWT library)
355
+ decoded_user_id,decoded_access_token = decode_jwt(token)
356
+ is_valid = verify_access_token(db_uri=MONGO_URI, user_id=decoded_user_id, access_token=decoded_access_token)
357
+ if is_valid != True: # Example check
358
+ raise HTTPException(status_code=401, detail="Invalid token")
359
+ doc = {"user_id":decoded_user_id}
360
+ user_info = user_details_func(db_uri=MONGO_URI,document=doc)
361
+ return { "userInfo": user_info}
362
+
363
+
364
 
365
 
366
  @app.get("/protected-route")
 
375
  raise HTTPException(status_code=401, detail="Invalid token")
376
 
377
  return {"message": "Access granted", "verification": "verified"}
378
+
379
+
380
+
tokenManagement.py CHANGED
@@ -77,7 +77,7 @@ def create_refreshToken(db_uri: str, user_id: str) -> str:
77
  # Insert the document
78
  result = collection.insert_one({"user_id":user_id,"current_time":current_time,"expire_at":expire_at,"previous_access_token":"None"})
79
  streaks_doc={}
80
- streaks_doc['user_id'] = user_id
81
  streaks_manager(db_uri=db_uri,document=streaks_doc)
82
  client.close()
83
  return str(result.inserted_id)
@@ -121,29 +121,35 @@ def verify_access_token(db_uri: str, user_id: str, access_token: str) -> bool:
121
  client = MongoClient(db_uri)
122
  db = client["crayonics"]
123
  collection = db["AccessToken"]
124
- doc = collection.find_one({"user_id":user_id})
 
 
125
 
126
-
127
- if doc==None:
128
- return False
129
- else:
130
- if str(doc['_id']) == access_token:
131
- if isexpired(doc['expire_at']):
132
- streaks_doc={}
133
- streaks_doc['user_id'] = user_id
134
- streaks_manager(db_uri=db_uri,document=streaks_doc)
135
- return False
 
 
 
 
 
 
 
136
  else:
 
 
137
  streaks_doc={}
138
- streaks_doc['user_id'] = user_id
139
  streaks_manager(db_uri=db_uri,document=streaks_doc)
140
- return True
141
- else:
142
- streaks_doc={}
143
- streaks_doc['user_id'] = user_id
144
- streaks_manager(db_uri=db_uri,document=streaks_doc)
145
- return False
146
-
147
 
148
 
149
 
@@ -180,4 +186,3 @@ def logout_func(db_uri: str, refresh_token: str) -> str:
180
  # Close the connection
181
 
182
 
183
-
 
77
  # Insert the document
78
  result = collection.insert_one({"user_id":user_id,"current_time":current_time,"expire_at":expire_at,"previous_access_token":"None"})
79
  streaks_doc={}
80
+ streaks_doc['user_id'] = str(user_id)
81
  streaks_manager(db_uri=db_uri,document=streaks_doc)
82
  client.close()
83
  return str(result.inserted_id)
 
121
  client = MongoClient(db_uri)
122
  db = client["crayonics"]
123
  collection = db["AccessToken"]
124
+ docs = collection.find({"user_id":user_id})
125
+ for doc in docs:
126
+ print("doc=", doc,"user access token =" , access_token)
127
 
128
+ if doc==None:
129
+ return False
130
+ else:
131
+ if str(doc['_id']) == access_token:
132
+ print("accesstoke is correct")
133
+ if isexpired(doc['expire_at'])!=False:
134
+ print("isexpired!=False")
135
+ streaks_doc={}
136
+ streaks_doc['user_id'] = str(user_id)
137
+ streaks_manager(db_uri=db_uri,document=streaks_doc)
138
+ pass
139
+ else:
140
+ streaks_doc={}
141
+ print("isexpired!=True")
142
+ streaks_doc['user_id'] = str(user_id)
143
+ streaks_manager(db_uri=db_uri,document=streaks_doc)
144
+ return True
145
  else:
146
+ print("doc=", str(doc['_id']),"user access token =" , access_token)
147
+ print("accesstoken is wrong")
148
  streaks_doc={}
149
+ streaks_doc['user_id'] = str(user_id)
150
  streaks_manager(db_uri=db_uri,document=streaks_doc)
151
+ pass
152
+ return False
 
 
 
 
 
153
 
154
 
155
 
 
186
  # Close the connection
187
 
188
 
 
utils.py CHANGED
@@ -1,3 +1,5 @@
 
 
1
  import requests
2
  from pymongo import MongoClient
3
  from password import *
@@ -201,21 +203,51 @@ def login_user(db_uri: str, db_name: str, collection_name: str, document: dict)
201
  collection = db[collection_name]
202
 
203
  # Insert the document
204
- s = collection.find_one({"email":document.get("email")})
205
  print(s)
206
  print(document.get('email'))
207
  if s==None:
208
- return "User Doesn;t exist"
209
  else:
210
 
211
  if check_password(password=document['password'],hashed_password=s['password']):
212
- streaks_doc['user_id'] = s.get("_id")
213
  streaks_manager(db_uri=db_uri,document=streaks_doc)
214
  return str(s['_id'])
215
  else:
216
- return "Wrong Password"
217
  # Close the connection
218
 
219
 
220
 
221
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ from bson import ObjectId
2
+ import json
3
  import requests
4
  from pymongo import MongoClient
5
  from password import *
 
203
  collection = db[collection_name]
204
 
205
  # Insert the document
206
+ s = collection.find_one({"email":document["email"]})
207
  print(s)
208
  print(document.get('email'))
209
  if s==None:
210
+ return False
211
  else:
212
 
213
  if check_password(password=document['password'],hashed_password=s['password']):
214
+ streaks_doc['user_id'] = str(s["_id"])
215
  streaks_manager(db_uri=db_uri,document=streaks_doc)
216
  return str(s['_id'])
217
  else:
218
+ return False
219
  # Close the connection
220
 
221
 
222
 
223
 
224
+
225
+
226
+ def user_details_func(db_uri: str, document: dict) -> str:
227
+ streaks_doc={}
228
+
229
+ # Connect to MongoDB
230
+ client = MongoClient(db_uri)
231
+ db = client["crayonics"]
232
+ collection = db["users"]
233
+ streaks_collection = db["Streaks"]
234
+
235
+ # Insert the document
236
+ doc = collection.find_one({"_id":ObjectId(document.get("user_id"))})
237
+
238
+ if doc==None:
239
+ return None
240
+ else:
241
+ streaks_collection_doc = streaks_collection.find_one(filter={"user_id":document.get("user_id")})
242
+ streaks_doc['user_id'] = document.get("user_id")
243
+ streaks_manager(db_uri=db_uri,document=streaks_doc)
244
+
245
+ streaks_collection_doc.pop("_id")
246
+ doc['user_id'] = str(doc['_id'])
247
+ doc.pop('_id')
248
+ doc.pop('password')
249
+ streaks_collection_doc.pop('user_id')
250
+ doc['streak_dates'] =streaks_collection_doc['streak_dates']
251
+ return doc
252
+
253
+