
    LCj                         d Z ddlmZ ddlmZmZ ddlmZ ddlm	Z	 ddl
mZ ddlmZ ddlmZ dd	lmZ  ed
      Z ee       ee      fdedz  de	defdZy)aN  
Current-user dependency for JWT-protected endpoints.

Use `current_user: User = Depends(get_current_user)` on any endpoint
that should only be accessible to a logged-in customer (dashboard,
crop plans, notifications). This is distinct from `verify_api_key` in
app/core/security.py, which protects the machine-to-machine API surface.
    )Depends)HTTPAuthorizationCredentials
HTTPBearer)select)AsyncSession)decode_token)AuthenticationError)User)get_db_sessionF)
auto_errorcredentialsNdbreturnc                   K   | t        d      t        | j                  d      }|j                  d      }|st        d      |j	                  t        t              j                  t        j                  |k(               d {   }|j                         }|t        d      |j                  st        d      |S 7 <w)	Nz'Authentication required. Please log in.)messageaccess)expected_typesubzInvalid token payloadzUser account no longer existsz3This account has been deactivated. Contact support.)r	   r   r   getexecuter   r
   whereidscalar_one_or_none	is_active)r   r   payloaduser_idresultusers         BC:\Crop_Prediction\Backend\crop-ai-system\app\core\current_user.pyget_current_userr       s      !*STT;22(KGkk% G!*ABB::fTl00G1CDEEF$$&D|!*IJJ>>!*_``K Fs   BCC	=C)__doc__fastapir   fastapi.securityr   r   
sqlalchemyr   sqlalchemy.ext.asyncior   app.core.auth_tokensr   app.core.exceptionsr	   app.db.modelsr
   app.db.sessionr   bearer_schemer         r   <module>r-      sd     E  / - 3  )e, 8?}7M~.-4 
r,   