1

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.

Emmanuele
  • 71
  • 1
  • 9
  • 1
    I'm not overly familiar with react, does it send an `Accept` header with `application/json` out of the box? Also have you setup CakePHP to be able to handle JSON requests (ie loaded the body parser middleware or the request handler component)? – ndm Oct 30 '19 at 13:22
  • Hi @ndm, thank you for the help. I've modified my header like you said ( 'Accept': 'application/json','Content-Type': 'application/json'), request handler already enabled, and now Auth->identify() return the correct user. But inside the network tab now I see that now it calls users/login twice: first time return an empty response, the second one return the json with my user data not hidden (the second one was the expected behaviour). – Emmanuele Oct 30 '19 at 14:24
  • 1
    Your `mode` option is set to `cors`, so I guess the first request is an [**`OPTIONS` request**](https://stackoverflow.com/questions/29954037/why-is-an-options-request-sent-and-can-i-disable-it). – ndm Oct 30 '19 at 14:34
  • Thank you for the help! I'm going to read a lot of stuff about that. – Emmanuele Oct 30 '19 at 16:31

0 Answers0