20 |
21 | {* Main content of the child template goes here *}
22 | {include content}
23 |
24 |
25 | {* Scripts block; by default includes Nette Forms script for validation *}
26 | {block scripts}
27 |
28 | {/block}
29 |
30 |
31 |
--------------------------------------------------------------------------------
/readme.md:
--------------------------------------------------------------------------------
1 | User Authentication (Nette example)
2 | ===================================
3 |
4 | Example of user management.
5 |
6 | - User login, registration and logout (`SignPresenter`)
7 | - Command line registration (`bin/create-user.php`)
8 | - Authentication using database table (`UserFacade`)
9 | - Password hashing
10 | - Presenter requiring authentication (`DashboardPresenter`) using the `RequireLoggedUser` trait
11 | - Rendering forms using Bootstrap CSS framework
12 | - Automatic CSRF protection using a token when the user is logged in (`FormFactory`)
13 | - Separation of form factories into independent classes (`SignInFormFactory`, `SignUpFormFactory`)
14 | - Return to previous page after login (`SignPresenter::$backlink`)
15 |
16 |
17 | Installation
18 | ------------
19 |
20 | ```shell
21 | git clone https://github.com/nette-examples/user-authentication
22 | cd user-authentication
23 | composer install
24 | ```
25 |
26 | Make directories `data/`, `temp/` and `log/` writable.
27 |
28 | By default, SQLite is used as the database which is located in the `data/db.sqlite` file. If you would like to switch to a different database, configure access in the `config/local.neon` file:
29 |
30 | ```neon
31 | database:
32 | dsn: 'mysql:host=127.0.0.1;dbname=***'
33 | user: ***
34 | password: ***
35 | ```
36 |
37 | And then create the `users` table using SQL statements in the [data/mysql.sql](data/mysql.sql) file.
38 |
39 | The simplest way to get started is to start the built-in PHP server in the root directory of your project:
40 |
41 | ```shell
42 | php -S localhost:8000 www/index.php
43 | ```
44 |
45 | Then visit `http://localhost:8000` in your browser to see the welcome page.
46 |
47 | It requires PHP version 8.1 or newer.
48 |
--------------------------------------------------------------------------------
/www/.htaccess:
--------------------------------------------------------------------------------
1 | # Apache configuration file (see https://httpd.apache.org/docs/current/mod/quickreference.html)
2 |
3 | # Allow access to all resources by default
4 | Require all granted
5 |
6 | # Disable directory listing for security reasons
7 |
8 | Options -Indexes
9 |
10 |
11 | # Enable pretty URLs (removing the need for "index.php" in the URL)
12 |
13 | RewriteEngine On
14 |
15 | # Uncomment the next line if you want to set the base URL for rewrites
16 | # RewriteBase /
17 |
18 | # Force usage of HTTPS (secure connection). Uncomment if you have SSL setup.
19 | # RewriteCond %{HTTPS} !on
20 | # RewriteRule .? https://%{HTTP_HOST}%{REQUEST_URI} [R=301,L]
21 |
22 | # Permit requests to the '.well-known' directory (used for SSL verification and more)
23 | RewriteRule ^\.well-known/.* - [L]
24 |
25 | # Block access to hidden files (starting with a dot) and URLs resembling WordPress admin paths
26 | RewriteRule /\.|^\.|^wp- - [F]
27 |
28 | # Return 404 for missing files with specific extensions (images, scripts, styles, archives)
29 | RewriteCond %{REQUEST_FILENAME} !-f
30 | RewriteRule \.(pdf|js|mjs|ico|gif|jpg|jpeg|png|webp|avif|svg|css|rar|zip|7z|tar\.gz|map|eot|ttf|otf|woff|woff2)$ - [L]
31 |
32 | # Front controller pattern - all requests are routed through index.php
33 | RewriteCond %{REQUEST_FILENAME} !-f
34 | RewriteCond %{REQUEST_FILENAME} !-d
35 | RewriteRule . index.php [L]
36 |
37 |
38 | # Enable gzip compression for text files
39 |
40 | AddOutputFilterByType DEFLATE text/html text/plain text/xml text/css application/javascript application/json application/xml application/rss+xml image/svg+xml
41 |
42 |
--------------------------------------------------------------------------------
/app/Bootstrap.php:
--------------------------------------------------------------------------------
1 | rootDir = dirname(__DIR__);
23 |
24 | // The configurator is responsible for setting up the application environment and services.
25 | // Learn more at https://doc.nette.org/en/bootstrap
26 | $this->configurator = new Configurator;
27 |
28 | // Set the directory for temporary files generated by Nette (e.g. compiled templates)
29 | $this->configurator->setTempDirectory($this->rootDir . '/temp');
30 | }
31 |
32 |
33 | public function bootWebApplication(): Nette\DI\Container
34 | {
35 | $this->initializeEnvironment();
36 | $this->setupContainer();
37 | return $this->configurator->createContainer();
38 | }
39 |
40 |
41 | public function initializeEnvironment(): void
42 | {
43 | // Nette is smart, and the development mode turns on automatically,
44 | // or you can enable for a specific IP address it by uncommenting the following line:
45 | // $this->configurator->setDebugMode('secret@23.75.345.200');
46 |
47 | // Enables Tracy: the ultimate "swiss army knife" debugging tool.
48 | // Learn more about Tracy at https://tracy.nette.org
49 | $this->configurator->enableTracy($this->rootDir . '/log');
50 | }
51 |
52 |
53 | private function setupContainer(): void
54 | {
55 | // Load configuration files
56 | $configDir = $this->rootDir . '/config';
57 | $this->configurator->addConfig($configDir . '/common.neon');
58 | $this->configurator->addConfig($configDir . '/services.neon');
59 | }
60 | }
61 |
--------------------------------------------------------------------------------
/app/Presentation/form-bootstrap5.latte:
--------------------------------------------------------------------------------
1 | {* Generic form template for Bootstrap v5 *}
2 |
3 | {define bootstrap-form, $name}
4 |
12 | {/define}
13 |
14 |
15 | {define local controls, array $controls}
16 | {* Loop over form controls and render each one *}
17 |