Note that there are some explanatory texts on larger screens.

plurals
  1. POInitialize AngularJS config block with asynchronous data
    primarykey
    data
    text
    <p>While defining the routes of an AngularJS application, in order to indicate what access level each route should have, a new property called <code>access</code> has been added to each route. This is based on a Client-Side authorization approach described <a href="http://www.frederiknakstad.com/authentication-in-single-page-applications-with-angular-js/" rel="nofollow noreferrer">here</a>.</p> <p>The <code>.config</code> block would look like the following:</p> <pre><code>app.config(['$routeProvider', '$locationProvider', '$httpProvider', function ($routeProvider, $locationProvider, $httpProvider) { var access = routingConfig.accessLevels; //Initialize with asynch data instead $routeProvider. when('/home', { templateUrl: '/Templates/home.html', controller: 'homeController', access: access.user }); $routeProvider. when('/private', { templateUrl: '/Templates/private.html', controller: 'adminController', access: access.admin }); //...more route configurations }]); </code></pre> <p>Leaving the core logic of the approach aside, I just want to know in general how can we initialize the <code>access</code> variable with some asynchronous data so that it is available while defining the <code>access</code> property for each route? This is needed because only the server has knowledge of the list of access levels.</p> <p>Although there are answers to <a href="https://stackoverflow.com/questions/16286605/initialize-angularjs-service-with-asynchronous-data">Initialize AngularJS service with asynchronous data</a> based on exposing a <code>promise</code> in the service &amp; using <code>resolve</code> in route config, it won't work in my case as I need the asynchronous call to be resolved before defining the route configuration because the <code>access</code> property depends on the asynchronous data.</p> <p>What options do I have? Can the <a href="http://docs.angularjs.org/api/ng.$q" rel="nofollow noreferrer">promise/deferred</a> approach still be used somehow?</p> <p>Any suggestion would be much appreciated.</p> <p><strong>EDIT :</strong></p> <p>To describe what <code>access</code> is doing a bit, it is being populated by a separate module called <code>routingConfig</code>. In the <a href="http://www.frederiknakstad.com/authentication-in-single-page-applications-with-angular-js/" rel="nofollow noreferrer">approach</a>, a user can belong to a certain role which is defined as a binary number. For e.g public : 001, user : 010, admin : 100 (and so forth).</p> <p>The access level for any route will again be a binary number defined by the OR operation of all user-roles that are allowed to access it. So for e.g. if the <code>user</code> access-level can be accessed by user-roles <code>user</code>(010) and <code>admin</code>(100), its bitmask would be 110. Later to check access on a route, we can do a AND operation on the user-role and access-level to see if it is authorized or not. Details can be seen in the above link.</p> <p>Note that data at the server end is still secure by more complex algorithms, so even if the above logic is manipulated with at the client-side, such a user would only be able to see the markup of any unauthorized page.</p> <p>Again, coming back to the problem in hand. How can <code>access</code> which is being used as a property in the <code>config</code> block be initialized with asynchronous data that comes from the server. This data may look like the following:</p> <pre><code>accessLevels : { 'public' : '111', 'user' : '110', 'admin': '100' } </code></pre>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload