You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
34 lines
1.2 KiB
34 lines
1.2 KiB
from fastapi import APIRouter,Depends
|
|
from fastapi.security import OAuth2PasswordRequestForm
|
|
from datetime import timedelta
|
|
from fastapi.security import OAuth2PasswordRequestForm
|
|
from fastapi import Depends, FastAPI, HTTPException, status
|
|
from dependencies import *
|
|
from internal.models import Token
|
|
from fastapi.middleware.cors import CORSMiddleware
|
|
router=APIRouter(
|
|
prefix="/users",
|
|
tags=["用户管理"]
|
|
)
|
|
|
|
# 用户登录
|
|
@router.post("/token", response_model=Token)
|
|
async def login_for_access_token(
|
|
form_data: OAuth2PasswordRequestForm = Depends(),
|
|
) -> Token:
|
|
user = authenticate_user(form_data.username, form_data.password)
|
|
if not user:
|
|
raise HTTPException(
|
|
status_code=status.HTTP_401_UNAUTHORIZED,
|
|
detail="Incorrect username or password",
|
|
headers={"WWW-Authenticate": "Bearer"},
|
|
)
|
|
access_token_expires = timedelta(minutes=ACCESS_TOKEN_EXPIRE_MINUTES)
|
|
access_token = create_access_token(
|
|
data={"sub": user.username}, expires_delta=access_token_expires
|
|
)
|
|
return {"access_token": access_token, "token_type": "bearer"}
|
|
|
|
@router.get("/me/", response_model=User)
|
|
async def read_users_me(current_user: User = Depends(get_current_active_user)):
|
|
return current_user
|