[ Web Proxy ]
URL:
Viewing: https://leafphp.dev/docs/auth/ [Back]  [Original]

Authentication | Leaf PHP
Skip to content
Menu
Return to top
Sidebar Navigation SearchK
On this page
    CREATIVE LEAF

    We build products too.

    From the creators of Leaf PHP, a studio that's shipped frameworks and products used by millions. Let's build yours.

    Start your project
    Open with AI

    Authentication

    Leaf Auth

    Authentication without turning your app into a maze.

    Leaf Auth gives you login, registration, sessions, JWT, password hashing, user management, protected routes, and database-backed identity with a small API you can understand and extend.

    install
    $ leaf install auth
    login, signup, sessions, JWT, middleware

    Auth covers

    User identity

    Register, login, logout, and access the authenticated user.

    Route protection

    Protect pages, dashboards, APIs, and role-aware workflows.

    AI-friendly setup

    Auth commands and config give assistants a clear path for secure user flows.

    Using Leaf MVC?

    Use the MVC auth guide when you want controllers, routes, config files, and app structure.

    Open MVC auth

    Setting up

    You can install Leaf Auth using the Leaf CLI:

    Leaf CLIComposer
    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:

    Auth connectExisting PDO instance
    php
    auth()->connect([
      'dbtype' => '...',
      'charset' => '...',
      'port' => '...',
      'host' => '...',
      'dbname' => '...',
      'username' => '...',
      '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:

      LeafLeaf MVC - config/auth.php
      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:

      LeafLeaf MVC - config/auth.php
      php
      auth()->config('db.table', 'admins');
      php
      'db.table' => 'admins'
    Pager

    Web Proxy Viewer  |  New URL  |  Original Page