1111from  app .core  import  security 
1212from  app .core .config  import  settings 
1313from  app .core .db  import  engine 
14- from  app .models  import  TokenPayload , User 
14+ from  app .models  import  TokenPayload , User ,  Patient ,  Menu 
1515
1616reusable_oauth2  =  OAuth2PasswordBearer (
1717    tokenUrl = f"{ settings .API_V1_STR }  
@@ -25,8 +25,6 @@ def get_db() -> Generator[Session, None, None]:
2525
2626SessionDep  =  Annotated [Session , Depends (get_db )]
2727TokenDep  =  Annotated [str , Depends (reusable_oauth2 )]
28- 
29- 
3028def  get_current_user (session : SessionDep , token : TokenDep ) ->  User :
3129    try :
3230        payload  =  jwt .decode (
@@ -55,3 +53,50 @@ def get_current_active_superuser(current_user: CurrentUser) -> User:
5553            status_code = 403 , detail = "The user doesn't have enough privileges" 
5654        )
5755    return  current_user 
56+ 
57+ 
58+ 
59+ def  get_current_patient (session : SessionDep , token : TokenDep ) ->  User :
60+     try :
61+         payload  =  jwt .decode (
62+             token , settings .SECRET_KEY , algorithms = [security .ALGORITHM ]
63+         )
64+         token_data  =  TokenPayload (** payload )
65+     except  (InvalidTokenError , ValidationError ):
66+         raise  HTTPException (
67+             status_code = status .HTTP_403_FORBIDDEN ,
68+             detail = "Could not validate credentials" ,
69+         )
70+     user  =  session .get (User , token_data .sub )
71+     if  not  user :
72+         raise  HTTPException (status_code = 404 , detail = "User not found" )
73+     if  not  user .is_active :
74+         raise  HTTPException (status_code = 400 , detail = "Inactive user" )
75+     return  user 
76+ 
77+ CurrentPatient  =  Annotated [Menu , Depends (get_current_menu )]
78+ 
79+ def  get_current_menu (session : SessionDep , token : TokenDep ) ->  Menu :
80+     try :
81+         payload  =  jwt .decode (
82+             token , settings .SECRET_KEY , algorithms = [security .ALGORITHM ]
83+         )
84+         token_data  =  TokenPayload (** payload )
85+     except  (InvalidTokenError , ValidationError ):
86+         raise  HTTPException (
87+             status_code = status .HTTP_403_FORBIDDEN ,
88+             detail = "Could not validate credentials" ,
89+         )
90+     patient  =  session .get (Menu , token_data .sub )
91+     if  not  patient :
92+         raise  HTTPException (status_code = 404 , detail = "User not found" )
93+     if  not  patient .is_active :
94+         raise  HTTPException (status_code = 400 , detail = "Inactive user" )
95+     return  patient 
96+ 
97+ 
98+ CurrentMenu  =  Annotated [Menu , Depends (get_current_menu )]
99+ 
100+ 
101+ 
102+ 
0 commit comments