Brenda Olivia Martis
Brenda Olivia Martis

Reputation: 563

How to use JWT with cakephp 2.10?

I'm using CakePHP 2.10.9 version and trying to use token-based authentication instead of session-based authentication.

I couldn't find any information on how to use JWT with CakePHP 2.x.

Here is what I have tried so far. As a first step, I downloaded the plugin t73biz/cakephp2-jwt-auth and added this into the folder app/Plugin/JwtAuth. As mentioned in the usage(https://github.com/t73biz/cakephp2-jwt-auth), I added below configuration in the app/Controller/AppController.php

var $components = array(
        'Auth' => array(
            'authenticate' => array(
                'JwtAuth.JwtToken' => array(
                    'fields' => array(
                        'username' => 'username',
                        'password' => 'password',
                        'token' => 'public_key',
                    ),
                    'parameter' => '_token',
                    'userModel' => 'User',
                    'scope' => array('User.active' => 1),
                    'pepper' => 'sneezing',
                ),
            ),
        ),'Session','RequestHandler','Email','Flash');

From my past experience with Plugin's, I knew I'm supposed to include the plugin in bootstrap.php.

CakePlugin::load('JwtAuth');

I don't know what am I supposed to do after this. Could someone guide me?

Upvotes: 1

Views: 1603

Answers (1)

tarikul05
tarikul05

Reputation: 1913

I think you didn't read the the Authentication part

=> The query string parameter defined as parameter in the config array (defaults to _token)

=>The contents of the header defined as header in the config array (defaults to X_JSON_WEB_TOKEN)

It means when you access any method then you have to pass the JWT toekn with query params or Header request.

Sample Query params: http://example.com/users/add?_token=THEJWTWEVTOKEN

Upvotes: 1

Related Questions