'CodeIgniter 4 Submit form leads back to the same page

I am trying to get my login/registration form to submit. I suspected it was because I did not have post routes set, but even after adding these it still leads to the same page. I am able to access the pages directly, for example localhost:8080/SignUpController/store but can't seem to get the forms to submit to these pages. Here's the code:

Sign up page

<!doctype html>
<html lang="en">
<head>
    <meta charset="utf-8">
    <meta name="viewport" content="width=device-width, initial-scale=1">
    <link href="https://cdn.jsdelivr.net/npm/[email protected]/dist/css/bootstrap.min.css" rel="stylesheet">
    <title>Sign Up</title>
</head>
    <body>
        <div class="container mt-5">
            <div class="row justify-content-md-center">
                <div class="col-5">
                    <h2>Register User</h2>
                    <?php if(isset($validation)):?>
                    <div class="alert alert-warning">
                    <?= $validation->listErrors() ?>
                    </div>
                    <?php endif;?>
                    <form action="<?php echo base_url(); ?>/SignupController/store" method="post">
                        <div class="form-group mb-3">
                            <input type="text" name="name" placeholder="Name" value="<?= set_value('name') ?>" class="form-control" >
                        </div>
                        <div class="form-group mb-3">
                            <input type="email" name="email" placeholder="Email" value="<?= set_value('email') ?>" class="form-control" >
                        </div>
                        <div class="form-group mb-3">
                            <input type="password" name="password" placeholder="Password" class="form-control" >
                        </div>
                        <div class="form-group mb-3">
                            <input type="password" name="confirmpassword" placeholder="Confirm Password" class="form-control" >
                        </div>
                        <div class="d-grid">
                            <button type="submit" class="btn btn-dark">Signup</button>
                        </div>
                    </form>
                </div>
            </div>
        </div>
    </body>
</html>

Sign in page

<!doctype html>
<html lang="en">
  <head>
    <meta charset="utf-8">
    <meta name="viewport" content="width=device-width, initial-scale=1">
    <link href="https://cdn.jsdelivr.net/npm/[email protected]/dist/css/bootstrap.min.css" rel="stylesheet">
    <title>Sign In</title>
  </head>
  <body>
    <div class="container">
        <div class="row justify-content-md-center">
            <div class="col-5">
                
                <h2>Login</h2>
                
                <?php if(session()->getFlashdata('msg')):?>
                    <div class="alert alert-warning">
                       <?= session()->getFlashdata('msg') ?>
                    </div>
                <?php endif;?>
                <form action="<?php echo base_url(); ?>/SigninController/loginAuth" method="post">
                    <div class="form-group mb-3">
                        <input type="email" name="email" placeholder="Email" value="<?= set_value('email') ?>" class="form-control" >
                    </div>
                    <div class="form-group mb-3">
                        <input type="password" name="password" placeholder="Password" class="form-control" >
                    </div>
                    
                    <div class="d-grid">
                         <button type="submit" class="btn btn-success">Sign in</button>
                    </div>     
                </form>
            </div>
              
        </div>
    </div>
  </body>
</html>

Sign in Controller

<?php 
namespace App\Controllers;  
use CodeIgniter\Controller;
use App\Models\UserModel;
  
class SignInController extends Controller
{
    public function index()
    {
        helper(['form']);
        echo view('signin');
    } 
  
    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('/profile');
            
            }else{
                $session->setFlashdata('msg', 'Password is incorrect.');
                return redirect()->to('/signin');
            }
        }else{
            $session->setFlashdata('msg', 'Email does not exist.');
            return redirect()->to('/signin');
        }
    }
}

Sign Up Controller

<?php 
namespace App\Controllers;  
use CodeIgniter\Controller;
use App\Models\UserModel;
  
class SignUpController extends Controller
{
    public function index()
    {
        helper(['form']);
        $data = [];
        echo view('signup', $data);
    }
  
    public function store()
    {
        helper(['form']);
        $rules = [
            'name'          => 'required|min_length[2]|max_length[50]',
            'email'         => 'required|min_length[4]|max_length[100]|valid_email|is_unique[users.email]',
            'password'      => 'required|min_length[4]|max_length[50]',
            'confirmpassword'  => 'matches[password]'
        ];
          
        if($this->validate($rules)){
            $userModel = new UserModel();
            $data = [
                'name'     => $this->request->getVar('name'),
                'email'    => $this->request->getVar('email'),
                'password' => password_hash($this->request->getVar('password'), PASSWORD_DEFAULT)
            ];
            $userModel->save($data);
            return redirect()->to('signin');
        }else{
            $data['validation'] = $this->validator;
            echo view('signup', $data);
        }
          
    }
  
}

Routes

$routes->get('/', 'SignupController::index');
$routes->post('/', 'SignupController::store');

$routes->get('/signup', 'SignUpController::index');
$routes->post('/signup', 'SignUpController::store');

$routes->get('/signin', 'SignInController::index');
$routes->post('/signin', 'SignInController::loginAuth');

Prior to this I was having a problem where after clicking log in or sign up it would lead me to a page such as http://localhost/Ci4/index.php/signin which would give me a 404 page not found. Changing the baseURL back to the default http://localhost:8080/ (which is what I'd prefer to use) did not seem to solve that issue so after some research it seems that moving the index.php and .htaccess files from the public folder to the project's root has solved that issue. Now I'm at the present issue where the login and sign up buttons don't seem to do anything. I'm not sure if this is relevant to the issue but thought I might add this info.

Any help with this or pointing me in the right direction would be greatly appreciated. I am also using XAMPP as my web server.



Solution 1:[1]

I ended up restarting this and got things to work. I can't recall entirely what I did, however, I believe Tsefo's answer was the primary issue.

As for the issue of the page leading to the "http://localhost/Ci4/index.php/signin" URL is because I had an Auth filter set up to deny access to certain pages if a user was not logged in. I don't know why that didn't occur to me at the time...

Auth.php

<?php 

namespace App\Filters;
use CodeIgniter\HTTP\RequestInterface;
use CodeIgniter\HTTP\ResponseInterface;
use CodeIgniter\Filters\FilterInterface;

class Auth implements FilterInterface
{
    public function before(RequestInterface $request, $arguments = null)
    {
        // if user not logged in
        if(! session()->get('logged_in')) {
        // then redirct to login page
        return redirect()->to('/login'); 
        }
    }
    //--------------------------------------------------------------------
    public function after(RequestInterface $request, ResponseInterface $response, $arguments = null)
    {
    // Do something here
    }
}

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 user18857467