Skip to content

Auth v2.0


You can install Leaf Db with Leaf CLI:

leaf install auth@2.0

Or with composer:

composer require leafs/auth v2.0

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.


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

$auth = new Leaf\Auth;

// syntax
  $host = '',
  string $dbname = '',
  string $user = '',
  string $password = ''

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


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:



$auth = new Leaf\Auth;

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=', 'root', '');


// 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.


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.


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


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

// you can use auth straight away without any connect

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.


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


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


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


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


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


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

Next Steps

Auth v2.0 has loaded