0

I have a functioning site that includes a search function that loads results into a <div> element via a jQuery $.ajax call like so:

$.ajax({
    url:        '{{ path('order_search') }}',
    type:       $(this).attr('method'),
    data:       $('#search_form').serialize(),
    success:    function (data) {
        $('#js-search-results').html(data);
    }
});

I'm working in a twig template within a symfony project, thus the url notation. This is working perfectly.

This is on a site that requires login. I have an event listener in symfony that checks for a period of inactivity at each each kernel request (taken from here), and if the inactive period exceeds a maxIdleTime then the user is redirected to the login page.

My problem is that if the user is inactive for a period and then enters a search, the js-search-results div is filled with the login page. What I would like to happen is to have the entire window redirect to the login page. Seems simple, but I haven't figured out how to do it.

One what I thought to handle would for my login page script to check whether it was being loaded into the full window (rather than just a div element), and if not then refresh the entire window. How to do this?

Community
  • 1
  • 1
ehymel
  • 1,360
  • 2
  • 15
  • 24
  • the back end code at `'{{ path('order_search') }}'` should return an error if the user isn't logged in, then the `$.ajax` should have an error handler to redirect the page to `login` in the case of an error - alternatively, the success function should check to see what `data` is, rather than blindly assuming it's a valid search response – Jaromanda X May 13 '17 at 02:29

2 Answers2

1

It sounds like you want to find out what type of data is there and if it is the login page do a redirect.

Find out if the data contains elements that are in the login page. if it is do a window.location.href = "/login" if not display the search results in the div. send data from backend like {page : "login"}

jack blank
  • 5,073
  • 7
  • 41
  • 73
1

You can check the content inside the variable "data" to check if it contains elements from the login page and then use an if else statement.

if(data.includes("username") && data.includes("password")){
  window.location.assign("https://your.login.page.html");
}else{
  $('#js-search-results').html(data);
}
Max08
  • 955
  • 1
  • 7
  • 16