PHPackages                             leafs/auth - PHPackages - PHPackages  [Skip to content](#main-content)[PHPackages](/)[Directory](/)[Categories](/categories)[Trending](/trending)[Leaderboard](/leaderboard)[Changelog](/changelog)[Analyze](/analyze)[Collections](/collections)[Log in](/login)[Sign up](/register)

1. [Directory](/)
2. /
3. [Framework](/categories/framework)
4. /
5. leafs/auth

ActiveLibrary[Framework](/categories/framework)

leafs/auth
==========

Leaf PHP auth helper

v5.0(3mo ago)623.0k↓12.5%6[5 issues](https://github.com/leafsphp/auth/issues)4MITPHPCI passing

Since Sep 19Pushed 2mo ago1 watchersCompare

[ Source](https://github.com/leafsphp/auth)[ Packagist](https://packagist.org/packages/leafs/auth)[ Docs](https://leafphp.netlify.app/#/)[ GitHub Sponsors](https://github.com/leafsphp)[ Fund](https://opencollective.com/leaf)[ RSS](/packages/leafs-auth/feed)WikiDiscussions v4.x Synced 1mo ago

READMEChangelog (10)Dependencies (10)Versions (42)Used By (4)

 [![](https://camo.githubusercontent.com/d98ee5e32c2ff016fdfdac6c42654a908f4cc34b229c7b00caacc5a717455ae8/68747470733a2f2f6c6561667068702e6465762f6c6f676f2d636972636c652e706e67)](https://camo.githubusercontent.com/d98ee5e32c2ff016fdfdac6c42654a908f4cc34b229c7b00caacc5a717455ae8/68747470733a2f2f6c6561667068702e6465762f6c6f676f2d636972636c652e706e67)

Leaf Auth
=========

[](#leaf-auth)

[![PHPStan: Level 8](https://camo.githubusercontent.com/f60d96f7c2579690ab6dfa8918f777fe93a02a92301c661eb38a85861a92b780/68747470733a2f2f696d672e736869656c64732e696f2f62616467652f5048505374616e2d6c6576656c253230382d627269676874677265656e2e7376673f7374796c653d666c6174)](https://camo.githubusercontent.com/f60d96f7c2579690ab6dfa8918f777fe93a02a92301c661eb38a85861a92b780/68747470733a2f2f696d672e736869656c64732e696f2f62616467652f5048505374616e2d6c6576656c253230382d627269676874677265656e2e7376673f7374796c653d666c6174)[![Latest Stable Version](https://camo.githubusercontent.com/ba9bf853550eab989a5a36cf2bfb838e0469b7aed8f6a5ba0801c562d469b839/68747470733a2f2f706f7365722e707567782e6f72672f6c656166732f617574682f762f737461626c65)](https://packagist.org/packages/leafs/auth)[![Total Downloads](https://camo.githubusercontent.com/11e872bb341bdb7d8bd8de4201d93ab597f2bc0719e560761019e768eea9b51e/68747470733a2f2f706f7365722e707567782e6f72672f6c656166732f617574682f646f776e6c6f616473)](https://packagist.org/packages/leafs/auth)[![License](https://camo.githubusercontent.com/10fece7f69f932ef0e10f3930de4e34e96f9041509fd8297ee968beb96bd09dd/68747470733a2f2f706f7365722e707567782e6f72672f6c656166732f617574682f6c6963656e7365)](https://packagist.org/packages/leafs/auth)

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.

Installation
------------

[](#installation)

You can easily install Leaf Auth using the Leaf CLI:

```
leaf install auth
```

Or via composer:

```
composer require leafs/auth
```

Connecting to a database
------------------------

[](#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.

```
auth()->connect([
  'dbtype' => '...',
  'charset' => '...',
  'port' => '...',
  'host' => '...',
  'dbname' => '...',
  'user' => '...',
  'password' => '...'
]);
```

If you have an existing PDO connection, you can pass it to Leaf Auth:

```
$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
```

Signing a user in
-----------------

[](#signing-a-user-in)

To sign a user in, you can use the login() method. This method takes in an array of data you want to use to authenticate the user. This data is usually the user's email and password, but can be anything as long as the password field is present.

```
auth()->login([
  'email' => 'm@example.com',
  'password' => 'password'
]);
```

Signing a user up
-----------------

[](#signing-a-user-up)

To sign a user up is to create a new user account on your application. This is usually done by collecting the user's details and storing them in your database. You also need to validate the user's details to ensure they are correct and that they don't conflict with existing data.

Leaf allows you to do all this using the register() method. This method takes in an array of data you want to use to create the user.

```
auth()->register([
  'username' => 'example',
  'email' => 'm@example.com',
  'password' => 'password'
]);
```

Using Middleware
----------------

[](#using-middleware)

Leaf Auth also provides a middleware that you can use to protect your routes. The auth middleware checks if a user is logged in and allows you to set a callback function to run if a user is not logged in.

```
auth()->middleware('auth.required', function () {
  response()->redirect('/login');
});
```

Once you have defined a callback for the middleware, you can use it in your routes like this:

```
app()->get('/protected', ['middleware' => 'auth.required', function () {
  // this route is protected
}]);

// or on a route group
app()->group('/protected', ['middleware' => 'auth.required', function () {
  app()->get('/route', function () {
    // this route is protected
  });
}]);
```

You can find the full documentation [here](https://leafphp.dev/docs/auth/protected-routes.html)

Stay In Touch
-------------

[](#stay-in-touch)

- [Twitter](https://twitter.com/leafphp)
- [Join the forum](https://github.com/leafsphp/leaf/discussions/37)
- [Chat on discord](https://discord.com/invite/Pkrm9NJPE3)

Learning Leaf PHP
-----------------

[](#learning-leaf-php)

- Leaf has a very easy to understand [documentation](https://leafphp.dev) which contains information on all operations in Leaf.
- You can also check out our [youtube channel](https://www.youtube.com/channel/UCllE-GsYy10RkxBUK0HIffw) which has video tutorials on different topics
- You can also learn from [codelabs](https://leafphp.dev/codelabs/) and contribute as well.

Contributing
------------

[](#contributing)

We are glad to have you. All contributions are welcome! To get started, familiarize yourself with our [contribution guide](https://leafphp.dev/community/contributing.html) and you'll be ready to make your first pull request 🚀.

To report a security vulnerability, you can reach out to [@mychidarko](https://twitter.com/mychidarko) or [@leafphp](https://twitter.com/leafphp) on twitter. We will coordinate the fix and eventually commit the solution in this project.

Sponsoring Leaf
---------------

[](#sponsoring-leaf)

We are committed to keeping Leaf open-source and free, but maintaining and developing new features now requires significant time and resources. As the project has grown, so have the costs, which have been mostly covered by the team. To sustain and grow Leaf, we need your help to support full-time maintainers.

You can sponsor Leaf and any of our packages on [open collective](https://opencollective.com/leaf) or check the [contribution page](https://leafphp.dev/support/) for a list of ways to contribute.

And to all our [existing cash/code contributors](https://leafphp.dev#sponsors), we love you all ❤️

###  Health Score

49

—

FairBetter than 95% of packages

Maintenance66

Regular maintenance activity

Popularity34

Limited adoption so far

Community21

Small or concentrated contributor base

Maturity63

Established project with proven stability

 Bus Factor1

Top contributor holds 83% of commits — single point of failure

How is this calculated?**Maintenance (25%)** — Last commit recency, latest release date, and issue-to-star ratio. Uses a 2-year decay window.

**Popularity (30%)** — Total and monthly downloads, GitHub stars, and forks. Logarithmic scaling prevents top-heavy scores.

**Community (15%)** — Contributors, dependents, forks, watchers, and maintainers. Measures real ecosystem engagement.

**Maturity (30%)** — Project age, version count, PHP version support, and release stability.

###  Release Activity

Cadence

Every ~43 days

Recently: every ~74 days

Total

38

Last Release

89d ago

Major Versions

v1.1.2 → v2.02022-01-24

v1.2.1 → v2.1.32022-02-17

v2.4 → v3.02024-11-06

v3.x-dev → v4.02025-02-06

v4.2.1 → v5.02026-02-18

### Community

Maintainers

![](https://avatars.githubusercontent.com/u/29547806?v=4)[Mychi](/maintainers/Mychi)[@mychi](https://github.com/mychi)

---

Top Contributors

[![mychidarko](https://avatars.githubusercontent.com/u/26604242?v=4)](https://github.com/mychidarko "mychidarko (166 commits)")[![fadrian06](https://avatars.githubusercontent.com/u/109766973?v=4)](https://github.com/fadrian06 "fadrian06 (23 commits)")[![crosa7](https://avatars.githubusercontent.com/u/32076460?v=4)](https://github.com/crosa7 "crosa7 (8 commits)")[![terrybr](https://avatars.githubusercontent.com/u/378788?v=4)](https://github.com/terrybr "terrybr (2 commits)")[![milosPavic](https://avatars.githubusercontent.com/u/7430264?v=4)](https://github.com/milosPavic "milosPavic (1 commits)")

---

Tags

authenticationleafphpphpphpframeworkAuthenticationloginregisterleafsimple auth

###  Code Quality

TestsPest

Static AnalysisRector

Code StylePHP CS Fixer

### Embed Badge

![Health badge](/badges/leafs-auth/health.svg)

```
[![Health](https://phpackages.com/badges/leafs-auth/health.svg)](https://phpackages.com/packages/leafs-auth)
```

###  Alternatives

[leafs/leaf

Elegant PHP for modern developers

1.3k44.3k9](/packages/leafs-leaf)

PHPackages © 2026

[Directory](/)[Categories](/categories)[Trending](/trending)[Changelog](/changelog)[Analyze](/analyze)
