Update code in endpoints/login.post.py
This commit is contained in:
parent
ce085b9c8f
commit
4068611f4d
@ -1,37 +1,24 @@
|
|||||||
from fastapi import APIRouter, Depends, HTTPException
|
from fastapi import APIRouter, Depends, HTTPException, status
|
||||||
from pydantic import BaseModel
|
|
||||||
from datetime import timedelta
|
|
||||||
from core.database import get_db
|
|
||||||
from sqlalchemy.orm import Session
|
from sqlalchemy.orm import Session
|
||||||
from core.auth import verify_password, create_access_token
|
from typing import List
|
||||||
from models.user import User
|
from core.database import get_db
|
||||||
|
from models.proxy import Proxy
|
||||||
|
from schemas.proxy import ProxySchema, ProxyLogin
|
||||||
|
from helpers.proxy_helpers import authenticate_proxy, create_proxy_session
|
||||||
|
|
||||||
router = APIRouter()
|
router = APIRouter()
|
||||||
|
|
||||||
class UserAuth(BaseModel):
|
@router.post("/login", status_code=200, response_model=ProxySchema)
|
||||||
username: str
|
async def login_proxy(
|
||||||
password: str
|
proxy_data: ProxyLogin,
|
||||||
|
|
||||||
@router.post("/login")
|
|
||||||
async def login(
|
|
||||||
user_data: UserAuth,
|
|
||||||
db: Session = Depends(get_db)
|
db: Session = Depends(get_db)
|
||||||
):
|
):
|
||||||
"""User authentication endpoint"""
|
proxy_session = authenticate_proxy(db, proxy_data)
|
||||||
user = db.query(User).filter(User.username == user_data.username).first()
|
if not proxy_session:
|
||||||
|
raise HTTPException(
|
||||||
|
status_code=status.HTTP_401_UNAUTHORIZED,
|
||||||
|
detail="Invalid credentials"
|
||||||
|
)
|
||||||
|
|
||||||
if not user or not verify_password(user_data.password, user.hashed_password):
|
session = create_proxy_session(db, proxy_session)
|
||||||
raise HTTPException(status_code=400, detail="Invalid credentials")
|
return session
|
||||||
|
|
||||||
# Generate token with expiration
|
|
||||||
access_token = create_access_token(
|
|
||||||
data={"sub": user.id},
|
|
||||||
expires_delta=timedelta(hours=1)
|
|
||||||
)
|
|
||||||
|
|
||||||
return {
|
|
||||||
"access_token": access_token,
|
|
||||||
"token_type": "bearer",
|
|
||||||
"user_id": user.id,
|
|
||||||
"username": user.username
|
|
||||||
}
|
|
Loading…
x
Reference in New Issue
Block a user