summaryrefslogtreecommitdiffstats
path: root/authentication/endpoints.py
diff options
context:
space:
mode:
Diffstat (limited to 'authentication/endpoints.py')
-rw-r--r--authentication/endpoints.py10
1 files changed, 9 insertions, 1 deletions
diff --git a/authentication/endpoints.py b/authentication/endpoints.py
index 6877383..3af49fc 100644
--- a/authentication/endpoints.py
+++ b/authentication/endpoints.py
@@ -6,6 +6,7 @@ from uuid import uuid7
6 6
7from fastapi import FastAPI, Request, Form, Query 7from fastapi import FastAPI, Request, Form, Query
8from pydantic import BaseModel, ValidationError, model_validator, EmailStr 8from pydantic import BaseModel, ValidationError, model_validator, EmailStr
9from sqlalchemy import func
9from sqlmodel import select 10from sqlmodel import select
10from starlette.authentication import requires 11from starlette.authentication import requires
11from starlette.middleware.authentication import AuthenticationMiddleware 12from starlette.middleware.authentication import AuthenticationMiddleware
@@ -106,10 +107,17 @@ class RegisterForm(BaseModel):
106 return self 107 return self
107 108
108 109
110def is_first_run() -> bool:
111 with get_session_context() as session:
112 user_count = session.exec(select(func.count(User.id)))
113 return user_count.first() == 0
114
115
109async def base_register(request: Request, form: RegisterForm | None = None, errors: dict[str, str] | None = None): 116async def base_register(request: Request, form: RegisterForm | None = None, errors: dict[str, str] | None = None):
110 return templates.TemplateResponse(request, 'register.html', context={ 117 return templates.TemplateResponse(request, 'register.html', context={
111 'form': form, 118 'form': form,
112 'errors': errors, 119 'errors': errors,
120 'is_first_run': is_first_run(),
113 }) 121 })
114 122
115 123
@@ -145,7 +153,7 @@ async def post_register(request: Request, email: Annotated[str, Form()] = '', pa
145 return RedirectResponse(next, status_code=303) 153 return RedirectResponse(next, status_code=303)
146 154
147 except ValidationError as exc: 155 except ValidationError as exc:
148 errors.update({e['loc'][0]: e['msg'] for e in exc.errors()}) 156 errors.update({(e['loc'][0] if e['loc'] else 'verify_password'): e['msg'] for e in exc.errors()})
149 157
150 return await base_register(request, form, errors) 158 return await base_register(request, form, errors)
151 159