0

Ive created a backbone application that sends form data to the codeigniter backend for verification using a MySQL database. Below is the authenticate event of the backbone view that will be called on submission of the form,

authenticate: function(event) {
      var usernameValue = $('#username').val();
      var passwordValue = $('#password').val();
      var c = new MyQuiz();
      var userDetails = {
        'username': usernameValue,
        'password': passwordValue
      }
      c.save(userDetails, {
        type: 'POST',
        url: '/CW2/ASSWDCW2/cw2app/index.php/User/loginUser',
        success: function(model, response) {
          alert('Form data submitted successfully :' + response);
        },
        error: function(model, response) {
          alert('Error saving model');
        }
      }).done(function(model, response) {
        console.log('Done saving model');
      });
    }

I'm getting this data to the User controller and after successfully authenticating the data, I want to redirect to Home controller where the home_view will be called in the index function.

    public function loginUser()
{
    if ($this->input->server('REQUEST_METHOD') == 'POST') {
        $data = $this->decode_data($this->input->raw_input_stream);
        $username = $data['username'];
        $password = $data['password'];
        $this->load->model('UserModel');
        if ($this->UserModel->authenticate($username, $password)) {
            $this->session->is_logged_in = true;
            $this->session->username = $username;

            //sucessfully athenticated
            redirect('/Home/');
        } else {
            $this->session->login_error = True;
            redirect('/User/');
        }
    }
}

Problem : It wont redirect to Home controller once authenticated successfully.Please help!

Kavishka Rajapakshe
  • 537
  • 1
  • 8
  • 23
  • who's using backbone.js anyway? :D – technophyle Dec 30 '22 at 02:11
  • on a serious note, what happens when authenticated successfully? – technophyle Dec 30 '22 at 02:13
  • when authenticated it should redirect to the home controller which will load the home view – Kavishka Rajapakshe Dec 30 '22 at 02:18
  • my question is what happens currently? you just said "It wont redirect". what exactly happens? – technophyle Dec 30 '22 at 02:27
  • it reloads the login view(the view that's passing the data to codeigniter). no errors in console. – Kavishka Rajapakshe Dec 30 '22 at 02:33
  • three things: 1) check if the code `redirect('/Home/')` is called correctly when auth succeeds, 2) check if the route `/Home/` is correct, and 3) check if there's any redirection from `/Home/` route to login route if there's no auth info available. #3 could be the most probable culprit. there's nothing in your code that stores a user's token in cookie or local storage. – technophyle Dec 30 '22 at 02:40
  • 1). Authentication happens successfully but a redirect isn't called. 2) the route is correct as the redirect (/Home/) used to work before I implemented backbone.js to the client side.3)There are no redirections from Home controller – Kavishka Rajapakshe Dec 30 '22 at 02:57
  • See if any of these answers helps, https://stackoverflow.com/questions/723883/redirect-with-codeigniter – MZM Dec 30 '22 at 03:30

0 Answers0