You can download this code by clicking the button below.
This code is now available for download.
This custom function generates a JWT (JSON Web Token) with a payload containing the user ID and expiration time. The token expires in 1 hour.
Technology Stack : PyJWT, datetime
Code Type : Custom function
Code Difficulty : Intermediate
import jwt
import random
from datetime import datetime, timedelta
def generate_jwt_token(user_id, secret_key):
# Create a payload with the user ID and expiration time
payload = {
'user_id': user_id,
'exp': datetime.utcnow() + timedelta(hours=1) # Token expires in 1 hour
}
# Encode the payload with the secret key to create a JWT token
token = jwt.encode(payload, secret_key, algorithm='HS256')
return token
# JSON representation of the code