Skip to content

Login succeeds via AJAX but does not redirect to dashboard due to base URL mismatch and lost session state #23

Description

@duckpigdog

GitHub Issue: Login Does Not Redirect After Successful Authentication in Local Deployment

Title

Login succeeds via AJAX but does not redirect to dashboard due to base URL mismatch and lost session state

Summary

When the project is deployed locally at http://127.0.0.1:3000/, the login form may either:

  1. show An error occoured!, or
  2. stop showing the popup but still remain on the login page after valid credentials are submitted.

This issue was caused by two separate problems in the login flow:

  • the frontend AJAX request used a hard-coded base_url that did not match the active local deployment URL
  • after successful authentication, the next request could lose the expected login session and regenerate an empty session

Environment

  • Project: School-Student-Management-System
  • Stack: PHP + CodeIgniter 3 + MySQL + jQuery
  • Local URL used during debugging: http://127.0.0.1:3000/

Symptoms

Case 1: AJAX error popup

The login page showed:

An error occoured!

This came from the frontend error callback in:

  • assets/js/neon-login.js

Case 2: No popup, but no redirect

After fixing the request URL, the login API could return success, but the browser still stayed on the login page instead of reaching the correct dashboard.

Expected Behavior

  • valid credentials should redirect the user to the correct role dashboard
  • invalid credentials should stay on the login page and show the normal invalid-login UI

Actual Behavior

  • login AJAX initially requested the wrong endpoint because base_url did not match the running host
  • after that was fixed, authentication could succeed but the next page request still behaved as unauthenticated in the local environment

Root Cause

1. Hard-coded base URL mismatch

The project configuration used:

$config['base_url'] = 'http://localhost/School-Student-Management-System/';

While the app was actually being accessed at:

http://127.0.0.1:3000/

Because the login page generates:

var baseurl = '<?php echo base_url(); ?>';

the frontend AJAX request was sent to the wrong URL.

2. Session state was not preserved reliably after AJAX login

Even when the login endpoint returned login_status=success, the following request could regenerate a new empty ci_session, which caused the application to render the login page again instead of the authenticated dashboard.

Changes Made

Change 1: Fix local deployment base URL

Updated:

  • application/config/config.php

From:

$config['base_url'] = 'http://localhost/School-Student-Management-System/';

To:

$config['base_url'] = 'http://127.0.0.1:3000/';

This ensures the login page submits AJAX requests to the actual running host.

Change 2: Stabilize post-login authentication state

Implemented a fallback authentication restore mechanism for local session instability:

  • added application/core/MY_Controller.php
  • updated Login.php to extend MY_Controller
  • updated protected controllers to extend MY_Controller
    • Admin.php
    • Teacher.php
    • Student.php
    • Parents.php
    • Modal.php
  • issued a signed auth cookie after successful login
  • restored role session from that signed cookie before access checks
  • cleared the auth cookie on logout
  • changed successful login responses to return a role-specific dashboard URL directly

Files Changed

  • application/config/config.php
  • application/core/MY_Controller.php
  • application/controllers/Login.php
  • application/controllers/Admin.php
  • application/controllers/Teacher.php
  • application/controllers/Student.php
  • application/controllers/Parents.php
  • application/controllers/Modal.php

Verification

Verified that:

  • the login API returns success for valid credentials
  • the response now includes a role-specific redirect URL
  • authenticated requests can reach the correct dashboard page

Example verified redirect response:

{
  "submitted_data": {
    "password": "123",
    "email": "riham@gmail.com"
  },
  "login_status": "success",
  "redirect_url": "http://127.0.0.1:3000/index.php?student/dashboard"
}

Notes

  • The signed auth cookie fallback was introduced as a practical source-level fix for the local debugging environment where the expected session continuity was not stable across requests.
  • If the deployment environment is later normalized, the session implementation can be simplified again.

Suggested Labels

  • bug
  • login
  • session
  • codeigniter
  • local-dev

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions