Skip to content

Commit 4c7d809

Browse files
42 - User SessionState Object
1 parent 6a2c167 commit 4c7d809

File tree

5 files changed

+58
-2
lines changed

5 files changed

+58
-2
lines changed

full_stack_python/auth/__init__.py

+4-1
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,11 @@
11
from .models import UserInfo
22
from . import pages
33

4+
from .state import SessionState
5+
46

57
__all__ = [
68
'pages',
7-
'UserInfo'
9+
'UserInfo',
10+
'SessionState'
811
]

full_stack_python/auth/state.py

+25
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,37 @@
11
import reflex as rx
22
import reflex_local_auth
33

4+
import sqlmodel
5+
46
from .models import UserInfo
57

8+
9+
10+
11+
class SessionState(reflex_local_auth.LocalAuthState):
12+
@rx.cached_var
13+
def authenticated_user_info(self) -> UserInfo | None:
14+
if self.authenticated_user.id < 0:
15+
return
16+
with rx.session() as session:
17+
return session.exec(
18+
sqlmodel.select(UserInfo).where(
19+
UserInfo.user_id == self.authenticated_user.id
20+
),
21+
).one_or_none()
22+
23+
def on_load(self):
24+
if not self.is_authenticated:
25+
return reflex_local_auth.LoginState.redir
26+
print(self.is_authenticated)
27+
print(self.authenticated_user_info)
28+
29+
630
class MyRegisterState(reflex_local_auth.RegistrationState):
731
# This event handler must be named something besides `handle_registration`!!!
832
def handle_registration_email(self, form_data):
933
registration_result = super().handle_registration(form_data)
34+
print(self.new_user_id)
1035
if self.new_user_id >= 0:
1136
with rx.session() as session:
1237
session.add(

full_stack_python/full_stack_python.py

+6
Original file line numberDiff line numberDiff line change
@@ -63,6 +63,12 @@ def index() -> rx.Component:
6363
app.add_page(pages.about_page,
6464
route=navigation.routes.ABOUT_US_ROUTE)
6565

66+
app.add_page(
67+
pages.protected_page,
68+
route="/protected/",
69+
on_load=auth.SessionState.on_load
70+
)
71+
6672
app.add_page(
6773
blog.blog_post_list_page,
6874
route=navigation.routes.BLOG_POSTS_ROUTE,

full_stack_python/pages/__init__.py

+3-1
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,11 @@
11
from .about import about_page
22
from .pricing import pricing_page
3+
from .protected import protected_page
34
# from .contact import contact_page
45

56
__all__ = [
67
'about_page',
78
# 'contact_page',
8-
'pricing_page'
9+
'pricing_page',
10+
'protected_page'
911
]

full_stack_python/pages/protected.py

+20
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
import reflex as rx
2+
import reflex_local_auth
3+
4+
from ..ui.base import base_page
5+
6+
# @rx.page(route='/about')
7+
@reflex_local_auth.require_login
8+
def protected_page() -> rx.Component:
9+
my_child = rx.vstack(
10+
rx.heading("Protect Page", size="9"),
11+
rx.text(
12+
"Something cool about us.",
13+
),
14+
spacing="5",
15+
justify="center",
16+
align="center",
17+
min_height="85vh",
18+
id='my-child'
19+
)
20+
return base_page(my_child)

0 commit comments

Comments
 (0)