PHPackages                             sinbadxiii/phalcon-auth-jwt - 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. sinbadxiii/phalcon-auth-jwt

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

sinbadxiii/phalcon-auth-jwt
===========================

Basic Phalcon JWT Auth

v1.0.0-alpha3(2y ago)37.5k↓50%3MITPHPPHP ^7.4 || ^8.1

Since Sep 10Pushed 7mo ago3 watchersCompare

[ Source](https://github.com/sinbadxiii/phalcon-auth-jwt)[ Packagist](https://packagist.org/packages/sinbadxiii/phalcon-auth-jwt)[ RSS](/packages/sinbadxiii-phalcon-auth-jwt/feed)WikiDiscussions master Synced 1mo ago

READMEChangelog (6)Dependencies (5)Versions (10)Used By (0)

Phalcon JWT Auth
================

[](#phalcon-jwt-auth)

Example micro app [sinbadxiii/phalcon-auth-jwt-example](https://github.com/sinbadxiii/phalcon-auth-jwt-example)

Additional JWT guard for the Phalcon authentication library [sinbadxiii/phalcon-auth](https://github.com/sinbadxiii/phalcon-auth)

[![Banner](https://github.com/sinbadxiii/images/raw/master/phalcon-auth-jwt/logo.png?raw=true)](https://github.com/sinbadxiii/images/blob/master/phalcon-auth-jwt/logo.png?raw=true)

[![Software License](https://camo.githubusercontent.com/c090e080484e2a2bc766446291d04437db823929042bf614b26a1643660ddf6f/68747470733a2f2f696d672e736869656c64732e696f2f62616467652f6c6963656e73652d4d49542d627269676874677265656e3f7374796c653d666c61742d737175617265)](LICENSE)[![Packagist Downloads](https://camo.githubusercontent.com/21f9f142dd7fac56fd697190e2377ee1c2b70f6bc2d040ea0e67c9b5e045a16f/68747470733a2f2f696d672e736869656c64732e696f2f7061636b61676973742f64742f73696e626164786969692f7068616c636f6e2d617574682d6a77743f7374796c653d666c61742d737175617265)](https://packagist.org/packages/sinbadxiii/phalcon-auth-jwt)[![Latest Version](https://camo.githubusercontent.com/6b9c7b3d94f47cb5b259f061f54a2c1ef2aa73761faa2cd608625e94f408ed6b/68747470733a2f2f696d672e736869656c64732e696f2f6769746875622f72656c656173652f73696e626164786969692f7068616c636f6e2d617574682d6a77743f7374796c653d666c61742d737175617265)](https://github.com/sinbadxiii/phalcon-auth-jwt/releases)

Demo
----

[](#demo)

[![Banner](https://github.com/sinbadxiii/images/raw/master/phalcon-auth-jwt/howusage.gif?raw=true)](https://github.com/sinbadxiii/images/blob/master/phalcon-auth-jwt/howusage.gif?raw=true)

Requirements
------------

[](#requirements)

Phalcon: ^5

PHP: ^7.4 || ^8.1

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

[](#installation)

### Install via composer

[](#install-via-composer)

Run the following command to pull in the latest version::

`composer require "sinbadxiii/phalcon-auth-jwt"`

### Add service provider

[](#add-service-provider)

```
use Sinbadxiii\PhalconAuthJWT\Blacklist;
use Sinbadxiii\PhalconAuthJWT\Builder;
use Sinbadxiii\PhalconAuthJWT\Http\Parser\Chains\AuthHeaders;
use Sinbadxiii\PhalconAuthJWT\Http\Parser\Chains\InputSource;
use Sinbadxiii\PhalconAuthJWT\Http\Parser\Chains\QueryString;
use Sinbadxiii\PhalconAuthJWT\Http\Parser\Parser;
use Sinbadxiii\PhalconAuthJWT\JWT;
use Sinbadxiii\PhalconAuthJWT\Manager as JWTManager;

$di->setShared("jwt", function () {

    $configJwt = $this->getConfig()->path('jwt');

    $providerJwt = $configJwt->providers->jwt;

    $builder = new Builder();

    $builder->lockSubject($configJwt->lock_subject)
        ->setTTL($configJwt->ttl)
        ->setRequiredClaims($configJwt->required_claims->toArray())
        ->setLeeway($configJwt->leeway)
        ->setMaxRefreshPeriod($configJwt->max_refresh_period);

    $parser = new Parser($this->getRequest(), [
        new AuthHeaders,
        new QueryString,
        new InputSource,
    ]);

    $providerStorage = $configJwt->providers->storage;

    $blacklist = new Blacklist(new $providerStorage($this->getCache()));

    $blacklist->setGracePeriod($configJwt->blacklist_grace_period);

    $manager = new JWTManager(new $providerJwt(
        $configJwt->secret,
        $configJwt->algo,
        $configJwt->keys->toArray()
    ), $blacklist, $builder);

    $manager->setBlacklistEnabled((bool) $configJwt->blacklist_enabled);

    return new JWT($builder, $manager, $parser);
});
```

### Configuration

[](#configuration)

Copy file from `config/jwt.php` in your folder config and merge your config

### Generate secret key

[](#generate-secret-key)

Update the `secret` value in config jwt.php or JWT\_SECRET value in your .env file.

*Generate a 32 character secret phrase like here*

### Update your User model

[](#update-your-user-model)

Firstly you need to implement the Sinbadxiii\\PhalconAuthJWT\\JWTSubject contract on your User model, which requires that you implement the 2 methods `getJWTIdentifier()` and `getJWTCustomClaims()`.

The example below:

```
