Skip to content

Authentication

Numerous web applications offer their users a means to authenticate and access the application by "logging in." Adding this functionality to web applications can be a challenging and potentially dangerous task.

Leaf provides a lightweight but very powerful authentication system to handle all the complexities of authentication in a few lines of code. We understand that authentication is a critical part of your application, so we've made it as simple and secure as possible.

Using Leaf MVC?

We've crafted a specialized guide for auth in Leaf MVC. While it's similar to the basic routing in Leaf, it's more detailed and tailored for Leaf MVC.

Start building

Setting up

You can install Leaf Auth using the Leaf CLI:

bash
leaf install auth
bash
composer require leafs/auth

The next step is to link your database and start signing users in.

Connecting to a database

To do any kind of authentication, you need to connect to some kind of database which will store your users' data. If you are already using Leaf DB or Leaf MVC, then your database connection will automatically be used by Leaf Auth, so you don't need to connect to your database again.

If you are NOT using Leaf DB or Leaf MVC, you can connect to your database manually:

php
auth()->connect([
  'dbtype' => '...',
  'charset' => '...',
  'port' => '...',
  'host' => '...',
  'dbname' => '...',
  'user' => '...',
  'password' => '...'
]);
php
$db = new PDO('mysql:dbname=test;host=127.0.0.1', 'root', '');

auth()->dbConnection($db);

// you can use leaf auth the same way you always have

Database Considerations

Leaf Auth doesn't give you any structure for your database, with that, you can structure your database in any way you prefer. However, there are some things you should note:

  1. By default, Leaf Auth assumes that your database primary key is id. If you have a database where you are using another field, say admin_id as the primary key, you will need to tell Leaf the name of your primary key. You can do this using the id.key config:

    php
    auth()->config('id.key', 'admin_id');
    php
    'id.key' => 'admin_id'
  2. Leaf Auth assumes that you will save your users in a database table named users, this might however not be the case for your application. If you want to use a different table, you can configure Leaf Auth using db.table:

    php
    auth()->config('db.table', 'admins');
    php
    'db.table' => 'admins'

Released under the MIT License.