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
Copy file name to clipboardExpand all lines: examples/official-site/sqlpage/migrations/07_authentication.sql
+47-9Lines changed: 47 additions & 9 deletions
Original file line number
Diff line number
Diff line change
@@ -54,32 +54,70 @@ VALUES (
54
54
55
55
### Usage with HTTP basic authentication
56
56
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
60
58
[`sqlpage.basic_auth_username()`](functions.sql?function=basic_auth_username#function) and
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:
62
62
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:
65
74
66
75
```sql
67
76
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''
69
83
sqlpage.basic_auth_password() AS password; -- this is the password that the user entered in the browser popup
70
84
```
71
85
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.
73
93
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.
75
100
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` :
77
112
78
113
```sql
79
114
SELECT ''authentication'' AS component,
80
115
''login.sql'' AS link,
81
116
''$argon2id$v=19$m=16,t=2,p=1$TERTd0lIcUpraWFTcmRQYw$+bjtag7Xjb6p1dsuYOkngw'' AS password_hash, -- generated using sqlpage.hash_password
82
117
: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
0 commit comments