Skip to content

Commit 6a2c167

Browse files
41 - Customize User Registration for Related Model
1 parent a3ba31a commit 6a2c167

3 files changed

Lines changed: 65 additions & 1 deletion

File tree

full_stack_python/auth/forms.py

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
import reflex as rx
2+
import reflex_local_auth
3+
from reflex_local_auth.pages.components import input_100w, MIN_WIDTH
4+
5+
from .state import MyRegisterState
6+
7+
def register_error() -> rx.Component:
8+
"""Render the registration error message."""
9+
return rx.cond(
10+
reflex_local_auth.RegistrationState.error_message != "",
11+
rx.callout(
12+
reflex_local_auth.RegistrationState.error_message,
13+
icon="triangle_alert",
14+
color_scheme="red",
15+
role="alert",
16+
width="100%",
17+
),
18+
)
19+
20+
21+
def my_register_form() -> rx.Component:
22+
"""Render the registration form."""
23+
return rx.form(
24+
rx.vstack(
25+
rx.heading("Create an account", size="7"),
26+
register_error(),
27+
rx.text("Username"),
28+
input_100w("username"),
29+
rx.text("Email"),
30+
input_100w("email", type='email'),
31+
rx.text("Password"),
32+
input_100w("password", type="password"),
33+
rx.text("Confirm Password"),
34+
input_100w("confirm_password", type="password"),
35+
rx.button("Sign up", width="100%"),
36+
rx.center(
37+
rx.link("Login", on_click=lambda: rx.redirect(reflex_local_auth.routes.LOGIN_ROUTE)),
38+
width="100%",
39+
),
40+
min_width=MIN_WIDTH,
41+
),
42+
on_submit=MyRegisterState.handle_registration_email,
43+
)

full_stack_python/auth/pages.py

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,8 @@
55

66
from ..ui.base import base_page
77

8+
from .forms import my_register_form
9+
810
def my_login_page()->rx.Component:
911
return base_page(
1012
rx.center(
@@ -25,7 +27,7 @@ def my_register_page()->rx.Component:
2527
rx.vstack(
2628
rx.text("Registration successful!"),
2729
),
28-
rx.card(register_form()),
30+
rx.card(my_register_form()),
2931

3032
),
3133
min_height="85vh",

full_stack_python/auth/state.py

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
import reflex as rx
2+
import reflex_local_auth
3+
4+
from .models import UserInfo
5+
6+
class MyRegisterState(reflex_local_auth.RegistrationState):
7+
# This event handler must be named something besides `handle_registration`!!!
8+
def handle_registration_email(self, form_data):
9+
registration_result = super().handle_registration(form_data)
10+
if self.new_user_id >= 0:
11+
with rx.session() as session:
12+
session.add(
13+
UserInfo(
14+
email=form_data["email"],
15+
user_id=self.new_user_id,
16+
)
17+
)
18+
session.commit()
19+
return registration_result

0 commit comments

Comments
 (0)