File tree Expand file tree Collapse file tree 3 files changed +57
-63
lines changed Expand file tree Collapse file tree 3 files changed +57
-63
lines changed Original file line number Diff line number Diff line change 1
1
from datetime import datetime
2
2
import reflex as rx
3
+ from reflex_local_auth .user import LocalUser
3
4
4
5
import sqlalchemy
5
- from sqlmodel import Field
6
+ from sqlmodel import Field , Relationship
6
7
7
8
from .. import utils
8
9
9
10
class UserInfo (rx .Model , table = True ):
10
11
email : str
11
12
user_id : int = Field (foreign_key = 'localuser.id' )
13
+ user : LocalUser | None = Relationship () # LocalUser instance
12
14
created_at : datetime = Field (
13
15
default_factory = utils .timing .get_utc_now ,
14
16
sa_type = sqlalchemy .DateTime (timezone = True ),
Original file line number Diff line number Diff line change 9
9
10
10
11
11
class 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
+
12
19
@rx .cached_var
13
20
def authenticated_user_info (self ) -> UserInfo | None :
14
21
if self .authenticated_user .id < 0 :
15
- return
22
+ return None
16
23
with rx .session () as session :
17
- return session .exec (
24
+ result = session .exec (
18
25
sqlmodel .select (UserInfo ).where (
19
26
UserInfo .user_id == self .authenticated_user .id
20
27
),
21
28
).one_or_none ()
29
+ if result is None :
30
+ return None
31
+ # user_obj = result.user
32
+ # print(result.user)
33
+ return result
22
34
23
35
def on_load (self ):
24
36
if not self .is_authenticated :
Original file line number Diff line number Diff line change 1
1
import reflex as rx
2
2
from reflex .style import toggle_color_mode
3
3
4
+ from ..auth .state import SessionState
4
5
from .. import navigation
5
6
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
+ )
7
44
8
45
def sidebar_logout_item () -> rx .Component :
9
46
return rx .box (
@@ -129,36 +166,7 @@ def sidebar() -> rx.Component:
129
166
width = "100%" ,
130
167
),
131
168
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 (),
162
170
width = "100%" ,
163
171
spacing = "5" ,
164
172
),
@@ -201,35 +209,7 @@ def sidebar() -> rx.Component:
201
209
spacing = "1" ,
202
210
),
203
211
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 (),
233
213
width = "100%" ,
234
214
spacing = "5" ,
235
215
),
You can’t perform that action at this time.
0 commit comments