Giter Club home page Giter Club logo

jwt-codeigniter's Introduction

Bem vindo

O repositório acima é um projeto usando CodeIgniter na versão 3.1, para testar basta entrar no diretório do projeto via linha de comando e executar

php -S localhost:80

Iniciando

Crie o helper jwt usando raw disponivel em

framework\application\helpers\jwt_helper.php

Adicione o helper ao config/autoload $autoload['helper'] = array('jwt_helper');

Crie um arquivo em config chamado jwt.php com o conteúdo:

<?php
/**
* JSON Web Token configuration params
*
* @param  string $config['jwt_secret'] Chave secreta para geração de token
* @param  string $config['jwt_expiration_time'] tempo em segundos que o token tem de vida
* @param  string $config['jwt_start_time'] tempo em segundos que o token começa a valer a partir da hora em que foi gerado
*
*/

$config['jwt_secret'] =  "OakleyTiffAtripFlackLowOuFlackJack";
$config['jwt_expiration_time'] =  3600;
$config['jwt_start_time'] =  3;

Exemplos do uso do helper no seu controller extends CI_Controller:

public  function  token(){
	$this->config->load('jwt');
	$jwt  =  new  JWT;
	$JwtSecretKey  =  $this->config->item('jwt_secret');
	$data  =  Array(
	"loginInfo" => Array(
			"id" => 1,
			"email" => "[email protected]",
			"tipo" => "admin"
		),
		"iat" => time(),
		"exp" => time() +  $this->config->item('jwt_expiration_time'),
		"nbf" => time() +  $this->config->item('jwt_start_time'),
	);
	$token  =  $jwt->encode($data, 'HS256');
	echo  $token;
}

Layout: O layout acima no Array segue o padrão rfc7519:

O conteúdo da mensagem loginInfo corresponde aos dados de login do usuário inseridos na mensagem do token

Exemplo de decode:

public  function  decode_token(){
	$token  =  'eyJ0eXAiOiJKV1QiLCJhbGciOiJIUzI1NiJ9.eyJpZCI6MSwiZW1haWwiOiJ1c2VyQHVzZXIuY29tIiwidGlwbyI6ImFkbWluIn0.YHoMsgq3s9Ex9S3p3ucPpZJOqMnxD3c14datJ9bVAZk'; $jwt  =  new  JWT;
	$JwtSecretKey  =  "OakleyTiffAtripFlackLowOuFlackJack";
	$decoded_token  =  $jwt->decode($token, $JwtSecretKey, 'HS256');
	echo  json_encode($decoded_token,  JSON_PRETTY_PRINT,  200);
}

Referencias:

jwt-codeigniter's People

Contributors

guilherme8787 avatar

Watchers

 avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.