1

I'm developing an API for an application that I'm creating. This application will get all the information from this API (first will auth the user, then get information)

Right now, I'm trying to make the user send a username and password to the API, it validates the information and returns if it's "ok" or "not", very simple to start only. I know all the security involved in this, just need to get this working.

Already managed to send the username and passsword on the API Side (and i'm 100% sure that the data is correctly saved). Though, when I call

$this->Auth->identify($this->request->data);

It always returns false to me (already tried with parameters and without, result is the same).

I'm using the HttpRequester, firefox plugin, to send information.

I've did a debug of $this->request->data to see if the information is correct, and it is. Can't do a find on database since the password is being hashed.

The database password field is a varchar with 300 length (already tried with 255, also no work)

Thanks for the help

EDIT:

      $this->loadComponent('Auth', [
        'authenticate' => [
            'Form' => [
                'fields' => [
                    'username' => 'email',
                    'password' => 'password'
                ]
            ],
        ]
    ]);

     if($this->request->is('POST')){
        //debug($this->request->data);
        $user = $this->Auth->identify($this->request->data);
    }

Users Table:

protected $_accessible = [
    '*' => true,
];

/**
* Hash password
*
*/
protected function _setPassword($password)
{
    return (new DefaultPasswordHasher)->hash($password);
}

protected function _getFullName()
{
    if(isset($this->_properties['full_name'])){
        return ucwords(mb_strtolower($this->_properties['full_name']));
    }
}

ps: Also tried doing the following (replacing the variables form post, but also no luck)

$this->request->data['username'] = "xxxx";
$this->request->data['password'] = "zzzz";
2
  • In the request data you must use the custom field names too, ie email instead of username. The username key is just the option name by which the authenticator can access your configured field/column name. Commented Feb 25, 2017 at 16:12
  • Yep, that was the problem :) Thanks mate! Commented Feb 27, 2017 at 8:00

1 Answer 1

3

Problem is here

    'Form' => [
                'fields' => [
                    'username' => 'email', //email is your database field
                    'password' => 'password' // password is your database field name
                ]
            ],

Your code should be

      'Form' => [
                'fields' => [
                    'username' => 'username',
                    'password' => 'password'
                ]
            ],

Details check Configuring Authentication Handlers

Sign up to request clarification or add additional context in comments.

3 Comments

Hi there @tarikul05. Thanks for the answer! Though, on my database, the two fields I use is email and password
then $this->request->data['username'] should be $this->request->data['email']
That was it @tarikul05. Thanks for the help :)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.