Skip to content

9.1 Authentication

DA edited this page May 1, 2019 · 30 revisions

Authentication is key to any transactional web application. This framework provides a quick and easy way to have a basic authentication into your application by providing the necessary models, views and controllers readily available.

users table

As part of this framework, a MySql script is provided to create the basic users table to put in a place an authentication mechanism. This script can be found in application/models/Auth/create_db.sql.

This script can be run from either the Terminal or from a DB manager application such phpMyAdmin.

Run from the Terminal

~: cd /application/models/Auth/
~: mysql -u <username> -p <databasename> > create_db.sql

Run from phpMyAdmin

  • Click on the database name that you created
  • Click Import from the action menu
  • Browse to application/Models/Auth
  • Select the file create_db.sql
  • Click on Go

User Model

The framework has a User Model already created for use that can be found in /application/Models/Auth/User.php. This model is a representation of the database table that was previously created. Both the table and the model have basic attributes id, username, passcode, permissions, created_at, modified_at.

Before making any changes to both the table and the model, we recommend that you understand how the authentication mechanism works and how the out-of-the-box controllers and views are implemented

Authentication Controller

The authentication controller is the controller that will allow interfacing with the User model, the users table and the different authentication views (explained in the next section). This controller can be found in /application/Controllers/Auth/AuthController.php.

This controller is shipped with 4 methods that can be accessed from the route

  • index(): This method is called from a GET route and it returns the index view that has 2 states, authorized and not authorized.
  • register(): This method is called from a POST route to create a new record in the users tables.
  • login(): This method is called from a POST route to validate the credentials entered against a record in the users table.
  • logout(): This method is called from a GET route and calls a logout core helper method that unauthorizes a user and redirects to a specified URL after logging out.

Authentication Views

This framework comes with several views out-of-the-box to perform the basic authentication interactions.

  • /application/Views/default/index.pug: This view acts as a welcome page that has 2 states, authorized and unauthorized - to help illustrate how authentication works.
  • /application/Views/Auth/login.pug: This view has a basic login form with basic field validations.
  • **/application/Views/Auth/register.pug: ** This view has a basic registration form with basic field validations.
Clone this wiki locally