|
1 | | -from fastapi import APIRouter, Depends, HTTPException |
| 1 | +from src.utils.utils import get_db |
2 | 2 | from sqlalchemy.orm import Session |
3 | | -from src.schemas.users.users_schema import UserCreate, UserLogin, UserResponse |
| 3 | +from schemas.token_schema import Token |
| 4 | +from utils.security import create_access_token |
4 | 5 | from src.schemas.users.CRUD.create import create_user |
5 | | -from src.schemas.users.CRUD.read import authenticate_user |
6 | | -from src.utils.utils import get_db |
| 6 | +from fastapi import APIRouter, Depends, HTTPException |
| 7 | +from src.schemas.users.CRUD.read import authenticate_user, get_user_by_email |
| 8 | +from src.schemas.users.users_schema import UserCreate, UserLogin, UserResponse |
7 | 9 |
|
8 | 10 | authentication_router = APIRouter(prefix="/auth", tags=["auth"]) |
9 | 11 |
|
10 | 12 |
|
11 | 13 | @authentication_router.post("/register", response_model=UserResponse) |
12 | 14 | def register_user(user: UserCreate, db: Session = Depends(get_db)): |
13 | 15 | """ |
14 | | - Registers a new user in the system. |
| 16 | + Register a new user in the system. |
15 | 17 |
|
16 | 18 | Args: |
17 | 19 | user (UserCreate): The user data to be registered. |
18 | 20 | db (Session, optional): The database session dependency. |
19 | 21 |
|
20 | 22 | Raises: |
21 | | - HTTPException: If a user with the provided email already exists. |
| 23 | + HTTPException: Raised if a user with the provided email already exists. |
22 | 24 |
|
23 | 25 | Returns: |
24 | 26 | UserResponse: The newly created user information. |
25 | 27 | """ |
26 | | - existing = authenticate_user(db, user.email, user.password) |
27 | | - if existing: |
28 | | - raise HTTPException(status_code=400, detail="User already exists.") |
| 28 | + existing_user = get_user_by_email(db, email=user.email) |
| 29 | + if existing_user: |
| 30 | + raise HTTPException( |
| 31 | + status_code=400, detail="User with this email already exists." |
| 32 | + ) |
29 | 33 | return create_user(db, user) |
30 | 34 |
|
31 | 35 |
|
32 | | -@authentication_router.post("/login") |
| 36 | +@authentication_router.post("/login", response_model=Token) |
33 | 37 | def login_user(user: UserLogin, db: Session = Depends(get_db)): |
34 | 38 | """ |
35 | | - Handles user login by verifying credentials. |
| 39 | + Handle user login by verifying credentials and returning a JWT token. |
36 | 40 |
|
37 | 41 | Args: |
38 | | - user (UserLogin): The user login data containing email and password. |
| 42 | + user (UserLogin): The login data containing email and password. |
39 | 43 | db (Session, optional): The database session dependency. |
40 | 44 |
|
41 | | - Returns: |
42 | | - dict: A message indicating successful login. |
43 | | -
|
44 | 45 | Raises: |
45 | | - HTTPException: If the credentials are invalid (status code 401). |
| 46 | + HTTPException: Raised if the credentials are invalid (401). |
| 47 | +
|
| 48 | + Returns: |
| 49 | + dict: A dictionary containing the access token and its type. |
46 | 50 | """ |
47 | 51 | auth_user = authenticate_user(db, user.email, user.password) |
48 | 52 | if not auth_user: |
49 | 53 | raise HTTPException(status_code=401, detail="Invalid credentials.") |
50 | | - # TODO: gerar JWT |
51 | | - return {"msg": "Login successful!"} |
| 54 | + |
| 55 | + # NOVO: Geração do token JWT |
| 56 | + access_token = create_access_token(data={"sub": auth_user.email}) |
| 57 | + |
| 58 | + return {"access_token": access_token, "token_type": "bearer"} |
0 commit comments