angular-ui / ui-router

The de-facto solution to flexible routing with nested views in AngularJS
http://ui-router.github.io/
MIT License
13.53k stars 3k forks source link

Initialization data before $state starts working #357

Closed colthreepv closed 11 years ago

colthreepv commented 11 years ago

I'm having a quite-common issue, judging by the issue tracker of this project.

A company website needs to validate an user before displaying a page/resource, this is selectively done with $state.resolve, but i need a global version of this.

Global --> because it's the login information.

Quick sumup of the app:

That solution to be robust needs to work on all routes, because i don't know where the user tries to access on his first request. At the state of developing we have already ~40 states, it's not really a clever idea put 40 resolve(s).

Possible Ideas

I was looking carefully to the $stateChangeStart event, it can be prevented, generating a rejected promise. What i would really want is to delay the resolving of this promise (using .then!), not reject it completely.

This resolve would be run only on the first $stateChange request of the AngularJS Application, since after this we'll have the informations we need.

I'll update this post if i get any more valid ideas :smile:

colthreepv commented 11 years ago

another possible idea

Create a father, abstract, state with a resolve with the required info. All the other states have to be child of this State, it should imply no problems whatsoever, just a some find/replace in the existing code!

Will try that soon© :soon:

UPDATE: i've elaborated this idea as a project, mrgamer/angular-login-example, works :smile: It will require quite a few documentation because it's more a logic project than code one.

laurelnaiad commented 11 years ago

I'd recommmend https://github.com/witoldsz/angular-http-auth and/or https://github.com/fnakstad/angular-client-side-auth

colthreepv commented 11 years ago

Those projects are really cool, but they are not what i'm looking for: they are for resource-based authentication.

I need something that is state-based, i'll explain a little better, hoping to be concise:

My states that requires server-data already haves resolve(s) and they have a redirect system similar to angular-client-side-auth, using $stateChangeError. But some states doesn't require "resources", for example a website that i would define robust wouldn't let an already logged-in user to access the registration page. That can happen with an AngularJS application, the flow goes this way:

The quick'n dirty solution would be change the location when the promise gets completed, but.. i'm looking for a more robust solution.

laurelnaiad commented 11 years ago

If your resources are protected by serving 401/403 errors when not authenticated/authorized then what's left is tailoring your UI (including the selection of appropriate states and login screen triggers, which is what those projects do). You could of course do it entirely differently using state resolve features, but ultimately security comes from the server.

timkindberg commented 11 years ago

Your idea of one grandfather state is what I would do. It's perfectly suited for your scenario.

colthreepv commented 11 years ago

The grandfather idea actually works, example here: mrgamer/angular-login-example. Closing this :+1: hope to be useful to someone having my same headache.