Skip to content
On this page

Leaf Auth

Installation

You can install Leaf Db with Leaf CLI:

leaf install auth

Or with composer:

composer require leafs/auth

From there, you can link your database and start writing some awesome queries.

Db Connection

After installing leaf auth, you would need to connect to a database. Leaf auth will search for users and add/update users in this database when a login/register or update operation is called. There are a couple of ways to connect to a database with leaf auth.

connect

The connect method allows you to pass in your database connection parameters directly to leaf auth.

$auth = new Leaf\Auth;

// syntax
$auth->connect(
  $host = '',
  string $dbname = '',
  string $user = '',
  string $password = ''
);

// example
$auth->connect('127.0.0.1', 'dbname', 'root', '');

autoConnect

This method allows you to connect to your database from parameters in a .env file. Most MVC frameworks and other libraries rely on a .env for a lot of configurations including the database. With autoConnect, you can directly pick up these configs.

example env:

DB_CONNECTION=mysql
DB_HOST=127.0.0.1
DB_PORT=3306
DB_DATABASE=LeafMVC
DB_USERNAME=root
DB_PASSWORD=

App:

$auth = new Leaf\Auth;
$auth->autoConnect();

PDO connection

Leaf Auth also allows you to skip the entire connection process and share an existing PDO instance with leaf db. This allows you to gradually rewrite your existing apps with Leaf Auth without having multiple db connections and doing so at your own pace.

$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

Leaf Db has been rewritten based on PDO, this also means that you can pass your leaf db connection into leaf auth directly.

$auth->dbConnection(db()->connection());

Leaf db (auth v2 + leaf 3 only)

If you are using leaf auth in a leaf 3 app, you will have access to the auth global as shown in some of the above connections. Along with this, if you already have a leaf db connection, you no longer need to explicitly connect to your database. Leaf auth searches for a leaf db instance and connects to it automatically.

Note

This only works in a leaf 3 app and only if you already have a leaf db connection.

<?php

db()->connect('127.0.0.1', 'dbname', 'username', 'password');

// you can use auth straight away without any connect
auth()->login(...);

Functional Mode

If you are using leaf auth v2 in a leaf 3 app, you will have access to the auth global which allows you to use Leaf Auth from anywhere in your entire application. You simply need to call auth() and leaf 3 will create and maintain a shared instance of Leaf auth which you can call from anywhere.

This also means that you don't need to initialize leaf auth anymore.

<?php

require __DIR__ . "/vendor/autoload.php";

auth()->autoConnect();

app()->get("/", function () {
  // auth can be used here
  // auth()->...
});

app()->run();

Functional mode also makes the guard, hasAuth and sessionUser globals available to you from anywhere.

guard

The guard method is a shortcut method for Auth::guard(). You can find the guards documentation here.

hasAuth

hasAuth returns a boolean which is whether there's an active user session or not.

sessionUser

This method returns the active session user or null if there's no session user.

Next Steps

Leaf Auth has loaded