#๐Ÿ”’ FastAPI registration/login + HTML

7 messages ยท Page 1 of 1 (latest)

mossy matrix
#
@router.post("/register")
async def register(user: UserCreate, background_tasks: BackgroundTasks):
    existing_user = await find_one_user({"email": user.email})

    if existing_user:
        raise HTTPException(status_code=400, detail="Email already registered")

    hashed_password = bcrypt.hashpw(user.password.encode('utf-8'), bcrypt.gensalt()).decode('utf-8')
    confirmation_token = str(uuid.uuid4())

    new_user = {
        "username": user.username,
        "email": user.email,
        "hashed_password": hashed_password,
        "is_active": False,
        "confirmation_token": confirmation_token
    }
    await write_new_user(new_user)
    background_tasks.add_task(send_confirmation_email, user.email, confirmation_token, config.BASE_URL)
    print(f"{confirmation_token=}")

    return {"message": "Registration successful. Please check your email to confirm your account."}
@router.post("/login")
async def login(form_data: OAuth2PasswordRequestForm = Depends()):
    db_user = await find_one_user({"username": form_data.username})
    if not db_user:
        raise HTTPException(status_code=401, detail="Invalid username or password")

    if not bcrypt.checkpw(form_data.password.encode('utf-8'), db_user["hashed_password"].encode('utf-8')):
        raise HTTPException(status_code=401, detail="Invalid username or password")

    if not db_user["is_active"]:
        raise HTTPException(status_code=400, detail="Email not confirmed")

    access_token_expires = timedelta(minutes=config.ACCESS_TOKEN_EXPIRE_MINUTES)
    access_token = create_access_token(
        data={"sub": db_user["username"]}, expires_delta=access_token_expires
    )
    return {"access_token": access_token, "token_type": "bearer"}
rose nebulaBOT
#

@mossy matrix

Python help channel opened

Remember to:

  • Ask your Python question, not if you can ask or if there's an expert who can help.
  • Show a code sample as text (rather than a screenshot) and the error message, if you've got one.
  • Explain what you expect to happen and what actually happens.

:warning: Do not pip install anything that isn't related to your question, especially if asked to over DMs.

mossy matrix
#

registation.html:

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Registration</title>
    <link rel="stylesheet" href="/static/styles/style.css">
</head>
<body>
    <div class="container">
        <h1>Registration</h1>
        <form action="/register" method="post">
            <input type="text" name="username" placeholder="Username" required>
            <input type="email" name="email" placeholder="Email" required>
            <input type="password" name="password" placeholder="Password" required>
            <button type="submit" class="button">Register</button>
        </form>
        <a href="/" class="button">Back to Home</a>
    </div>
</body>
</html>

login.html:

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Login</title>
    <link rel="stylesheet" href="/static/styles/style.css">
</head>
<body>
    <div class="container">
        <h1>Login</h1>
        <form action="/login" method="post">
            <input type="text" name="username" placeholder="Username" required>
            <input type="password" name="password" placeholder="Password" required>
            <button type="submit" class="button">Login</button>
        </form>
        <a href="/" class="button">Back to Home</a>
    </div>
</body>
</html>
#

i wanna add my html to fast api functions

#

i am using something like on screenshoot, but idk how to do it better with registration/login

rose nebulaBOT
#

@mossy matrix

Python help channel closed

This help channel has been closed and it's no longer possible to send messages here. If your question wasn't answered, feel free to create a new post in #1035199133436354600. To maximize your chances of getting a response, check out this guide on asking good questions.