0

I have an app that is in house using Web API 2 Active Directory for authentication and angularjs on the front end. When a user navigates to the index page I want to route them to /home/users/jdoe (where jdoe is their AD id). My angularjs route is setup so that user and jdoe are parameters:

.when("/events/:type/:value", {

I do this because type can be someone else like category and value will be some category.

Now to start out with the client doesn't know who this user is of course. So I need to make an $http call to get that from the web api 2 and return it. Then somehow use that value in the redirect on "/" route. Since this is the default route and it seems like you can't inject services into config() how would I be able to set something like this up?

I need a way to setup the redirectTo: 'home/users/' + currentUser where currentUser was received from an $http call at the start and filled in before the config() function so the route can use it.

georgeawg
  • 48,608
  • 13
  • 72
  • 95
user441521
  • 6,942
  • 23
  • 88
  • 160
  • The `$http` service can't be injected into a `.config` function, but it can be injected into a resolve function of a route. For more information, see [AngularJS ngRoute $routeProvider API Reference - when](https://docs.angularjs.org/api/ngRoute/provider/$routeProvider#when) – georgeawg May 02 '19 at 21:36
  • Right, but you have to get to the route first for that routes resolve to be called. That's the main issue. When they go to "/" I want to redirect to the "/home/users/" + curentUser. How would I get that in the redirectTo statement in the "/" route? – user441521 May 02 '19 at 23:23
  • Use the [$location](https://docs.angularjs.org/api/ng/service/$location) service to change routes from AngularJS JavaScript. – georgeawg May 02 '19 at 23:31
  • Oh, I see. So make a controller for my "/" route and have a resolve on it to get the current user. Then inside that controller just use $location to change the route to the final route (I assume I can't use $location inside the resolve function but maybe I can?). I wonder if this will produce a flickering as it navigates to the "/" route then quickly reroutes. I'll give it a try though, thanks. – user441521 May 02 '19 at 23:36
  • After digging I'm seeing a setting resolveRedirectTo() that seems interesting and could possibly do this from within the routing in config. – user441521 May 02 '19 at 23:58

0 Answers0