I'm a newbie with ReactJS and Cakephp. I've managed to write some api to retrive/filter posts from my DB, but now I'm struggling with the Authentication part.
Actually I've done the registration pretty easly, now I'm trying to make a login form (with React). I'm able to send the username and password data to my backend, but $this->Auth->identify() return false.
This is what I've already checked/done:
DB password column length 255 varchar
DB user password correctly hashed after registration
Pass User entity with user data to identify() method as parameter
$this->request->input('json_decode', true); return correctly username and password (password not hashed)
So I've some doubts:
- I'm passing the user data as JSON through fetch(), but when it reach backend is plain text so Do I need to json_encode that data before identify() can process it? or Does identify() hash my password when it tries to find user/pass inside my DB?
ReactJS
handleSubmit(e) {
e.preventDefault();
let myHeaders = new Headers();
// state structure
// this.state={username:'userName', password: '123asdqwe'};
let myInit = {
method: 'POST',
headers: myHeaders,
mode: 'cors',
body: JSON.stringify(this.state)
};
fetch('url/users/login', myInit)
.then(() => {console.log("done");});
}
CakePHP Login function (base)
public function login()
{
if ($this->request->is('post')) {
$user = $this->Auth->identify();
if ($user) {
$this->Auth->setUser($user);
$this->response->body(json_encode(array("user_id" => $this->Auth->user('id'))));
return $this->response;
} else {
$this->response->body(json_encode(array("error" => "error")));
return $this->response;
}
}
}
Actually I don't know how to proceed, I can't find any examples on the web and the doc isnt giving me any clue.
How should I have to process data before login?
Thank you.