PHPackages                             sebk/small-swoft-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. [Authentication &amp; Authorization](/categories/authentication)
4. /
5. sebk/small-swoft-auth

ActiveLibrary[Authentication &amp; Authorization](/categories/authentication)

sebk/small-swoft-auth
=====================

Swoft jwt auth based on small-orm

1.1.2(4y ago)024GPL-3.0-onlyPHP

Since Oct 20Pushed 4y ago1 watchersCompare

[ Source](https://github.com/sebk69/small-swoft-auth)[ Packagist](https://packagist.org/packages/sebk/small-swoft-auth)[ RSS](/packages/sebk-small-swoft-auth/feed)WikiDiscussions master Synced 3w ago

READMEChangelog (4)Dependencies (5)Versions (5)Used By (0)

sebk/small-swoft-auth
=====================

[](#sebksmall-swoft-auth)

Jwt auth for Swoft based on small-orm

Include a controller superclass to simple implement token support in your controllers and link with voters to manage user rigths

Install
-------

[](#install)

Create your Swoft project :

Install dependencies

```
composer require sebk/small-orm-core
composer require sebk/small-orm-forms
composer require sebk/swoft-voter

```

Require Swoft Voter package () :

```
composer require sebk/small-swoft-auth

```

Documentation
-------------

[](#documentation)

### Parameter

[](#parameter)

In base.php, register AuthManagerService :

```
return [
...
    \Swoft\Auth\Contract\AuthManagerInterface::class => [
        'class' => \Sebk\SmallSwoftAuth\Service\AuthManagerService::class
    ],
...
];

```

In app.php, register app.user according to your app to tell AuthManager to request you're app user:

```
return [
...
    'user' => [
        'dao' => ['GenericBundle', 'User'],
        'accountField' => 'login',
        'identityField' => 'id',
    ],
...
];

```

### Implement UserModelInterface

[](#implement-usermodelinterface)

You're application user model must implement UserModelInterface. (In this example, password stored via md5 hash for simplicity. Don't use md5 hash, prefere SHA-256 hash or more for security reasons) :

```
use Sebk\SmallSwoftAuth\Interfaces\UserModelInterface;

class User extends Model implements UserModelInterface
{

    /**
     * Check user password
     * @param string $password
     * @return bool
     */
    public function checkPassword(string $password)
    {
        return $this->getPassword() == md5($password);
    }

}

```

### Implement your voters

[](#implement-your-voters)

See  for config and implementation

### Implement login

[](#implement-login)

### Implement your controllers

[](#implement-your-controllers)

To protect your controller :

- use AuthMiddleware of swoft/auth
- your controller must extends TokenSecuredController abstract class

Additionnaly, you can protect a route via specific rules of your app by using voter on your controller object using denyAccessUnlessGranted method :

```
$this->denyAccessUnlessGranted(VoterInterface::ATTRIBUTE_READ, $this);

```

Here is a controller example :

```
