-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathmain.py
58 lines (43 loc) · 1.21 KB
/
main.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
from fasthtml import common as fh
from components import auth
app, rt = fh.fast_app(before=auth.bware)
@rt("/login")
def get():
return auth.login_get()
@rt("/login")
def post(login: auth.Login, sess):
return auth.login_post(login, sess)
@rt("/logout")
def logout(sess):
return auth.logout(sess)
@rt("/protected")
def protected(auth):
print(f"Accessing protected page. Auth: {auth}")
return fh.Titled(
"Protected Page",
fh.P(f"Welcome, {auth}!"),
fh.A("Back", href="/"),
fh.P(),
fh.A("Logout", href="/logout"),
)
@rt("/")
def home(sess):
if auth.is_authenticated(sess):
user = sess["user"]
print(f"User: {user}")
return fh.Titled(
"Dashboard",
fh.P(f"Welcome, {user}!"),
fh.P("You are logged in. View a protected page below."),
fh.A("Protected Page", href="/protected"),
fh.P(),
fh.P("Logout here:"),
fh.A("Logout", href="/logout"),
)
else:
return fh.Titled(
"Home", fh.H1("Welcome to the App"), fh.A("Login", href="/login")
)
if __name__ == "__main__":
print("Starting server")
fh.serve(port=8080)