daycry/jwt

JWT Token for Codeigniter 4

v1.0.6 2023-12-15 10:21 UTC

This package is auto-updated.

Last update: 2024-12-15 12:39:45 UTC


README

Donate

JWT

JWT for Codeigniter 4

Build Status Coverage Status Downloads GitHub release (latest by date) GitHub stars GitHub license

Installation via composer

Use the package with composer install

> composer require daycry/jwt

Manual installation

Download this repo and then enable it by editing app/Config/Autoload.php and adding the Daycry\JWT namespace to the $psr4 array. For example, if you copied it into app/ThirdParty:

$psr4 = [
    'Config'      => APPPATH . 'Config',
    APP_NAMESPACE => APPPATH,
    'App'         => APPPATH,
    'Daycry\JWT' => APPPATH .'ThirdParty/JWT/src',
];

Configuration

Run command:

> php spark jwt:publish

This command will copy a config file to your app namespace. Then you can adjust it to your needs. By default file will be present in app/Config/JWT.php.

Usage Loading Library

<?php 

$library = new \Daycry\JWT\JWT();
$encode = $this->library->encode('hello');

If you want change attributes before controller call:

<?php
$config = config('JWT');
$library = new \Daycry\JWT\JWT($config);
$encode = $this->library->encode('hello');

$decode = $this->library->decode($encode);

$decode->get('data');

By default it is saved in the data parameter, but it can be modified as follows

<?php
$config = config('JWT');
$library = (new \Daycry\JWT\JWT($config))->setParamData('another');
$encode = $this->library->encode('hello');

$decode = $this->library->decode($encode);

$decode->get('another');

If you want save an array data

<?php
$config = config('JWT');
$library = (new \Daycry\JWT\JWT($config))->setParamData('another');
$encode = $this->library->encode(array( 'first' => '1', 'second' => '2' ));

$decode = $this->library->decode($encode);

\json_decode($decode->get('another'));

If you want to save the data array separately

<?php
$config = config('JWT');
$library = (new \Daycry\JWT\JWT($config))->setSplitData();
$encode = $this->library->encode(array( 'first' => '1', 'second' => '2' ));

$decode = $this->library->decode($encode);

$decode->get('first');
$decode->get('second');