File tree 5 files changed +58
-2
lines changed
5 files changed +58
-2
lines changed Original file line number Diff line number Diff line change 1
1
from .models import UserInfo
2
2
from . import pages
3
3
4
+ from .state import SessionState
5
+
4
6
5
7
__all__ = [
6
8
'pages' ,
7
- 'UserInfo'
9
+ 'UserInfo' ,
10
+ 'SessionState'
8
11
]
Original file line number Diff line number Diff line change 1
1
import reflex as rx
2
2
import reflex_local_auth
3
3
4
+ import sqlmodel
5
+
4
6
from .models import UserInfo
5
7
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
+
6
30
class MyRegisterState (reflex_local_auth .RegistrationState ):
7
31
# This event handler must be named something besides `handle_registration`!!!
8
32
def handle_registration_email (self , form_data ):
9
33
registration_result = super ().handle_registration (form_data )
34
+ print (self .new_user_id )
10
35
if self .new_user_id >= 0 :
11
36
with rx .session () as session :
12
37
session .add (
Original file line number Diff line number Diff line change @@ -63,6 +63,12 @@ def index() -> rx.Component:
63
63
app .add_page (pages .about_page ,
64
64
route = navigation .routes .ABOUT_US_ROUTE )
65
65
66
+ app .add_page (
67
+ pages .protected_page ,
68
+ route = "/protected/" ,
69
+ on_load = auth .SessionState .on_load
70
+ )
71
+
66
72
app .add_page (
67
73
blog .blog_post_list_page ,
68
74
route = navigation .routes .BLOG_POSTS_ROUTE ,
Original file line number Diff line number Diff line change 1
1
from .about import about_page
2
2
from .pricing import pricing_page
3
+ from .protected import protected_page
3
4
# from .contact import contact_page
4
5
5
6
__all__ = [
6
7
'about_page' ,
7
8
# 'contact_page',
8
- 'pricing_page'
9
+ 'pricing_page' ,
10
+ 'protected_page'
9
11
]
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