I am having a similar problem – user can log in successfully, and page refreshes to last viewed page (using your code) but the page doesnt reflect that the user is logged in. If you refresh the page it shows as logged in.
Not getting any JavaScript errors.
Please let me know what email address to send site and login details too.