0

I'm writing a single-page application in AngularJS that needs to know, whether user is logged-in before running any routes at all.

I'm using Django backend, which supports anonymous sessions, so just looking at cookies I can't figure, if user is logged-in or anonymous. So, to find that out I want to make an Ajax request to backend in Angular application-global .run() and delay any execution of angular application until that request finishes.

How do I do that?

11
  • 1
    I would suggest you use github.com/angular-ui/ui-router/wiki#resolve in ui-router or an http interceptor docs.angularjs.org/api/ng/service/$http. Commented Mar 28, 2016 at 9:47
  • For your need, you should use ui-router resolve. But its better to check for the server response with an http interceptor and redirecting the user if authentication fails. Commented Mar 28, 2016 at 9:49
  • @AbdulMannan I considered using ui-router's resolve as a last resort solution. Unfortunately, with this approach I'd have to manually call the same resolve code at every route, which is repetitive and error-prone. Checking out $http interceptors. Thanks for suggestion. Commented Mar 28, 2016 at 9:50
  • 1
    In that case, you should use interceptors and in case of failed authentication, return a 403 error from server and look for the 403 error resonse in your angular interceptor and redirect. Commented Mar 28, 2016 at 9:53
  • @AbdulMannan Probably, you are right. Reading the docs currently. Just one question: will interceptors delay rendering of the first page until authentication promise is resolved? Cause I want to avoid the scenario when an anonymous page starts rendering and then in the middle of that a promise resolves, so user gets a Frankenstein of anonymous and logged-in pages. Commented Mar 28, 2016 at 9:58

1 Answer 1

0

Create a directive that you can put on each view. Our project has a base view using razor so it makes it easier. Let the directive wait for the promise and only make the content visible when it is resolved.

Sign up to request clarification or add additional context in comments.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.