str: """ Generates a JWT token for user authentication. """ # Define the expiration time (e.g., 30 minutes from now) expire = datetime.datetime.utcnow() + datetime.timedelta(minutes=30) # Define the payload (claims) # Common claims include 'sub' (subject, typically user ID) and 'exp' (expiration time) payload = { "sub": user_id, "exp": expire, "iat": datetime.datetime.utcnow(),# Issued at time "custom_data":"any additional user data" } # Encode the token using the payload, secret key, and algorithm encoded_jwt = jwt.encode( payload=payload, key=SECRET_KEY, algorithm=ALGORITHM ) return encoded_jwt # Ex"> str: """ Generates a JWT token for user authentication. """ # Define the expiration time (e.g., 30 minutes from now) expire = datetime.datetime.utcnow() + datetime.timedelta(minutes=30) # Define the payload (claims) # Common claims include 'sub' (subject, typically user ID) and 'exp' (expiration time) payload = { "sub": user_id, "exp": expire, "iat": datetime.datetime.utcnow(),# Issued at time "custom_data":"any additional user data" } # Encode the token using the payload, secret key, and algorithm encoded_jwt = jwt.encode( payload=payload, key=SECRET_KEY, algorithm=ALGORITHM ) return encoded_jwt # Ex"> str: """ Generates a JWT token for user authentication. """ # Define the expiration time (e.g., 30 minutes from now) expire = datetime.datetime.utcnow() + datetime.timedelta(minutes=30) # Define the payload (claims) # Common claims include 'sub' (subject, typically user ID) and 'exp' (expiration time) payload = { "sub": user_id, "exp": expire, "iat": datetime.datetime.utcnow(),# Issued at time "custom_data":"any additional user data" } # Encode the token using the payload, secret key, and algorithm encoded_jwt = jwt.encode( payload=payload, key=SECRET_KEY, algorithm=ALGORITHM ) return encoded_jwt # Ex">
import jwt
import datetime
import secrets

# 1. Define a secure secret key
# In a real application, store this key securely (e.g., in an environment variable or key vault)
# You can generate a strong key using: secrets.token_urlsafe(32)
SECRET_KEY ="your_very_secure_and_long_secret_key_here"
ALGORITHM ="HS256"

defgenerate_jwt_token(user_id: str) -> str:
"""
    Generates a JWT token for user authentication.
"""
# Define the expiration time (e.g., 30 minutes from now)
expire = datetime.datetime.utcnow() + datetime.timedelta(minutes=30)

# Define the payload (claims)
# Common claims include 'sub' (subject, typically user ID) and 'exp' (expiration time)
payload = {
"sub": user_id,
"exp": expire,
"iat": datetime.datetime.utcnow(),# Issued at time
"custom_data":"any additional user data"
    }

# Encode the token using the payload, secret key, and algorithm
encoded_jwt = jwt.encode(
        payload=payload,
        key=SECRET_KEY,
        algorithm=ALGORITHM
    )

return encoded_jwt

# Example Usage:
user_id_example ="1234567890"
token = generate_jwt_token(user_id_example)
print(f"Generated JWT Token:{token}")

# Example of decoding/verification (for demonstration; backend services would do this)
try:
decoded_payload = jwt.decode(token, SECRET_KEY, algorithms=[ALGORITHM])
    print(f"\\nDecoded Payload:{decoded_payload}")
except jwt.ExpiredSignatureError:
    print("Token has expired")
except jwt.InvalidTokenError:
    print("Invalid token")