forked from sqlpage/SQLPage
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
update documentation
- Loading branch information
Showing
8 changed files
with
113 additions
and
62 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
16 changes: 16 additions & 0 deletions
16
examples/official-site/examples/authentication/create_session_token.sql
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
-- delete expired sessions | ||
delete from user_sessions where created_at < datetime('now', '-1 day'); | ||
|
||
-- check that the | ||
SELECT 'authentication' AS component, | ||
'login.sql?failed' AS link, -- redirect to the login page on error | ||
(SELECT password_hash FROM users WHERE username = :Username) AS password_hash, -- this is a hash of the password 'admin' | ||
:Password AS password; -- this is the password that the user sent through our form in 'index.sql' | ||
|
||
-- if we haven't been redirected, then the password is correct | ||
-- create a new session | ||
insert into user_sessions (session_token, username) values (sqlpage.random_string(32), :Username) | ||
returning 'cookie' as component, 'session_token' as name, session_token as value; | ||
|
||
-- redirect to the authentication example home page | ||
select 'redirect' as component, '/examples/authentication' as link; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
-- redirect the user to the login page if they are not logged in | ||
-- this query should be present at the top of every page that requires authentication | ||
set $role = (select role from users natural join user_sessions where session_token = sqlpage.cookie('session_token')); | ||
select 'redirect' as component, 'login.sql' as link where $role is null; | ||
|
||
select 'dynamic' as component, | ||
json_insert(properties, '$[0].menu_item[#]', 'logout') as properties | ||
FROM example WHERE component = 'shell' LIMIT 1; | ||
|
||
select 'alert' as component, 'info' as color, CONCAT('You are logged in as ', $role) as title; | ||
|
||
select 'text' as component, ' | ||
# Authentication | ||
Read the [source code](//github.com/lovasoa/SQLpage/blob/main/examples/official-site/examples/authentication/) for this demo. | ||
[Log out](logout.sql) | ||
' as contents_md; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
select 'dynamic' as component, properties FROM example WHERE component = 'shell' LIMIT 1; | ||
|
||
select 'form' as component, 'Authentication' as title, 'Log in' as validate, 'create_session_token.sql' as action; | ||
select 'Username' as name, 'user' as prefix_icon, 'admin' as placeholder; | ||
select 'Password' as name, 'lock' as prefix_icon, 'admin' as placeholder, 'password' as type; | ||
|
||
select 'alert' as component, 'danger' as color, 'Invalid username or password' as title where $failed is not null; | ||
|
||
select 'text' as component, ' | ||
# Authentication | ||
This is a simple example of an authentication form. | ||
It uses | ||
- the [`form`](/documentation.sql?component=form#component) component to create a login form | ||
- the [`authentication`](/documentation.sql?component=authentication#component) component to check the user password | ||
- the [`cookie`](/documentation.sql?component=cookie#component) component to store a unique session token in the user browser | ||
- the [`redirect`](/documentation.sql?component=redirect#component) component to redirect the user to the login page if they are not logged in | ||
## Example credentials | ||
- Username: `admin`, Password: `admin` | ||
- Username: `user`, Password: `user` | ||
' as contents_md; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,4 @@ | ||
delete from user_sessions | ||
where session_token = sqlpage.cookie('session_token'); | ||
|
||
select 'redirect' as component, 'login.sql' as link; |
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
18 changes: 18 additions & 0 deletions
18
examples/official-site/sqlpage/migrations/44_authentication_example.sql
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
create table users ( | ||
username text primary key, | ||
password_hash text not null, | ||
role text not null | ||
); | ||
|
||
-- Create example users with trivial passwords for the website's demo | ||
insert into users (username, password_hash, role) | ||
values | ||
('admin', '$argon2i$v=19$m=8,t=1,p=1$YWFhYWFhYWE$ROyXNhK0utkzTA', 'admin'), -- password: admin | ||
('user', '$argon2i$v=19$m=8,t=1,p=1$YWFhYWFhYWE$qsrWdjgl96ooYw', 'user'); -- password: user | ||
-- (the password hashes can be generated using the `sqlpage.hash_password` function) | ||
|
||
create table user_sessions ( | ||
session_token text primary key, | ||
username text not null references users(username), | ||
created_at timestamp not null default current_timestamp | ||
); |