|
1 | 1 | from typing import Optional
|
2 | 2 |
|
3 |
| -from fastapi import Depends, FastAPI, Request |
| 3 | +from fastapi import Depends, FastAPI, HTTPException, Request |
4 | 4 | from fastapi.responses import RedirectResponse
|
5 | 5 | from fastapi.routing import APIRoute
|
6 | 6 | from nicegui import app, ui
|
|
13 | 13 | user_create_token,
|
14 | 14 | )
|
15 | 15 | from .app.db import User
|
| 16 | +from .app.crud import get_user_count |
16 | 17 | from .app.users import current_active_user
|
17 | 18 | from .configs import settings
|
18 | 19 | from .frontend.add_page import add_page_ui
|
@@ -113,37 +114,51 @@ async def try_login():
|
113 | 114 |
|
114 | 115 | with ui.card().classes("absolute-center shadow-none w-96"):
|
115 | 116 | email = ui.input("email").on("keydown.enter", try_login)
|
116 |
| - password = ui.input("password", password=True, password_toggle_button=True).on( |
117 |
| - "keydown.enter", try_login |
118 |
| - ) |
119 |
| - ui.button("Continue", on_click=try_login).props('padding="xs lg"') |
120 |
| - ui.separator() |
121 |
| - with ui.row(): |
122 |
| - ui.label("New around here?").classes("text-sm") |
123 |
| - ui.link("Create account", target="/register").classes("text-sm") |
| 117 | + email.classes("w-56") |
| 118 | + |
| 119 | + password = ui.input("password", password=True, password_toggle_button=True) |
| 120 | + password.on("keydown.enter", try_login) |
| 121 | + password.classes("w-56") |
| 122 | + |
| 123 | + with ui.element("div").classes("flex mt-4 justify-between items-center"): |
| 124 | + ui.button("Continue", on_click=try_login).props('padding="xs lg"') |
| 125 | + |
| 126 | + if not await get_user_count() >= settings.MAX_USER_COUNT > 0: |
| 127 | + ui.separator() |
| 128 | + with ui.row(): |
| 129 | + ui.label("New around here?").classes("text-sm") |
| 130 | + ui.link("Create account", target="/register").classes("text-sm") |
124 | 131 |
|
125 | 132 |
|
126 | 133 | @ui.page("/register")
|
127 | 134 | async def register():
|
| 135 | + async def validate_max_user_count(): |
| 136 | + if await get_user_count() >= settings.MAX_USER_COUNT > 0: |
| 137 | + raise HTTPException(status_code=404, detail="User limit reached") |
| 138 | + |
128 | 139 | async def try_register():
|
129 | 140 | try:
|
| 141 | + await validate_max_user_count() |
130 | 142 | user = await user_create(email=email.value, password=password.value)
|
131 | 143 | except Exception as e:
|
132 |
| - ui.notify(str(e)) |
| 144 | + ui.notify(str(e), color="negative") |
133 | 145 | else:
|
134 | 146 | token = user and await user_create_token(user)
|
135 | 147 | if token is not None:
|
136 | 148 | app.storage.user.update({"auth_token": token})
|
137 | 149 | ui.navigate.to(app.storage.user.get("referrer_path", "/"))
|
138 | 150 |
|
| 151 | + await validate_max_user_count() |
| 152 | + |
139 | 153 | with ui.card().classes("absolute-center shadow-none w-96"):
|
140 |
| - email = ui.input("email").on("keydown.enter", try_register) |
| 154 | + email = ui.input("email").on("keydown.enter", try_register).classes("w-56") |
141 | 155 | password = ui.input("password", password=True, password_toggle_button=True).on(
|
142 | 156 | "keydown.enter", try_register
|
143 |
| - ) |
| 157 | + ).classes("w-56") |
144 | 158 |
|
145 | 159 | with ui.element("div").classes("flex mt-4 justify-between items-center"):
|
146 |
| - ui.button("Register", on_click=try_register) |
| 160 | + ui.button("Register", on_click=try_register).props('padding="xs lg"') |
| 161 | + |
147 | 162 | ui.separator()
|
148 | 163 | with ui.row():
|
149 | 164 | ui.label("Already have an account?")
|
|
0 commit comments