feat: register endpoint + frontend login fix for updated backend schema
Browse files
Backend/app/api/v1/endpoints/auth.py
CHANGED
|
@@ -2,26 +2,21 @@ from fastapi import APIRouter, HTTPException, Depends, status
|
|
| 2 |
from sqlalchemy.ext.asyncio import AsyncSession
|
| 3 |
from sqlalchemy import select
|
| 4 |
from datetime import timedelta
|
| 5 |
-
from app.schema import UserCreate, Token
|
|
|
|
| 6 |
from app.models import User
|
| 7 |
from app.core import verify_password, get_password_hash, create_access_token
|
| 8 |
from app.api.deps import get_db
|
| 9 |
from app.config import settings
|
| 10 |
-
from pydantic import BaseModel
|
| 11 |
-
|
| 12 |
-
router = APIRouter()
|
| 13 |
|
| 14 |
|
|
|
|
| 15 |
|
| 16 |
-
class LoginRequest(BaseModel):
|
| 17 |
-
# This tells FastAPI to expect a JSON body with these keys
|
| 18 |
-
username: str
|
| 19 |
-
password: str
|
| 20 |
|
| 21 |
@router.post("/register", response_model=dict)
|
| 22 |
async def register(user: UserCreate, db: AsyncSession = Depends(get_db)):
|
| 23 |
try:
|
| 24 |
-
result = await db.execute(select(User).filter(User.
|
| 25 |
existing_user = result.scalar_one_or_none()
|
| 26 |
|
| 27 |
if existing_user:
|
|
@@ -31,6 +26,7 @@ async def register(user: UserCreate, db: AsyncSession = Depends(get_db)):
|
|
| 31 |
)
|
| 32 |
new_user = User(
|
| 33 |
username=user.username,
|
|
|
|
| 34 |
hashed_password=get_password_hash(user.password)
|
| 35 |
)
|
| 36 |
db.add(new_user)
|
|
@@ -48,12 +44,12 @@ async def register(user: UserCreate, db: AsyncSession = Depends(get_db)):
|
|
| 48 |
@router.post("/login", response_model=Token)
|
| 49 |
async def login(request: LoginRequest, db: AsyncSession = Depends(get_db)):
|
| 50 |
# Access the data via the request object
|
| 51 |
-
|
| 52 |
password = request.password
|
| 53 |
|
| 54 |
# The rest of your logic remains the same
|
| 55 |
try:
|
| 56 |
-
result = await db.execute(select(User).filter(User.
|
| 57 |
user = result.scalar_one_or_none()
|
| 58 |
|
| 59 |
if not user or not verify_password(password, user.hashed_password):
|
|
|
|
| 2 |
from sqlalchemy.ext.asyncio import AsyncSession
|
| 3 |
from sqlalchemy import select
|
| 4 |
from datetime import timedelta
|
| 5 |
+
from app.schema import UserCreate, Token, LoginRequest
|
| 6 |
+
# from app.schema.models import LoginRequest
|
| 7 |
from app.models import User
|
| 8 |
from app.core import verify_password, get_password_hash, create_access_token
|
| 9 |
from app.api.deps import get_db
|
| 10 |
from app.config import settings
|
|
|
|
|
|
|
|
|
|
| 11 |
|
| 12 |
|
| 13 |
+
router = APIRouter()
|
| 14 |
|
|
|
|
|
|
|
|
|
|
|
|
|
| 15 |
|
| 16 |
@router.post("/register", response_model=dict)
|
| 17 |
async def register(user: UserCreate, db: AsyncSession = Depends(get_db)):
|
| 18 |
try:
|
| 19 |
+
result = await db.execute(select(User).filter(User.email == user.email))
|
| 20 |
existing_user = result.scalar_one_or_none()
|
| 21 |
|
| 22 |
if existing_user:
|
|
|
|
| 26 |
)
|
| 27 |
new_user = User(
|
| 28 |
username=user.username,
|
| 29 |
+
email=user.email,
|
| 30 |
hashed_password=get_password_hash(user.password)
|
| 31 |
)
|
| 32 |
db.add(new_user)
|
|
|
|
| 44 |
@router.post("/login", response_model=Token)
|
| 45 |
async def login(request: LoginRequest, db: AsyncSession = Depends(get_db)):
|
| 46 |
# Access the data via the request object
|
| 47 |
+
email = request.email
|
| 48 |
password = request.password
|
| 49 |
|
| 50 |
# The rest of your logic remains the same
|
| 51 |
try:
|
| 52 |
+
result = await db.execute(select(User).filter(User.email==email))
|
| 53 |
user = result.scalar_one_or_none()
|
| 54 |
|
| 55 |
if not user or not verify_password(password, user.hashed_password):
|
Backend/app/models/tables.py
CHANGED
|
@@ -17,5 +17,5 @@ class User(Base):
|
|
| 17 |
|
| 18 |
id: Mapped[int] = mapped_column(primary_key=True, index=True)
|
| 19 |
username: Mapped[str] = mapped_column(String(50), unique=True, index=True)
|
| 20 |
-
|
| 21 |
hashed_password: Mapped[str] = mapped_column(String(255))
|
|
|
|
| 17 |
|
| 18 |
id: Mapped[int] = mapped_column(primary_key=True, index=True)
|
| 19 |
username: Mapped[str] = mapped_column(String(50), unique=True, index=True)
|
| 20 |
+
email: Mapped[str] = mapped_column(String(100), unique=True, index=True)
|
| 21 |
hashed_password: Mapped[str] = mapped_column(String(255))
|
Backend/app/schema/__init__.py
CHANGED
|
@@ -1,3 +1,3 @@
|
|
| 1 |
-
from app.schema.models import StudentCreate, StudentUpdate, StudentResponse, UserCreate, Token
|
| 2 |
|
| 3 |
-
__all__ = ["StudentCreate", "StudentUpdate", "StudentResponse", "UserCreate", "Token"]
|
|
|
|
| 1 |
+
from app.schema.models import StudentCreate, StudentUpdate, StudentResponse, UserCreate, Token, LoginRequest
|
| 2 |
|
| 3 |
+
__all__ = ["StudentCreate", "StudentUpdate", "StudentResponse", "UserCreate", "Token", "LoginRequest"]
|
Backend/app/schema/models.py
CHANGED
|
@@ -28,6 +28,7 @@ class StudentResponse(StudentBase):
|
|
| 28 |
|
| 29 |
class UserCreate(BaseModel):
|
| 30 |
username: str = Field(..., min_length=3, max_length=50)
|
|
|
|
| 31 |
password: str = Field(..., min_length=6, max_length=72)
|
| 32 |
|
| 33 |
@field_validator('password')
|
|
@@ -35,7 +36,10 @@ class UserCreate(BaseModel):
|
|
| 35 |
if len(v.encode("utf-8")) > 72:
|
| 36 |
raise ValueError('Password cannot exceed 72 bytes')
|
| 37 |
return v
|
| 38 |
-
|
|
|
|
|
|
|
|
|
|
| 39 |
|
| 40 |
class Token(BaseModel):
|
| 41 |
access_token: str
|
|
|
|
| 28 |
|
| 29 |
class UserCreate(BaseModel):
|
| 30 |
username: str = Field(..., min_length=3, max_length=50)
|
| 31 |
+
email: EmailStr = Field(...)
|
| 32 |
password: str = Field(..., min_length=6, max_length=72)
|
| 33 |
|
| 34 |
@field_validator('password')
|
|
|
|
| 36 |
if len(v.encode("utf-8")) > 72:
|
| 37 |
raise ValueError('Password cannot exceed 72 bytes')
|
| 38 |
return v
|
| 39 |
+
|
| 40 |
+
class LoginRequest(BaseModel):
|
| 41 |
+
email: EmailStr
|
| 42 |
+
password: str
|
| 43 |
|
| 44 |
class Token(BaseModel):
|
| 45 |
access_token: str
|
Backend/requirements.txt
ADDED
|
@@ -0,0 +1,10 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
fastapi
|
| 2 |
+
uvicorn[standard]
|
| 3 |
+
sqlalchemy
|
| 4 |
+
asyncpg
|
| 5 |
+
pydantic
|
| 6 |
+
pydantic-settings
|
| 7 |
+
passlib[argon2]
|
| 8 |
+
python-jose[cryptography]
|
| 9 |
+
email-validator
|
| 10 |
+
|