init
This commit is contained in:
@@ -17,18 +17,23 @@ def verify_password(plain: str, hashed: str) -> bool:
|
||||
return bcrypt.checkpw(plain.encode(), hashed.encode())
|
||||
|
||||
|
||||
def create_access_token(user_id: int, email: str) -> str:
|
||||
def create_access_token(user_id: int, email: str, token_type: str = "admin") -> str:
|
||||
expire = datetime.now(timezone.utc) + timedelta(hours=settings.JWT_EXPIRE_HOURS)
|
||||
payload = {
|
||||
"sub": str(user_id),
|
||||
"email": email,
|
||||
"type": token_type,
|
||||
"exp": expire,
|
||||
}
|
||||
token = jwt.encode(payload, settings.JWT_SECRET, algorithm=settings.JWT_ALGORITHM)
|
||||
logger.info("Created JWT for user_id=%d, email=%s, expires=%s", user_id, email, expire)
|
||||
logger.info("Created JWT (%s) for id=%d, email=%s, expires=%s", token_type, user_id, email, expire)
|
||||
return token
|
||||
|
||||
|
||||
def create_client_token(client_id: int, email: str) -> str:
|
||||
return create_access_token(client_id, email, token_type="client")
|
||||
|
||||
|
||||
def decode_access_token(token: str) -> dict | None:
|
||||
try:
|
||||
payload = jwt.decode(token, settings.JWT_SECRET, algorithms=[settings.JWT_ALGORITHM])
|
||||
|
||||
Reference in New Issue
Block a user