File tree Expand file tree Collapse file tree
Expand file tree Collapse file tree Original file line number Diff line number Diff line change 11from .models import UserInfo
22from . import pages
33
4+ from .state import SessionState
5+
46
57__all__ = [
68 'pages' ,
7- 'UserInfo'
9+ 'UserInfo' ,
10+ 'SessionState'
811]
Original file line number Diff line number Diff line change 11import reflex as rx
22import reflex_local_auth
33
4+ import sqlmodel
5+
46from .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+
630class 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 (
Original file line number Diff line number Diff line change @@ -63,6 +63,12 @@ def index() -> rx.Component:
6363app .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+
6672app .add_page (
6773 blog .blog_post_list_page ,
6874 route = navigation .routes .BLOG_POSTS_ROUTE ,
Original file line number Diff line number Diff line change 11from .about import about_page
22from .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]
Original file line number Diff line number Diff line change 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 )
You can’t perform that action at this time.
0 commit comments