Skip to content

Commit 7e81e8c

Browse files
46 - User Dashboard & Logout User
1 parent 0b27226 commit 7e81e8c

File tree

8 files changed

+82
-25
lines changed

8 files changed

+82
-25
lines changed

full_stack_python/auth/__init__.py

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,6 @@
11
from .models import UserInfo
2-
from . import pages
3-
4-
from .state import SessionState
52

63

74
__all__ = [
8-
'pages',
95
'UserInfo',
10-
'SessionState'
116
]

full_stack_python/auth/pages.py

Lines changed: 23 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,9 +3,11 @@
33
from reflex_local_auth.pages.login import LoginState, login_form
44
from reflex_local_auth.pages.registration import RegistrationState, register_form
55

6+
from .. import navigation
67
from ..ui.base import base_page
78

89
from .forms import my_register_form
10+
from .state import SessionState
911

1012
def my_login_page()->rx.Component:
1113
return base_page(
@@ -33,4 +35,24 @@ def my_register_page()->rx.Component:
3335
min_height="85vh",
3436
)
3537

36-
)
38+
)
39+
40+
41+
def my_logout_page() -> rx.Component:
42+
# Welcome Page (Index)
43+
my_child = rx.vstack(
44+
rx.heading("Are you sure you want logout?", size="7"),
45+
rx.link(
46+
rx.button("No", color_scheme="gray"),
47+
href=navigation.routes.HOME_ROUTE
48+
),
49+
rx.button("Yes, please logout", on_click=SessionState.perform_logout),
50+
spacing="5",
51+
justify="center",
52+
align="center",
53+
# text_align="center",
54+
min_height="85vh",
55+
id='my-child'
56+
)
57+
return base_page(my_child)
58+

full_stack_python/auth/state.py

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,11 @@ def on_load(self):
2626
print(self.is_authenticated)
2727
print(self.authenticated_user_info)
2828

29+
def perform_logout(self):
30+
self.do_logout()
31+
return rx.redirect("/")
32+
33+
2934

3035
class MyRegisterState(reflex_local_auth.RegistrationState):
3136
def handle_registration(

full_stack_python/full_stack_python.py

Lines changed: 17 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,14 @@
66
from rxconfig import config
77
from .ui.base import base_page
88

9-
from . import auth, blog, contact, navigation, pages
9+
from .auth.pages import (
10+
my_login_page,
11+
my_register_page,
12+
my_logout_page
13+
)
14+
from .auth.state import SessionState
15+
16+
from . import blog, contact, navigation, pages
1017

1118
class State(rx.State):
1219
"""The app state."""
@@ -49,24 +56,30 @@ def index() -> rx.Component:
4956
app.add_page(index)
5057
# reflex_local_auth pages
5158
app.add_page(
52-
auth.pages.my_login_page,
59+
my_login_page,
5360
route=reflex_local_auth.routes.LOGIN_ROUTE,
5461
title="Login",
5562
)
5663
app.add_page(
57-
auth.pages.my_register_page,
64+
my_register_page,
5865
route=reflex_local_auth.routes.REGISTER_ROUTE,
5966
title="Register",
6067
)
6168

69+
app.add_page(
70+
my_logout_page,
71+
route=navigation.routes.LOGOUT_ROUTE,
72+
title="Logout",
73+
)
74+
6275
# my pages
6376
app.add_page(pages.about_page,
6477
route=navigation.routes.ABOUT_US_ROUTE)
6578

6679
app.add_page(
6780
pages.protected_page,
6881
route="/protected/",
69-
on_load=auth.SessionState.on_load
82+
on_load=SessionState.on_load
7083
)
7184

7285
app.add_page(

full_stack_python/navigation/routes.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,4 +4,5 @@
44
BLOG_POST_ADD_ROUTE="/blog/add"
55
CONTACT_US_ROUTE="/contact"
66
CONTACT_ENTRIES_ROUTE="/contact/entries"
7+
LOGOUT_ROUTE="/logout"
78
PRICING_ROUTE="/pricing"

full_stack_python/navigation/state.py

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,9 @@ def to_register(self):
1111

1212
def to_login(self):
1313
return rx.redirect(reflex_local_auth.routes.LOGIN_ROUTE)
14+
15+
def to_logout(self):
16+
return rx.redirect(routes.LOGOUT_ROUTE)
1417

1518
def to_about_us(self):
1619
return rx.redirect(routes.ABOUT_US_ROUTE)

full_stack_python/ui/base.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
import reflex as rx
22

3+
from ..auth.state import SessionState
34
from .nav import navbar
45
from .dashboard import base_dashboard_page
56

@@ -20,11 +21,10 @@ def base_layout_component(child, *args, **kwargs) -> rx.Component:
2021
)
2122

2223
def base_page(child: rx.Component, *args, **kwargs) -> rx.Component:
23-
is_logged_in = True
2424
if not isinstance(child,rx. Component):
2525
child = rx.heading("this is not a valid child element")
2626
return rx.cond(
27-
is_logged_in,
27+
SessionState.is_authenticated,
2828
base_dashboard_page(child, *args, **kwargs),
2929
base_layout_component(child, *args, **kwargs ),
3030
)

full_stack_python/ui/sidebar.py

Lines changed: 31 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,34 @@
33

44
from .. import navigation
55

6+
7+
8+
def sidebar_logout_item() -> rx.Component:
9+
return rx.box(
10+
rx.hstack(
11+
rx.icon("log-out"),
12+
rx.text("Logout", size="4"),
13+
width="100%",
14+
padding_x="0.5rem",
15+
padding_y="0.75rem",
16+
align="center",
17+
style={
18+
"_hover": {
19+
"cursor": "pointer", # css
20+
"bg": rx.color("accent", 4),
21+
"color": rx.color("accent", 11),
22+
},
23+
"color": rx.color("accent", 11),
24+
"border-radius": "0.5em",
25+
},
26+
),
27+
on_click=navigation.NavState.to_logout,
28+
as_='button',
29+
underline="none",
30+
weight="medium",
31+
width="100%",
32+
)
33+
634
def sidebar_dark_mode_toggle_item() -> rx.Component:
735
return rx.box(
836
rx.hstack(
@@ -96,9 +124,7 @@ def sidebar() -> rx.Component:
96124
rx.vstack(
97125
rx.vstack(
98126
sidebar_dark_mode_toggle_item(),
99-
sidebar_item(
100-
"Log out", "log-out", "/#"
101-
),
127+
sidebar_logout_item(),
102128
spacing="1",
103129
width="100%",
104130
),
@@ -169,16 +195,8 @@ def sidebar() -> rx.Component:
169195
rx.spacer(),
170196
rx.vstack(
171197
rx.vstack(
172-
sidebar_item(
173-
"Settings",
174-
"settings",
175-
"/#",
176-
),
177-
sidebar_item(
178-
"Log out",
179-
"log-out",
180-
"/#",
181-
),
198+
sidebar_dark_mode_toggle_item(),
199+
sidebar_logout_item(),
182200
width="100%",
183201
spacing="1",
184202
),

0 commit comments

Comments
 (0)