simplify the user authentication example and make it easier to adapt to different databases

This commit is contained in:
lovasoa
2023-11-07 00:01:20 +01:00
parent c5a56937b4
commit 64a602897a
4 changed files with 28 additions and 23 deletions
+1 -1
View File
@@ -8,7 +8,7 @@
- The *submit* button can now be customized, and can be removed completely, which is useful to create multiple submit buttons that submit the form to different targets.
- Support non-string values in markdown fields. `NULL` values are now displayed as empty strings, numeric values are displayed as strings, booleans as `true` or `false`, and arrays as lines of text. This avoids the need to cast values to strings in SQL queries.
- Revert a change introduced in v0.15.0:
- Re-add the systematic `CAST(? AS TEXT)` around variables, which helps the database know which type it is dealing with in advance. This fixes a regression in 0.15 where some SQLite websites were broken because of missing affinity information. In SQLite `SELECT '1' = 1` returns `false` but `SELECT CAST('1' AS TEXT) = 1` returns `true`.
- Re-add the systematic `CAST(? AS TEXT)` around variables, which helps the database know which type it is dealing with in advance. This fixes a regression in 0.15 where some SQLite websites were broken because of missing affinity information. In SQLite `SELECT '1' = 1` returns `false` but `SELECT CAST('1' AS TEXT) = 1` returns `true`. This also fixes error messages like `could not determine data type of parameter $1` in PostgreSQL.
- Fix a bug where [cookie](https://sql.ophir.dev/documentation.sql?component=cookie#component) removal set the cookie value to the empty string instead of removing the cookie completely.
- Support form submission using the [button](https://sql.ophir.dev/documentation.sql?component=button#component) component using its new `form` property. This allows you to create a form with multiple submit buttons that submit the form to different targets.
+10 -21
View File
@@ -1,21 +1,10 @@
WITH inserted_user AS (
INSERT INTO user_info (username, password_hash)
VALUES (:username, sqlpage.hash_password(:password))
ON CONFLICT (username) DO NOTHING
RETURNING username
)
SELECT 'hero' AS component,
'Welcome' AS title,
'Welcome, ' || username || '! Your user account was successfully created. You can now log in.' AS description,
'https://upload.wikimedia.org/wikipedia/commons/thumb/e/e1/Community_wp20.png/974px-Community_wp20.png' AS image,
'signin.sql' AS link,
'Log in' AS link_text
FROM inserted_user
UNION ALL
SELECT 'hero' AS component,
'Sorry' AS title,
'Sorry, this user name is already taken.' AS description_md,
'https://upload.wikimedia.org/wikipedia/commons/thumb/f/f0/Sad_face_of_a_Wayuu_Woman.jpg/640px-Sad_face_of_a_Wayuu_Woman.jpg' AS image,
'signup.sql' AS link,
'Try again' AS link_text
WHERE NOT EXISTS (SELECT 1 FROM inserted_user);
INSERT INTO user_info (username, password_hash)
VALUES (:username, sqlpage.hash_password(:password))
ON CONFLICT (username) DO NOTHING
RETURNING
'redirect' AS component,
'create_user_welcome_message.sql?username=' || :username AS link;
-- If we are still here, it means that the user was not created
-- because the username was already taken.
SELECT 'redirect' AS component, 'create_user_welcome_message.sql?error&username=' || :username AS link;
@@ -0,0 +1,15 @@
SELECT 'hero' AS component,
'Welcome' AS title,
'Welcome, ' || $username || '! Your user account was successfully created. You can now log in.' AS description,
'https://upload.wikimedia.org/wikipedia/commons/thumb/e/e1/Community_wp20.png/974px-Community_wp20.png' AS image,
'signin.sql' AS link,
'Log in' AS link_text
WHERE $error IS NULL;
SELECT 'hero' AS component,
'Sorry' AS title,
'Sorry, the user name "' || $username || '" is already taken.' AS description,
'https://upload.wikimedia.org/wikipedia/commons/thumb/f/f0/Sad_face_of_a_Wayuu_Woman.jpg/640px-Sad_face_of_a_Wayuu_Woman.jpg' AS image,
'signup.sql' AS link,
'Try again' AS link_text
WHERE $error IS NOT NULL;
@@ -1,10 +1,11 @@
services:
web:
image: lovasoa/sqlpage
image: lovasoa/sqlpage:main # main is cutting edge, use lovasoa/sqlpage:latest for the latest stable version
ports:
- "8080:8080"
volumes:
- .:/var/www
- ./sqlpage:/etc/sqlpage
depends_on:
- db
environment: