Mbonea commited on
Commit
39fc4b3
·
1 Parent(s): 84b44a7

push user data

Browse files
Files changed (1) hide show
  1. App/Users/UserRoutes.py +13 -0
App/Users/UserRoutes.py CHANGED
@@ -13,6 +13,7 @@ from .Model import User
13
  from jose import jwt
14
  from typing import List
15
  from datetime import datetime, timedelta
 
16
  from App.Android.Android import AndroidClient
17
  from App.Android.Schema import RegisterUserRequest as AndroidRegister
18
  from App.Templates.Templates import MessageTemplate
@@ -60,16 +61,28 @@ async def register_user(request: RegisterUserRequest):
60
  "/user/login", response_model=AccessTokenResponse, status_code=status.HTTP_200_OK
61
  )
62
  async def login_user(request: LoginUserRequest):
 
63
  db_user = await User.filter(phoneNumber=request.phoneNumber).first()
 
 
64
  if db_user and db_user.verify_password(request.password):
 
 
 
 
 
 
65
  access_token = create_access_token(
66
  data={
67
  "sub": db_user.phoneNumber,
68
  "locked": db_user.account_locked,
69
  "userId": db_user.id,
 
70
  }
71
  )
72
  return AccessTokenResponse(access_token=access_token, token_type="bearer")
 
 
73
  raise HTTPException(
74
  status_code=status.HTTP_401_UNAUTHORIZED, detail="Invalid credentials"
75
  )
 
13
  from jose import jwt
14
  from typing import List
15
  from datetime import datetime, timedelta
16
+ from App.Subscriptions.Model import Subscription
17
  from App.Android.Android import AndroidClient
18
  from App.Android.Schema import RegisterUserRequest as AndroidRegister
19
  from App.Templates.Templates import MessageTemplate
 
61
  "/user/login", response_model=AccessTokenResponse, status_code=status.HTTP_200_OK
62
  )
63
  async def login_user(request: LoginUserRequest):
64
+ # Find user by phone number
65
  db_user = await User.filter(phoneNumber=request.phoneNumber).first()
66
+
67
+ # Check if user exists and password is correct
68
  if db_user and db_user.verify_password(request.password):
69
+ # Fetch active subscription if it exists
70
+ subscription = await Subscription.filter(user=db_user, active=True).first()
71
+
72
+ # Handle case when no active subscription is found
73
+ subscription_end = subscription.expiration_time if subscription else None
74
+
75
  access_token = create_access_token(
76
  data={
77
  "sub": db_user.phoneNumber,
78
  "locked": db_user.account_locked,
79
  "userId": db_user.id,
80
+ "subscription_end": subscription_end,
81
  }
82
  )
83
  return AccessTokenResponse(access_token=access_token, token_type="bearer")
84
+
85
+ # Raise an error if credentials are invalid
86
  raise HTTPException(
87
  status_code=status.HTTP_401_UNAUTHORIZED, detail="Invalid credentials"
88
  )