|
| 1 | +# User authentication demo |
| 2 | + |
| 3 | +This example demonstrates how to manually handle user authentication with SQLpage and PostgreSQL. |
| 4 | +All the user and password management is done in the database, using the standard [pgcrypto](https://www.postgresql.org/docs/current/pgcrypto.html) postgresql extension. |
| 5 | + |
| 6 | +This demonstrates how to implement: |
| 7 | + - [a signup form](./sign%20up.sql) |
| 8 | + - [a login form](./sign%20in.sql) |
| 9 | + - [a logout button](./logout.sql) |
| 10 | + - [secured pages](./protected_page.sql) that can only be accessed by logged-in users |
| 11 | + |
| 12 | +User authentication is a complex topic, and you can follow the work on implementing differenet authentication methods in [this issue](https://github.com/lovasoa/SQLpage/issues/12). |
| 13 | + |
| 14 | +## Caveats |
| 15 | + |
| 16 | +In this example, we store encrypted user passwords in the database, but we let the database itself handle the encryption. |
| 17 | +For that to be safe, you need to make sure that: |
| 18 | + - the database is not accessible by untrusted users |
| 19 | + - the database logs and configuration files are not accessible by untrusted users |
| 20 | + - your connection to the database is encrypted [(use SSL)](https://www.postgresql.org/docs/current/ssl-tcp.html). It should be the case by default if you use a recent version of PostgreSQL and a popular distribution. |
| 21 | + |
| 22 | +## Screenshots |
| 23 | + |
| 24 | +| Signup form | Login form | Protected page | |
| 25 | +| --- | --- | --- | |
| 26 | +|  |  |  | |
| 27 | +|  |  |  | |
| 28 | + |
| 29 | +## How it works |
| 30 | + |
| 31 | +### User creation |
| 32 | + |
| 33 | +The [a signup form](./sign%20up.sql) is a simple form that is handled by [`create_user.sql`](./create_user.sql). |
| 34 | +You could restrict user creation to existing administrators and create an initial administrator in a database migration. |
| 35 | + |
| 36 | +### User login |
| 37 | + |
| 38 | +The [a login form](./sign%20in.sql) is a simple form that is handled by [`login.sql`](./login.sql). |
| 39 | +It checks that the username exists and that the password is correct using the [pgcrypto](https://www.postgresql.org/docs/current/pgcrypto.html) extension with |
| 40 | + |
| 41 | +```sql |
| 42 | +SELECT * FROM users WHERE username = :username AND password = crypt(:password, password); |
| 43 | +``` |
| 44 | + |
| 45 | +If the login is successful, an entry is added to the [`login_session`](./sqlpage/migrations/0000_init.sql) table with a random session id. |
| 46 | +The session id is then stored in a cookie on the user's browser. |
| 47 | + |
| 48 | +The user is then redirected to [`./check_login.sql`](./check_login.sql) that checks that the session id is valid and redirects back to the login page if it is not. |
| 49 | + |
| 50 | +### Protected pages |
| 51 | + |
| 52 | +Protected pages are pages that can only be accessed by logged-in users. |
| 53 | +There is an example in [`protected_page.sql`](./protected_page.sql) that uses a simple [postgresql stored procedure](./sqlpage/migrations/0000_init.sql) |
| 54 | + to raise an error (and thus prevent content rendering) if the user is not logged in. |
| 55 | + |
| 56 | +### User logout |
| 57 | + |
| 58 | +The cookie can be deleted in the browser by navigating to [`./logout.sql`](./logout.sql). |
0 commit comments