Authentication plugin: install & use

Recapitulation/Recipe (for use after a fresh install of CakePHP 4.0.5):

  1. Install plugin.
    composer require cakephp/authentication:^2.0
    Installs plugin authentication in ‘./vendor/cakephp/authentication’.

  2. Load the plugin.
    Edit ‘./src/Application.php’.
    Add use of authentication.
    Expand function ‘bootstrap’.
    Add function ‘getAuthenticationService’.
    Expand function ‘middleware’.

  3. Load the authentication component.
    Edit ‘./src/Controller/AppController.php’.
    Expand function ‘initialize’.

  4. Make it possible to access pages without having to log in (for example home.php on localhost:8765).
    Edit ‘./src/Controller/PagesController.php’.
    Add function ‘beforeFilter’. The action to allowUnauthenticated is ‘display’.

  5. Make it possible to ‘index’ and ‘view’ of TableX without having to log in.
    Edit ‘./src/Controller/TableXController.php’.
    Add function ‘beforeFilter’.

  6. Add a Users-table (email, password) to the database .

  7. Generate MVC Users
    Do the ‘cake bake all users’.

  8. Create login- en logout-function.
    Edit ‘./src/Controller/UsersController.php’.
    Add function ‘beforeFilter’ to make it possible to login and add a new user withouth having to be logged in.
    Add function ‘login’ .
    Add function ‘logout’.

  9. Create the login-page.
    Add the code below the sentence ‘Add the template logic for your login action’ on the CMS Tutorial - Authentication page to a new file ‘./templates/Users/login.php’.

  10. Enable password hasher.
    Edit ‘.scr/Model/Entity/User.php’.
    Add use of PasswordHasher.
    Add function _setPassword.

2 Likes