Update code in endpoints/login.post.py
This commit is contained in:
parent
d7983c1f7a
commit
e2760b1911
@ -1,37 +1,34 @@
|
||||
from fastapi import APIRouter, Depends, HTTPException
|
||||
from pydantic import BaseModel
|
||||
from datetime import timedelta
|
||||
from core.database import get_db
|
||||
from sqlalchemy.orm import Session
|
||||
from core.auth import verify_password, create_access_token
|
||||
from models.user import User
|
||||
from fastapi import APIRouter, HTTPException
|
||||
|
||||
contacts = [] # In-memory storage
|
||||
|
||||
router = APIRouter()
|
||||
|
||||
class UserAuth(BaseModel):
|
||||
username: str
|
||||
password: str
|
||||
|
||||
@router.post("/login")
|
||||
async def login(
|
||||
user_data: UserAuth,
|
||||
db: Session = Depends(get_db)
|
||||
@router.post("/contact")
|
||||
async def create_contact(
|
||||
name: str = "John Doe",
|
||||
email: str = "john@example.com",
|
||||
message: str = "Hello there"
|
||||
):
|
||||
"""User authentication endpoint"""
|
||||
user = db.query(User).filter(User.username == user_data.username).first()
|
||||
"""Demo contact endpoint"""
|
||||
contact_id = len(contacts) + 1
|
||||
|
||||
if not user or not verify_password(user_data.password, user.hashed_password):
|
||||
raise HTTPException(status_code=400, detail="Invalid credentials")
|
||||
|
||||
# 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
|
||||
contact = {
|
||||
"id": contact_id,
|
||||
"name": name,
|
||||
"email": email,
|
||||
"message": message,
|
||||
"status": "received"
|
||||
}
|
||||
|
||||
contacts.append(contact)
|
||||
|
||||
return {
|
||||
"message": "Contact message received",
|
||||
"contact_id": contact_id,
|
||||
"status": "success",
|
||||
"next_steps": [
|
||||
"Your message is being reviewed",
|
||||
"We will respond within 24 hours"
|
||||
]
|
||||
}
|
Loading…
x
Reference in New Issue
Block a user