#7 Codeigniter 3 – Rest Server Library API – Generate PHP Token for User Authorization


Go to PHP-JWT Library A simple library to encode and decode JSON Web Tokens (JWT) in PHP This is encoded and decode methods Download this repository Paste the repository into the third_party folder of the project Extract zip file This is custom library for user authorization In which the PHP-JWT library is include This function generate user token And this function validate user token There is also a function that will later do it By this name we have to create a library in our project And have to paste the complete library Load Authorization Library We need to create a config file (jwt.php) To create JWT Secure Key and JWT Algorithm Type Generate a user token Pass user data in generate function Store user’s data in token variable user token successfully created Using this function, you can decode user data from token Decode user token User token not received We have to send a token to the header (Authorization) Add Authorization header We received the user’s data from the decode of the token We also need to add token time to token data We can also set the token expiry time And also can set the header

Tags: , , , , , , , , , , , , , , , , , , ,

20 thoughts on “#7 Codeigniter 3 – Rest Server Library API – Generate PHP Token for User Authorization”

  1. Clint Pudadera says:

    Hello sir thanks for the source 😀

  2. Durmazlar BT says:

    How can I solve this?
    { "status": false, "error": "Unknown method" }
    In my result "Unknown method", Rest_Controller : Line 702

  3. alaa hamoudah says:

    Thank you for the video, can u please help to fix this problem,
    [status] =>
    [message] => Token is not defined.
    this happened whey I try to get the access token, print_r($this->authorization_token->userData()); exit;

  4. haseeb fayyaz says:

    when we use jwt then our login/signup system will be unsecure ??

  5. Channeth Khon says:

    Nice sharing, on local is okay but on testing server cannot define token:
    "status": false,
    "message": "Token is not defined."

  6. C Tech हिन्दी says:

    It is important to understand that the purpose of using JWT is NOT to hide or obscure data in any way. The reason why JWT are used is to prove that the sent data was actually created by an authentic source.

    Since JWT are signed and encoded only, and since JWT are not encrypted, JWT do not guarantee any security for sensitive data.

  7. Juani Zz says:

    Hello, do not use Insomnia. I want to do it with a form in a view.

    { "status": false, "message": "Token is not defined." }

    How can I validate the token?

    My code:

    The library loads in constructor of controller users

    public function login_post(){
    header("Access-Control-Allow-Origin: *");

    $data = $this->security->xss_clean($_POST);

    $this->form_validation->set_rules('username', 'username', 'trim|required');
    $this->form_validation->set_rules('password', 'password', 'trim|required');

    if($this->form_validation->run() == FALSE){
    echo "error xd";

    $result = $this->User_model->user_login($this->input->post('username'), $this->input->post('password'));

    if(!empty($result) AND $result != FALSE){
    $token_data = array('user_id' => $result->id,
    'full_name' => $result->full_name,
    'email' => $result->email,
    'username' => $result->username
    // ,'time' => time()

    $user_token = $this->authorization_token->generateToken($token_data);

    echo "Logeado invalido";



    I must modify something in Authorization_Token.php?

    Sorry my english, i speak spanish.

    Thanks for helping me.

  8. shravankumar patil says:

    How to logout using jwt token in codeigniter ?? Please help me sir

  9. Aakash Kothule says:

    How can i get Athorization_Token.php that file?

  10. David Tacer says:

    Hi, thank you for your videos. Great job!
    Ok. I'm keep getting [message] => Token is not defined. I'm using Insomania, I set Header Authorization and put token as value, basically did everything what you did in the video and no success. Header not sending token. Any suggestions? Thank you.

  11. alaa hamoudah says:

    Does this library support silent login?

  12. Saifurrahim Widya says:

    Is it possible to add privilege for users? Like give them role, so user can only access GET method, and other user can access both GET and POST.

    I try to figure out using this PHP-JWT, but still had no clue.

    Nice video and very helpful project btw

  13. Alan Toro says:

    Quick question. Im using psotman for testing the method. Installed JWT all good, but when i try to login y can't get trough the first form validation, maybe im sending the request wrong. im sending it on params, and also tryed on headers. I se you have a 1 on headers, wich paramis it? maybe thats it. The response says that the username and password fields are required, wich suggests the username and passwords are not getting there

  14. naqibullah danishjo says:

    Really helpful articles. What if I had multiple role users? for example admin and customer. I would be glad if you respond me.

  15. naqibullah danishjo says:

    Thank you for this great tutorial. How to create a forget password page using email?

  16. Santosh Parmar says:

    Hi i have installed it in my old live running project and it is working but i have one confusion. Is it possible to use it with ION_Auth code-igniter library.. is yes then pls help me to validate ION_Auth functions like $this->ion_auth->logged_in(), $this->ion_auth->is_admin()… i know this is not part of you pls.

  17. Muhammad Faisal Budiman says:

    where can i get jwt.php in controller ?

  18. Sports Lovers, says:

    what is $output->id; in which $output?

  19. Adriel Bustos says:

    Hola, estoy generando el token sin problema, pero no me funciona el userData(), del token que genere, lo estoy pasand como authorizacion en las cebercas, pero siempre me dice "Forbidden", alguna solución? gracias

  20. manisha pardhe says:

    thank you very much …………………perfect working video

Leave a Reply

Your email address will not be published. Required fields are marked *