auth.py 490 B

12345678910111213141516
  1. import os
  2. from jose import JWTError, jwt
  3. from datetime import datetime, timedelta
  4. # Generate token function
  5. def create_access_token(data: dict, expires_delta: timedelta = None):
  6. to_encode = data.copy()
  7. if expires_delta:
  8. expire = datetime.now() + expires_delta
  9. else:
  10. expire = datetime.now() + timedelta(minutes=15)
  11. to_encode.update({"exp": expire})
  12. encoded_jwt = jwt.encode(to_encode, os.environ["SECRET_KEY"], algorithm="HS256")
  13. return encoded_jwt