Check whether a token has expired
Python · FastAPI · intermediate · greenfield
Adds the expiry check: compares the token's expiry against the current time and returns whether it has passed. Straightforward datetime comparison.
Servers run in various regions; the DB stores expires_at as timezone-aware UTC.
Requirements
- is_expired(token) returns True when the token's expiry has passed.
- `token.expires_at` is a timezone-aware UTC datetime (from the database).
- The comparison must be correct regardless of the server's local timezone.
Files touched
- app/auth/expiry.py
--- app/auth/expiry.py +from datetime import datetime + + +def is_expired(token) -> bool: + return token.expires_at < datetime.now()