1

I am using CI4 as backend and react Js as front end and I am doing authentication so after the authentication I want to push the user to other pages if the credentials are correct if not then go just refresh the same page

I understood how to change the pages but now the problem is that this code gives the status 200 for everything even if the credentials are not correct so what should I do to make the status change from true to false as I am using if the response is true push to another page

public function loginAuth()
    {
        $session = session();
        $userModel = new UserModel();
        $email = $this->request->getVar('email');
        $password = $this->request->getVar('password');
        
        $data = $userModel->where('email', $email)->first();
        
        if($data){
            $pass = $data['password'];
            $authenticatePassword = password_verify($password, $pass);
            if($authenticatePassword){
                $ses_data = [
                    'id' => $data['id'],
                    'name' => $data['name'],
                    'email' => $data['email'],
                    'isLoggedIn' => TRUE
                ];
                $session->set($ses_data);
                return redirect()->to('/');
            
            }else{
                $session->setFlashdata('msg', 'Password is incorrect.');
                // return redirect()->to('/signin');
            }
        }else{
            $session->setFlashdata('msg', 'Email does not exist.');
            // return redirect()->to('/signin');
        }
    }

this is the code and now I want that after the success it should push to a different page but the problem is that it gives true to all the conditions and I can not do anything if all of the conditions get statusCode=200

  • You need to use `HTTP status codes` Based on these status codes you can then redirect the user from your react app to other components. You can read more about HTTP status codes at: https://developer.mozilla.org/en-US/docs/Web/HTTP/Status – Hassaan Ali Mar 12 '22 at 08:14
  • yes but how to push the user to a different page of reactJS from CodeIgniter? – Ghulam Mustafa Mar 12 '22 at 08:15
  • `react-router-dom` is the thing you need. In the latest version, you are gonna use ``````. Just try to search [redirect with react-router-dom](https://stackoverflow.com/questions/69868956/how-to-redirect-in-react-router-v6). – Mukhriddin Shakhriyorov Mar 12 '22 at 08:20
  • I understood now but now how to change the status code of the wrong credentials? – Ghulam Mustafa Mar 12 '22 at 08:21

1 Answers1

1

I have found a way to do this first just use react-router to push to another page if your validation function is true then change the status for the other conditions to

        $model = new UserModel();
        $user  = $model->save($this->request->getPost());

        // Respond with 201 status code
        return $$this->fail($errors, 400);

and I think it will work for you