fixed persistence in auth example

This commit is contained in:
Sn3llius
2024-09-23 15:21:01 +02:00
parent a8a57fcef7
commit 7ff0f83bef
4 changed files with 116 additions and 101 deletions

View File

@@ -79,99 +79,113 @@ class Navbar(rio.Component):
else:
user_settings = True
# Create the content of the navbar. First we create a row with a certain
# spacing and margin. We can use the `.add()` method to add components
# by condition to the row.
navbar_content = rio.Row(spacing=1, margin=1)
# Links can be used to navigate to other pages and
# external URLs. You can pass either a simple string, or
# another component as their content.
navbar_content.add(
rio.Link(
rio.IconButton(
"rio/logo",
style="plain-text",
min_size=2.5,
),
"/app/home",
)
)
# This spacer will take up any superfluous space,
# effectively pushing the subsequent buttons to the
# right.
navbar_content.add(rio.Spacer())
# Based on the user's status, display the appropriate buttons
if user_settings:
# By sticking buttons into a `rio.Link`, we can easily
# make the buttons navigate to other pages, without
# having to write an event handler. Notice how there is
# no Python function called when the button is clicked.
navbar_content.add(
rio.Link(
rio.Button(
"Home",
icon="material/news",
style=(
"major"
if active_page_url_segment == "home"
else "plain-text"
),
),
"/app/home",
)
)
# Same game, different button
navbar_content.add(
rio.Link(
rio.Button(
"News",
icon="material/news",
style=(
"major"
if active_page_url_segment == "news-page"
else "plain-text"
),
),
"/app/news-page",
)
)
# Same game, different button
navbar_content.add(
rio.Link(
rio.Button(
"About",
icon="material/info",
style=(
"major"
if active_page_url_segment == "about-page"
else "plain-text"
),
),
"/app/about-page",
)
)
# Logout
navbar_content.add(
rio.Button(
"Logout",
icon="material/logout",
style="plain-text",
on_press=self.on_logout,
)
)
# Display the login button if the user is not logged in
else:
navbar_content.add(
rio.Link(
rio.Button(
"Login",
icon="material/login",
style="plain-text",
),
"/",
)
)
# The navbar should appear above all other components. This is easily
# done by using a `rio.Overlay` component.
return rio.Overlay(
# Use a rectangle for visual separation
rio.Rectangle(
content=rio.Row(
# Links can be used to navigate to other pages and
# external URLs. You can pass either a simple string, or
# another component as their content.
rio.Link(
rio.IconButton(
"rio/logo",
style="plain-text",
min_size=2.5,
),
"/app/home",
),
# This spacer will take up any superfluous space,
# effectively pushing the subsequent buttons to the
# right.
rio.Spacer(),
# By sticking buttons into a `rio.Link`, we can easily
# make the buttons navigate to other pages, without
# having to write an event handler. Notice how there is
# no Python function called when the button is clicked.
# Check if user is logged in and display the appropriate
# buttons
*(
# Display the navbar buttons if the user is logged in
[
rio.Link(
rio.Button(
"Home",
icon="material/news",
style=(
"major"
if active_page_url_segment == "home"
else "plain-text"
),
),
"/app/home",
),
rio.Link(
rio.Button(
"News",
icon="material/news",
style=(
"major"
if active_page_url_segment
== "news-page"
else "plain-text"
),
),
"/app/news-page",
),
# Same game, different button
rio.Link(
rio.Button(
"About",
icon="material/info",
style=(
"major"
if active_page_url_segment
== "about-page"
else "plain-text"
),
),
"/app/about-page",
),
# Logout
rio.Button(
"Logout",
icon="material/logout",
style="plain-text",
on_press=self.on_logout,
),
]
if user_settings
# Display the login button if the user is not logged in
else [
rio.Link(
rio.Button(
"Login",
icon="material/login",
style="plain-text",
),
"/",
),
]
),
spacing=1,
margin=1,
),
# Use the content we've built up by conditions
content=navbar_content,
# Set the fill of the rectangle to the neutral color of the
# theme
fill=self.session.theme.neutral_color,

View File

@@ -138,18 +138,20 @@ class UserSignUpForm(rio.Component):
# Form fields
rio.TextInput(
text=self.bind().username_sign_up,
label="Username",
label="Username*",
is_valid=self.username_valid,
),
rio.TextInput(
text=self.bind().password_sign_up,
label="Password",
label="Password*",
is_valid=self.passwords_valid,
is_secret=True,
),
rio.TextInput(
text=self.bind().password_sign_up_repeat,
label="Repeat Password*",
is_valid=self.passwords_valid,
is_secret=True,
),
# And finally, some buttons to confirm or cancel the sign-up
# process
@@ -164,7 +166,7 @@ class UserSignUpForm(rio.Component):
),
spacing=2,
),
spacing=2,
spacing=1,
margin=2,
),
align_x=0.5,

View File

@@ -1,7 +1,6 @@
from __future__ import annotations
# <additional-imports>
from datetime import datetime, timezone
from typing import * # type: ignore
import rio
@@ -170,7 +169,7 @@ class LoginPage(rio.Component):
),
spacing=2,
),
spacing=2,
spacing=1,
margin=2,
),
align_x=0.5,

View File

@@ -98,10 +98,10 @@ class Persistence:
cursor.execute(
"""
INSERT INTO users (id, username, created_at, password_hash, password_salt)
VALUES (?, ?, ?, ?, ?, ?)
VALUES (?, ?, ?, ?, ?)
""",
(
str(user.id), # TODO: int
str(user.id),
user.username,
user.created_at.timestamp(),
user.password_hash,
@@ -144,9 +144,9 @@ class Persistence:
return data_models.AppUser(
id=uuid.UUID(row[0]),
username=row[1],
created_at=datetime.fromtimestamp(row[3], tz=timezone.utc),
password_hash=row[4],
password_salt=row[5],
created_at=datetime.fromtimestamp(row[2], tz=timezone.utc),
password_hash=row[3],
password_salt=row[4],
)
# If no user was found, signal that with a KeyError
@@ -186,9 +186,9 @@ class Persistence:
return data_models.AppUser(
id=uuid.UUID(row[0]),
username=row[1],
created_at=datetime.fromtimestamp(row[3], tz=timezone.utc),
password_hash=row[4],
password_salt=row[5],
created_at=datetime.fromtimestamp(row[2], tz=timezone.utc),
password_hash=row[3],
password_salt=row[4],
)
# If no user was found, signal that with a KeyError