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
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
-
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
-
4
+
It offers the main functionalities for this type of form.
5
+
The user can enter their username and password.
8
6
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.
9
7
It is also possible to set the title of the form, display the company logo, or customize the appearance of the form submission button.
10
8
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.
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.
13
11
14
12
A few things to know :
15
13
- The form uses the POST method to transmit information to the destination page,
@@ -22,6 +20,7 @@ INSERT INTO parameter(component, name, description, type, top_level, optional) S
22
20
('title','Title of the authentication form.','TEXT',TRUE,TRUE),
23
21
('enctype','Form data encoding.','TEXT',TRUE,TRUE),
24
22
('action','An optional link to a target page that will handle the results of the form. ','TEXT',TRUE,TRUE),
23
+
('error_message','An error message to display above the form, typically shown after a failed login attempt.','TEXT',TRUE,TRUE),
25
24
('username','Label and placeholder for the user account identifier text field.','TEXT',TRUE,FALSE),
26
25
('password','Label and placeholder for the password field.','TEXT',TRUE,FALSE),
27
26
('username_icon','Icon to display on the left side of the input field, on the same line.','ICON',TRUE,TRUE),
@@ -43,57 +42,10 @@ INSERT INTO parameter(component, name, description, type, top_level, optional) S
43
42
INSERT INTO example(component, description, properties)
44
43
VALUES (
45
44
'login',
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('[{
45
+
'Using the main options of the login component',
46
+
JSON(
47
+
'[
48
+
{
97
49
"component": "login",
98
50
"action": "login.sql",
99
51
"image": "../assets/icon.webp",
@@ -107,6 +59,8 @@ select ''redirect'' as component, ''/'' as link where not exists (select 1 from
107
59
"remember_me_text": "Remember me",
108
60
"footer_md": "Don''t have an account? [Register here](register.sql)",
0 commit comments