Not sure is this is an option for you but my current solution works well
for this. I love it because i used to have this same problem you are having
constantly and it was frustrating. And takes a lot of logic to solve it.
Can you bootstrap the user into the page before the app ever evens loads.
Meaning can the server inject the current user logged in or not logged in
into the html and on the window object? Then no rest calls or async
problems exist. You could still make a http call for more info on the user,
but you know for sure if there is a session for that user.
In my main app module i have `$rootScope.user = window.user;` in the `run`
method.
And something like below in the head of my SPA html. The syntax of `{! user
!}` is jinja templating for Python. But all server side frameworks have
something like it.
<script type="text/javascript">
window.user = {! user !};
</script>
Is this bootstrapping possible for you?
Lastly i have also used localStorage for this before, I don't like it as
much but closing a tab doesn't remove localStorage so you could read the
last state of the user to know if they were logged in or not. This was my
old solution, not as pretty as the server bootstrap one.
On Friday, March 28, 2014 2:16:35 PM UTC-6, Alexandros Karypidis wrote:
>
> Hi,
>
> I find myself in this predicament:
>
> The short version of the question is "is it possible to pause execution
> until an XHR completes" ?
>
> My app has a service that tracks the user status. When it loads, the
> service issues a $http call to the server, to fetch the status (logged in
> if a session exists, not logged in otherwise. Something like (simplified)
> this:
>
> var appstateSvc = {
> authStatus: null,
>
> refreshAuthStatus: function() {
> //...
> $http.post(url, data).success(function(data, status) {
> //...
> self.authStatus = data.authStatus
> })
> },
>
> I also have a locationChangeSuccess handler registered which verifies the
> user is logged in when arriving at a location. It inject the above service
> and in cases where the target location would be "log-in" only I redirect to
> a login view:
>
> else if (!nextRoute.access.isPublic) {
> // redirect to login form
> if (this.authStatus != 'seemsLoggedIn')
> this.interceptForLogin(nextRoutePath);
>
>
> This works fine for the most part, but has the following problem:
>
> 1. Assume I am logged in looking at my SPA
> 2. I close the browser tab
> 3. I open a new tab before session times out (still logged in)
> 4. I select a bookmark to some location that requires log in
>
> What happens at this point is that the $http call has not returned yet and
> the status is "loading". However, my location change handler gets called
> and redirects to the login page. How can I prevent this from happening?
>
> else if (!nextRoute.access.isPublic) {
> // redirect to login form
> if (this.authStatus === 'loading') {
> // HERE what can I do?
> // I know XHR is in progres and need to wait for
> // respones...
> } else if (this.authStatus != 'seemsLoggedIn')
> ...
>
> Thank you!
>
>
--
You received this message because you are subscribed to the Google Groups
"AngularJS" group.
To unsubscribe from this group and stop receiving emails from it, send an email
to [email protected].
To post to this group, send email to [email protected].
Visit this group at http://groups.google.com/group/angular.
For more options, visit https://groups.google.com/d/optout.