Skip to content

Commit 4613615

Browse files
committed
simplify the user authentication example and make it easier to adapt to different databases
1 parent c0a1911 commit 4613615

File tree

4 files changed

+28
-23
lines changed

4 files changed

+28
-23
lines changed

CHANGELOG.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88
- 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.
99
- 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.
1010
- Revert a change introduced in v0.15.0:
11-
- 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`.
11+
- 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.
1212
- 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.
1313
- 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.
1414

Lines changed: 10 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -1,21 +1,10 @@
1-
WITH inserted_user AS (
2-
INSERT INTO user_info (username, password_hash)
3-
VALUES (:username, sqlpage.hash_password(:password))
4-
ON CONFLICT (username) DO NOTHING
5-
RETURNING username
6-
)
7-
SELECT 'hero' AS component,
8-
'Welcome' AS title,
9-
'Welcome, ' || username || '! Your user account was successfully created. You can now log in.' AS description,
10-
'https://upload.wikimedia.org/wikipedia/commons/thumb/e/e1/Community_wp20.png/974px-Community_wp20.png' AS image,
11-
'signin.sql' AS link,
12-
'Log in' AS link_text
13-
FROM inserted_user
14-
UNION ALL
15-
SELECT 'hero' AS component,
16-
'Sorry' AS title,
17-
'Sorry, this user name is already taken.' AS description_md,
18-
'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,
19-
'signup.sql' AS link,
20-
'Try again' AS link_text
21-
WHERE NOT EXISTS (SELECT 1 FROM inserted_user);
1+
INSERT INTO user_info (username, password_hash)
2+
VALUES (:username, sqlpage.hash_password(:password))
3+
ON CONFLICT (username) DO NOTHING
4+
RETURNING
5+
'redirect' AS component,
6+
'create_user_welcome_message.sql?username=' || :username AS link;
7+
8+
-- If we are still here, it means that the user was not created
9+
-- because the username was already taken.
10+
SELECT 'redirect' AS component, 'create_user_welcome_message.sql?error&username=' || :username AS link;
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
SELECT 'hero' AS component,
2+
'Welcome' AS title,
3+
'Welcome, ' || $username || '! Your user account was successfully created. You can now log in.' AS description,
4+
'https://upload.wikimedia.org/wikipedia/commons/thumb/e/e1/Community_wp20.png/974px-Community_wp20.png' AS image,
5+
'signin.sql' AS link,
6+
'Log in' AS link_text
7+
WHERE $error IS NULL;
8+
9+
SELECT 'hero' AS component,
10+
'Sorry' AS title,
11+
'Sorry, the user name "' || $username || '" is already taken.' AS description,
12+
'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,
13+
'signup.sql' AS link,
14+
'Try again' AS link_text
15+
WHERE $error IS NOT NULL;

examples/user-authentication/docker-compose.yml

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,11 @@
11
services:
22
web:
3-
image: lovasoa/sqlpage
3+
image: lovasoa/sqlpage:main # main is cutting edge, use lovasoa/sqlpage:latest for the latest stable version
44
ports:
55
- "8080:8080"
66
volumes:
77
- .:/var/www
8+
- ./sqlpage:/etc/sqlpage
89
depends_on:
910
- db
1011
environment:

0 commit comments

Comments
 (0)