Skip to content

Commit 99dd679

Browse files
authored
Merge branch 'lovasoa:main' into menu-item-icon
2 parents 3db0bb7 + a789e74 commit 99dd679

File tree

2 files changed

+65
-9
lines changed

2 files changed

+65
-9
lines changed
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
SELECT 'authentication' AS component,
2+
case sqlpage.basic_auth_username()
3+
when 'admin'
4+
then '$argon2i$v=19$m=8,t=1,p=1$YWFhYWFhYWE$oKBq5E8XFTHO2w' -- the password is 'password'
5+
when 'user'
6+
then '$argon2i$v=19$m=8,t=1,p=1$YWFhYWFhYWE$qsrWdjgl96ooYw' -- the password is 'user'
7+
end AS password_hash, -- this is a hash of the password 'password'
8+
sqlpage.basic_auth_password() AS password; -- this is the password that the user entered in the browser popup
9+
10+
select 'dynamic' as component, properties FROM example WHERE component = 'shell' LIMIT 1;
11+
12+
select 'text' as component, '
13+
# Authentication
14+
15+
Read the [source code](//github.com/lovasoa/SQLpage/blob/main/examples/official-site/examples/authentication/basic_auth.sql) for this demo.
16+
' as contents_md;
17+
18+
select 'alert' as component, 'info' as color, CONCAT('You are logged in as ', sqlpage.basic_auth_username()) as title;

examples/official-site/sqlpage/migrations/07_authentication.sql

Lines changed: 47 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -54,32 +54,70 @@ VALUES (
5454
5555
### Usage with HTTP basic authentication
5656
57-
The most basic usage of the authentication component is to let SQLPage handle the authentication through HTTP basic authentication.
58-
This is the simplest way to password-protect a page, but it is not very user-friendly, because the browser will show an unstyled popup asking for the username and password.
59-
The username and password entered by the user will be accessible in your SQL code using the
57+
The most basic usage of the authentication component is with the
6058
[`sqlpage.basic_auth_username()`](functions.sql?function=basic_auth_username#function) and
6159
[`sqlpage.basic_auth_password()`](functions.sql?function=basic_auth_password#function) functions.
60+
The component will check if the provided password matches the stored [password hash](/examples/hash_password.sql),
61+
and if not, it will prompt the user to enter a password in a browser popup:
6262
63-
The [`sqlpage.hash_password`](functions.sql?function=hash_password#function) function can be used to
64-
[generate a secure password hash](/examples/hash_password.sql) that you need to store in your database.
63+
```sql
64+
SELECT ''authentication'' AS component,
65+
''$argon2i$v=19$m=8,t=1,p=1$YWFhYWFhYWE$oKBq5E8XFTHO2w'' AS password_hash, -- this is a hash of the password ''password''
66+
sqlpage.basic_auth_password() AS password; -- this is the password that the user entered in the browser popup
67+
```
68+
69+
You can [generate a password hash using the `hash_password` function](/examples/hash_password.sql).
70+
71+
If you want to have multiple users with different passwords,
72+
you could store them with their password hashes in the database,
73+
or just hardcode them use a `CASE` statement:
6574
6675
```sql
6776
SELECT ''authentication'' AS component,
68-
''$argon2id$v=19$m=16,t=2,p=1$TERTd0lIcUpraWFTcmRQYw$+bjtag7Xjb6p1dsuYOkngw'' AS password_hash, -- generated using sqlpage.hash_password
77+
case sqlpage.basic_auth_username()
78+
when ''admin''
79+
then ''$argon2i$v=19$m=8,t=1,p=1$YWFhYWFhYWE$oKBq5E8XFTHO2w'' -- the password is ''password''
80+
when ''user''
81+
then ''$argon2i$v=19$m=8,t=1,p=1$YWFhYWFhYWE$qsrWdjgl96ooYw'' -- the password is ''user''
82+
end AS password_hash, -- this is a hash of the password ''password''
6983
sqlpage.basic_auth_password() AS password; -- this is the password that the user entered in the browser popup
7084
```
7185
72-
You can [try the hash_password function out here](/examples/hash_password.sql).
86+
Try this example online: [SQL Basic Auth](/examples/authentication/basic_auth.sql).
87+
88+
### Advanced user session management
89+
90+
*Basic auth* is the simplest way to password-protect a page,
91+
but it is not very flexible nor user-friendly,
92+
because the browser will show an unstyled popup asking for the username and password.
7393
74-
### Usage with a login form
94+
For more advanced authentication, you can store user information and user sessions in your database.
95+
You can then use the [`form`](components.sql?component=form#component) component to create a custom login form.
96+
When the user submits the form, you check if the password is correct using the `authentication` component.
97+
You then store a unique string of numbers and letters (a session token) both in the user''s browser
98+
using the [`cookie`](components.sql?component=cookie#component) component and in your database.
99+
Then, in all the pages that require authentication, you check if the cookie is present and matches the session token in your database.
75100
76-
The most basic usage of the authentication component is to simply check if the user has sent the correct password, and if not, redirect them to a login page:
101+
You can check if the user has sent the correct password in a form, and if not, redirect them to a login page.
102+
103+
Create a login form in a file called `login.sql`:
104+
105+
```sql
106+
select ''form'' as component, ''Authentication'' as title, ''Log in'' as validate, ''create_session_token.sql'' as action;
107+
select ''Username'' as name, ''admin'' as placeholder;
108+
select ''Password'' as name, ''admin'' as placeholder, ''password'' as type;
109+
```
110+
111+
And then, in `create_session_token.sql` :
77112
78113
```sql
79114
SELECT ''authentication'' AS component,
80115
''login.sql'' AS link,
81116
''$argon2id$v=19$m=16,t=2,p=1$TERTd0lIcUpraWFTcmRQYw$+bjtag7Xjb6p1dsuYOkngw'' AS password_hash, -- generated using sqlpage.hash_password
82117
:password AS password; -- this is the password that the user sent through our form
118+
119+
-- The code after this point is only executed if the user has sent the correct password
120+
83121
```
84122
85123
and in `login.sql` :

0 commit comments

Comments
 (0)