From 64a602897a32a8c7298194e250efbe7650b4ce1e Mon Sep 17 00:00:00 2001 From: lovasoa Date: Tue, 7 Nov 2023 00:01:20 +0100 Subject: [PATCH] simplify the user authentication example and make it easier to adapt to different databases --- CHANGELOG.md | 2 +- examples/user-authentication/create_user.sql | 31 ++++++------------- .../create_user_welcome_message.sql | 15 +++++++++ .../user-authentication/docker-compose.yml | 3 +- 4 files changed, 28 insertions(+), 23 deletions(-) create mode 100644 examples/user-authentication/create_user_welcome_message.sql diff --git a/CHANGELOG.md b/CHANGELOG.md index d8d579ef..85dc2c2a 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -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. diff --git a/examples/user-authentication/create_user.sql b/examples/user-authentication/create_user.sql index ba38ceea..aa63c4d5 100644 --- a/examples/user-authentication/create_user.sql +++ b/examples/user-authentication/create_user.sql @@ -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); \ No newline at end of file +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; \ No newline at end of file diff --git a/examples/user-authentication/create_user_welcome_message.sql b/examples/user-authentication/create_user_welcome_message.sql new file mode 100644 index 00000000..bfb11e51 --- /dev/null +++ b/examples/user-authentication/create_user_welcome_message.sql @@ -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; \ No newline at end of file diff --git a/examples/user-authentication/docker-compose.yml b/examples/user-authentication/docker-compose.yml index c4478dc9..c29ecdc2 100644 --- a/examples/user-authentication/docker-compose.yml +++ b/examples/user-authentication/docker-compose.yml @@ -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: