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

#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

Only registered users can comment.

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

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

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

  4. 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.

  5. 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";
    }else{

    $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);
    print_r($user_token);
    print_r($this->authorization_token->validateToken());

    print_r($this->authorization_token->userData());
    $this->otra_view();
    }
    else{
    echo "Logeado invalido";
    }

    }

    }

    I must modify something in Authorization_Token.php?

    Sorry my english, i speak spanish.

    Thanks for helping me.

  6. 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.

  7. 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

  8. 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

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

  10. 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 JWT..so pls.

  11. 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

Leave a Reply

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