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 datetime import datetime
22import reflex as rx
3+ from reflex_local_auth .user import LocalUser
34
45import sqlalchemy
5- from sqlmodel import Field
6+ from sqlmodel import Field , Relationship
67
78from .. import utils
89
910class UserInfo (rx .Model , table = True ):
1011 email : str
1112 user_id : int = Field (foreign_key = 'localuser.id' )
13+ user : LocalUser | None = Relationship () # LocalUser instance
1214 created_at : datetime = Field (
1315 default_factory = utils .timing .get_utc_now ,
1416 sa_type = sqlalchemy .DateTime (timezone = True ),
Original file line number Diff line number Diff line change 99
1010
1111class SessionState (reflex_local_auth .LocalAuthState ):
12+
13+ @rx .cached_var
14+ def authenticated_username (self ) -> str | None :
15+ if self .authenticated_user .id < 0 :
16+ return None
17+ return self .authenticated_user .username
18+
1219 @rx .cached_var
1320 def authenticated_user_info (self ) -> UserInfo | None :
1421 if self .authenticated_user .id < 0 :
15- return
22+ return None
1623 with rx .session () as session :
17- return session .exec (
24+ result = session .exec (
1825 sqlmodel .select (UserInfo ).where (
1926 UserInfo .user_id == self .authenticated_user .id
2027 ),
2128 ).one_or_none ()
29+ if result is None :
30+ return None
31+ # user_obj = result.user
32+ # print(result.user)
33+ return result
2234
2335 def on_load (self ):
2436 if not self .is_authenticated :
Original file line number Diff line number Diff line change 11import reflex as rx
22from reflex .style import toggle_color_mode
33
4+ from ..auth .state import SessionState
45from .. import navigation
56
6-
7+ def sidebar_user_item () -> rx .Component :
8+ user_info_obj = SessionState .authenticated_user_info
9+ username_via_user_obj = rx .cond (SessionState .authenticated_username , SessionState .authenticated_username , "Account" )
10+ return rx .cond (
11+ user_info_obj ,
12+ rx .hstack (
13+ rx .icon_button (
14+ rx .icon ("user" ),
15+ size = "3" ,
16+ radius = "full" ,
17+ ),
18+ rx .vstack (
19+ rx .box (
20+ rx .text (
21+ username_via_user_obj ,
22+ size = "3" ,
23+ weight = "bold" ,
24+ ),
25+ rx .text (
26+ f"{ user_info_obj .email } " ,
27+ size = "2" ,
28+ weight = "medium" ,
29+ ),
30+ width = "100%" ,
31+ ),
32+ spacing = "0" ,
33+ align = "start" ,
34+ justify = "start" ,
35+ width = "100%" ,
36+ ),
37+ padding_x = "0.5rem" ,
38+ align = "center" ,
39+ justify = "start" ,
40+ width = "100%" ,
41+ ),
42+ rx .fragment ("" )
43+ )
744
845def sidebar_logout_item () -> rx .Component :
946 return rx .box (
@@ -129,36 +166,7 @@ def sidebar() -> rx.Component:
129166 width = "100%" ,
130167 ),
131168 rx .divider (),
132- rx .hstack (
133- rx .icon_button (
134- rx .icon ("user" ),
135- size = "3" ,
136- radius = "full" ,
137- ),
138- rx .vstack (
139- rx .box (
140- rx .text (
141- "My account" ,
142- size = "3" ,
143- weight = "bold" ,
144- ),
145- rx .text (
146- 147- size = "2" ,
148- weight = "medium" ,
149- ),
150- width = "100%" ,
151- ),
152- spacing = "0" ,
153- align = "start" ,
154- justify = "start" ,
155- width = "100%" ,
156- ),
157- padding_x = "0.5rem" ,
158- align = "center" ,
159- justify = "start" ,
160- width = "100%" ,
161- ),
169+ sidebar_user_item (),
162170 width = "100%" ,
163171 spacing = "5" ,
164172 ),
@@ -201,35 +209,7 @@ def sidebar() -> rx.Component:
201209 spacing = "1" ,
202210 ),
203211 rx .divider (margin = "0" ),
204- rx .hstack (
205- rx .icon_button (
206- rx .icon ("user" ),
207- size = "3" ,
208- radius = "full" ,
209- ),
210- rx .vstack (
211- rx .box (
212- rx .text (
213- "My account" ,
214- size = "3" ,
215- weight = "bold" ,
216- ),
217- rx .text (
218- 219- size = "2" ,
220- weight = "medium" ,
221- ),
222- width = "100%" ,
223- ),
224- spacing = "0" ,
225- justify = "start" ,
226- width = "100%" ,
227- ),
228- padding_x = "0.5rem" ,
229- align = "center" ,
230- justify = "start" ,
231- width = "100%" ,
232- ),
212+ sidebar_user_item (),
233213 width = "100%" ,
234214 spacing = "5" ,
235215 ),
You can’t perform that action at this time.
0 commit comments