-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathauth.py
More file actions
53 lines (40 loc) · 1.68 KB
/
auth.py
File metadata and controls
53 lines (40 loc) · 1.68 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
from typing import Any
from fastapi import APIRouter, Depends, HTTPException, status
from fastapi.security import OAuth2PasswordRequestForm
from sqlalchemy.orm import Session
from app.db.database import get_db
from app.schemas.token import Token
from app.schemas.user import UserCreate
from app.services.auth_service import auth_service
from app.services.user_service import user_service
router = APIRouter()
@router.post("/register", response_model=Token)
async def register_user(user_in: UserCreate, db: Session = Depends(get_db)) -> Any:
"""
Register a new user.
"""
# Hash the password
hashed_password = auth_service.get_password_hash(user_in.password)
# Create the user (this includes validation)
user = user_service.create_user(db, user_in, hashed_password)
# Generate access token
access_token = auth_service.generate_access_token(int(user.id))
return {"access_token": access_token, "token_type": "bearer"}
@router.post("/login", response_model=Token)
async def login_for_access_token(
db: Session = Depends(get_db), form_data: OAuth2PasswordRequestForm = Depends()
) -> Any:
"""
OAuth2 compatible token login, get an access token for future requests.
"""
# Authenticate user
user = auth_service.authenticate_user(db, 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"},
)
# Generate access token
access_token = auth_service.generate_access_token(int(user.id))
return {"access_token": access_token, "token_type": "bearer"}