feat(auth): implement JWT authentication with secure password hashing
This commit is contained in:
parent
6656a8782c
commit
56df3228a4
7 changed files with 146 additions and 74 deletions
43
backend/hermes/app/api/auth.py
Normal file
43
backend/hermes/app/api/auth.py
Normal file
|
|
@ -0,0 +1,43 @@
|
|||
from fastapi import APIRouter, Depends, HTTPException, Response
|
||||
from sqlalchemy.orm import Session
|
||||
|
||||
from app.core.security import create_access_token, verify_password
|
||||
from app.db.database import get_db
|
||||
from app.models.user import User
|
||||
from app.schemas.user import LoginRequest
|
||||
|
||||
router = APIRouter(
|
||||
prefix="/auth",
|
||||
tags=["Authentication"],
|
||||
)
|
||||
|
||||
|
||||
@router.post("/login")
|
||||
def login(
|
||||
login: LoginRequest,
|
||||
response: Response,
|
||||
db: Session = Depends(get_db),
|
||||
):
|
||||
user = db.query(User).filter(User.username == login.username).first()
|
||||
|
||||
if not user:
|
||||
raise HTTPException(status_code=401, detail="Ungültige Anmeldedaten")
|
||||
|
||||
if not verify_password(login.password, user.password_hash):
|
||||
raise HTTPException(status_code=401, detail="Ungültige Anmeldedaten")
|
||||
|
||||
token = create_access_token(user.username)
|
||||
|
||||
response.set_cookie(
|
||||
key="access_token",
|
||||
value=token,
|
||||
httponly=True,
|
||||
secure=True,
|
||||
samesite="lax",
|
||||
max_age=3600,
|
||||
)
|
||||
|
||||
return {
|
||||
"message": "Login erfolgreich",
|
||||
"user": user.username,
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue