use simpler dotenv file

This commit is contained in:
Osman Faruk Bayram 2025-05-05 16:13:41 +03:00
parent 171a94f174
commit 948eb75416
3 changed files with 4 additions and 11 deletions

View file

@ -6,6 +6,7 @@ from pydantic import BaseModel
from fastapi import Depends, HTTPException from fastapi import Depends, HTTPException
from typing import Annotated, Optional from typing import Annotated, Optional
from fastapi.security import OAuth2PasswordBearer from fastapi.security import OAuth2PasswordBearer
from passlib.context import CryptContext
import jwt import jwt
from sqlmodel import SQLModel, Field, Session, select from sqlmodel import SQLModel, Field, Session, select
from pydantic.networks import EmailStr from pydantic.networks import EmailStr
@ -93,7 +94,7 @@ def create_access_token(
to_encode = data.copy() to_encode = data.copy()
expire = datetime.now(timezone.utc) + expires_delta expire = datetime.now(timezone.utc) + expires_delta
to_encode.update({"exp": expire}) to_encode.update({"exp": expire})
encoded_jwt = jwt.encode(to_encode, SECRET_KEY, algorithm=ALGORITHM) encoded_jwt = jwt.encode(to_encode, SECRET_KEY, algorithm="HS256")
return encoded_jwt return encoded_jwt
@ -118,7 +119,7 @@ async def get_current_user(
headers={"WWW-Authenticate": "Bearer"}, headers={"WWW-Authenticate": "Bearer"},
) )
try: try:
payload = jwt.decode(token, SECRET_KEY, algorithms=[ALGORITHM]) payload = jwt.decode(token, SECRET_KEY, algorithms=["HS256"])
token_data = TokenData(**payload) token_data = TokenData(**payload)
username: Optional[str] = payload.get("sub") username: Optional[str] = payload.get("sub")
if username is None: if username is None:

View file

@ -49,7 +49,7 @@ async def login_for_access_token(
detail="Incorrect username or password", detail="Incorrect username or password",
headers={"WWW-Authenticate": "Bearer"}, headers={"WWW-Authenticate": "Bearer"},
) )
access_token_expires = timedelta(minutes=ACCESS_TOKEN_EXPIRE_MINUTES) access_token_expires = timedelta(minutes=30)
access_token = create_access_token( access_token = create_access_token(
data={"sub": user.username, "role": user.role, 'status': user.status}, expires_delta=access_token_expires data={"sub": user.username, "role": user.role, 'status': user.status}, expires_delta=access_token_expires
) )

View file

@ -29,15 +29,7 @@ def get_session_db():
yield session yield session
### SECRET KEY ### ### SECRET KEY ###
SECRET_KEY = os.getenv("SECRET_KEY")
ALGORITHM = os.getenv("ALGORITHM")
ACCESS_TOKEN_EXPIRE_MINUTES = int(os.getenv("ACCESS_TOKEN_EXPIRE_MINUTES"))
pwd_context = CryptContext(schemes=[f"{os.getenv('CRYPTO_TYPE')}"], deprecated="auto")
origins = [ origins = [
"http://localhost", "http://localhost",
"http://localhost:8080", "http://localhost:8080",