Skip to content

Commit

Permalink
fix bug
Browse files Browse the repository at this point in the history
  • Loading branch information
jxxghp committed Jun 19, 2024
1 parent 631df4c commit 0fb12c7
Show file tree
Hide file tree
Showing 3 changed files with 10 additions and 4 deletions.
8 changes: 6 additions & 2 deletions app/api/endpoints/login.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
from app.core.security import get_password_hash
from app.db import get_db
from app.db.models.user import User
from app.helper.sites import SitesHelper
from app.log import logger
from app.utils.web import WebUtils

Expand Down Expand Up @@ -58,17 +59,20 @@ async def login_access_token(
elif user and not user.is_active:
raise HTTPException(status_code=403, detail="用户未启用")
logger.info(f"用户 {user.name} 登录成功!")
level = SitesHelper().auth_level
return schemas.Token(
access_token=security.create_access_token(
userid=user.id,
username=user.name,
super_user=user.is_superuser,
expires_delta=timedelta(minutes=settings.ACCESS_TOKEN_EXPIRE_MINUTES)
expires_delta=timedelta(minutes=settings.ACCESS_TOKEN_EXPIRE_MINUTES),
level=level
),
token_type="bearer",
super_user=user.is_superuser,
user_name=user.name,
avatar=user.avatar
avatar=user.avatar,
level=level
)


Expand Down
5 changes: 3 additions & 2 deletions app/core/security.py
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@

def create_access_token(
userid: Union[str, Any], username: str, super_user: bool = False,
expires_delta: timedelta = None
expires_delta: timedelta = None, level: int = 1
) -> str:
if expires_delta:
expire = datetime.utcnow() + expires_delta
Expand All @@ -42,7 +42,8 @@ def create_access_token(
"exp": expire,
"sub": str(userid),
"username": username,
"super_user": super_user
"super_user": super_user,
"level": level
}
encoded_jwt = jwt.encode(to_encode, settings.SECRET_KEY, algorithm=ALGORITHM)
return encoded_jwt
Expand Down
1 change: 1 addition & 0 deletions app/schemas/token.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ class Token(BaseModel):
super_user: bool
user_name: str
avatar: Optional[str] = None
level: int = 1


class TokenPayload(BaseModel):
Expand Down

0 comments on commit 0fb12c7

Please sign in to comment.