You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Improve login component documentation and usability
Add complete example with database schema, credential processing, and page protection. Update authentication migration to reference new login component. Enhance login template with default icons and autocomplete attributes. Make validate parameter optional with sensible default.
INSERT INTO component(name, icon, description, introduced_in_version) VALUES
2
2
('login', 'password-user', '
3
3
The login component is an authentication form with numerous customization options.
4
-
It offers the main functionalities for this type of form.
5
-
The user can enter their username and password.
4
+
The user enters their username and password,
5
+
and is then redirected to another page, where you can use the [authentication](?component=authentication) component
6
+
to check the credentials.
7
+
6
8
There are many optional attributes such as the use of icons on input fields, the insertion of a link to a page to reset the password, an option for the application to maintain the user''s identity via a cookie.
7
9
It is also possible to set the title of the form, display the company logo, or customize the appearance of the form submission button.
8
10
9
-
This component should be used in conjunction with other components such as [authentication](component.sql?component=authentication) and [cookie](component.sql?component=cookie).
10
-
It does not implement any logic and simply collects the username and password to pass them to the code responsible for authentication.
11
+
This component does not implement any logic. It simply collects the username and password to pass them to the code responsible for authentication.
12
+
It should be used in conjunction with other components such as [authentication](component.sql?component=authentication) and [cookie](component.sql?component=cookie) to actually allow or deny access.
11
13
12
14
A few things to know :
13
15
- The form uses the POST method to transmit information to the destination page,
@@ -20,8 +22,8 @@ INSERT INTO parameter(component, name, description, type, top_level, optional) S
20
22
('title','Title of the authentication form.','TEXT',TRUE,TRUE),
21
23
('enctype','Form data encoding.','TEXT',TRUE,TRUE),
22
24
('action','An optional link to a target page that will handle the results of the form. ','TEXT',TRUE,TRUE),
('username','Label and placeholder for the user account identifier text field.','TEXT',TRUE,FALSE),
26
+
('password','Label and placeholder for the password field.','TEXT',TRUE,FALSE),
25
27
('username_icon','Icon to display on the left side of the input field, on the same line.','ICON',TRUE,TRUE),
26
28
('password_icon','Icon to display on the left side of the input field, on the same line.','ICON',TRUE,TRUE),
27
29
('image','The URL of an centered image displayed before the title.','URL',TRUE,TRUE),
@@ -30,7 +32,7 @@ INSERT INTO parameter(component, name, description, type, top_level, optional) S
30
32
('remember_me_text','A text for the option allowing the user to request the preservation of their identity. If the text is empty, the option is not displayed.','TEXT',TRUE,TRUE),
31
33
('footer','A text placed at the bottom of the authentication form.','TEXT',TRUE,TRUE),
32
34
('footer_md','A markdown text placed at the bottom of the authentication form. Useful for creating links to other pages (creating a new account, contacting technical support, etc.).','TEXT',TRUE,TRUE),
33
-
('validate','The text to display in the button at the bottom of the form that submits the values.','TEXT',TRUE,FALSE),
35
+
('validate','The text to display in the button at the bottom of the form that submits the values.','TEXT',TRUE,TRUE),
34
36
('validate_color','The color of the button at the bottom of the form that submits the values. Omit this property to use the default color.','COLOR',TRUE,TRUE),
35
37
('validate_shape','The shape of the validation button.','TEXT',TRUE,TRUE),
36
38
('validate_outline','A color to outline the validation button.','COLOR',TRUE,TRUE),
@@ -41,10 +43,57 @@ INSERT INTO parameter(component, name, description, type, top_level, optional) S
41
43
INSERT INTO example(component, description, properties)
42
44
VALUES (
43
45
'login',
44
-
'Using the main options of the login component',
45
-
JSON(
46
-
'[
47
-
{
46
+
'This example shows how to implement a complete custom login system in your SQLPage app.
47
+
48
+
### Database schema
49
+
50
+
`sqlpage/migrations/001_users.sql`
51
+
52
+
```sql
53
+
create table account (
54
+
username TEXT PRIMARY KEY,
55
+
password_hash TEXT NOT NULL
56
+
);
57
+
58
+
create table session (
59
+
id TEXT PRIMARY KEY,
60
+
username TEXT REFERENCES account(username)
61
+
-- you could add more fields for session expiration, session metadata tracking...
62
+
);
63
+
```
64
+
65
+
### Process user credentials
66
+
67
+
Create a file named `login.sql`:
68
+
69
+
```sql
70
+
SELECT ''authentication'' AS component,
71
+
''/'' AS link, -- redirect the user to the homepage if the password is incorrect
72
+
(SELECT password_hash FROM account WHERE username = :username) AS password_hash,
73
+
:password AS password;
74
+
75
+
-- The code after this point is only executed if the user has sent the correct password
76
+
77
+
-- Generate a random session token
78
+
INSERT INTO session (id, username)
79
+
VALUES (sqlpage.random_string(32), :username)
80
+
RETURNING ''cookie'' AS component, ''session_token'' AS name, id AS value,
81
+
case when :remember is null then 3600*24*365 else 3600*4 end as max_age;
82
+
83
+
select ''redirect'' as component, ''protected.sql'' as link; -- once logged in, redirect to the protected page
84
+
```
85
+
86
+
### Protect pages
87
+
88
+
Start all protected pages with
89
+
90
+
```sql
91
+
select ''redirect'' as component, ''/'' as link where not exists (select 1 from session where id=sqlpage.cookie(''session_token''));
92
+
```
93
+
94
+
### Login form on the home page
95
+
',
96
+
JSON('[{
48
97
"component": "login",
49
98
"action": "login.sql",
50
99
"image": "../assets/icon.webp",
@@ -58,7 +107,6 @@ VALUES (
58
107
"remember_me_text": "Remember me",
59
108
"footer_md": "Don''t have an account? [Register here](register.sql)",
0 commit comments