Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Allow users to create an account #276

Merged
merged 3 commits into from
Nov 20, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 14 additions & 1 deletion backend/src/api/users.py
Original file line number Diff line number Diff line change
@@ -1,14 +1,27 @@
from fastapi import APIRouter
import logging as log
from passlib.hash import bcrypt
from ..api_types import LoginBody, LoginResponse
from ..api_types import SignupBody, SignupResponse, LoginBody, LoginResponse
from ..db import Database
from ..auth import generate_auth_token
from fastapi.exceptions import HTTPException

router = APIRouter()


@router.post("/users/signup", response_model=SignupResponse | None)
def signup(body: SignupBody):
with Database() as db:
hashed_password = bcrypt.hash(body.password)
query = """INSERT INTO users(username, email, pword, admin) VALUES (%s, %s, %s, false);"""
try:
db.execute(query, [body.username, body.email, hashed_password])
return SignupResponse(error="")
except Exception as e:
log.error(e)
return SignupResponse(error="Server error.")


@router.post("/users/login", response_model=LoginResponse | None)
def login(body: LoginBody):
with Database() as db:
Expand Down
10 changes: 10 additions & 0 deletions backend/src/api_types.py
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,16 @@ class EditBody(CamelModel):
new_description: str | None = None


class SignupBody(CamelModel):
username: str
email: str
password: str


class SignupResponse(CamelModel):
error: str


class LoginBody(CamelModel):
email: str
password: str
Expand Down
Loading
Loading