marmelab / ng-admin

Add an AngularJS admin GUI to any RESTful API
http://ng-admin-book.marmelab.com/
MIT License
3.94k stars 725 forks source link

Dynamic entities/menu/dashboard #1275

Open pvanderlinden opened 7 years ago

pvanderlinden commented 7 years ago

It seems impossible to configure ng-admin after the page has loaded.

I'm trying to do the following workflow:

I tried the other tickets:

Which all seems to result in the same errors:

It might have to do with me missing some knowledge as I'm new to angular (I hoped I didn't need to read all the in depth documentation of Angular before being able to use ng-admin). Is the above scenario possible?

pvanderlinden commented 7 years ago

If I follow: https://github.com/marmelab/ng-admin/issues/1026 It won't add the menu as the menuList is empty when the for loop happens, or when you put the for loop in the callback, they won't get displayed until you click on home.

pvanderlinden commented 7 years ago

I got it working in the end with angular-deferred-bootstrap and window.location.reload (to re-bootstrap the application). It might be good to have a full example in the demo application for less experienced javascript developers?

Phocea commented 7 years ago

Hello, part of the solution was described in https://github.com/marmelab/ng-admin/issues/1029#issuecomment-209285741. However this is more an angular issue than ng-admin itself. Using $http injection in the config section does not solve the problem correctly because the asynchronous call is resolved too late to be usable.

Please do post your code snippet in this issue .We should compile a list of recipes in the FAQ, this one being one coming back quiet often.

I have found that the best and more secure way to solve this however is to move the login page in its own application:

pvanderlinden commented 7 years ago

Here is my code, this uses a slightly modified version of https://github.com/akagadovskiy/ng-admin-jwt-auth but I think it is still clear how it works. I can also post the code for building the menu from JSON is that is interresting for the docs @Phocea .

var angular = require('angular');
var deferredBootstrapper = require('angular-deferred-bootstrap');

var apiUrl = 'http://localhost:8080/v1';

var app = angular.module('adminApp',
    [require('ng-admin'), require('ng-admin-auth')]
);

app.config(['NgAdminConfigurationProvider', 'ngAdminJWTAuthConfiguratorProvider', 'NG_ADMIN_CONF', function (nga, ngAdminJWTAuthConfigurator, NG_ADMIN_CONF) {
    ngAdminJWTAuthConfigurator.setJWTAuthURL(apiUrl + '/auth/');
    ngAdminJWTAuthConfigurator.setCustomAuthHeader({
        name: 'Authorization',
        template: 'Bearer {{token}}'
    });
    var admin = nga.application('Admin').debug(false).baseApiUrl(apiUrl + '/');
    ngAdminJWTAuthConfigurator.setLoginSuccessCallback(function() {window.location.reload();});

    if (NG_ADMIN_CONF) {
        // construct menu from the JSON in NG_ADMIN_CONF
    }
    nga.configure(admin);

}]);

app.run(['$location', 'NG_ADMIN_CONF', function($location, NG_ADMIN_CONF) {
    if (NG_ADMIN_CONF && $location.path() == '/login') {
        $location.path('/dashboard');
    }
}]);

deferredBootstrapper.bootstrap({
  element: document.body,
  module: 'adminApp',
  resolve: {
    NG_ADMIN_CONF: ['$http', '$q', function ($http, $q) {
        if (localStorage.userToken) {
            return $http.get(apiUrl + '/config.json', {'headers': {'Authorization': 'Bearer ' + localStorage.userToken}}).error(function (response) {
                localStorage.removeItem('userRole');
                localStorage.removeItem('userToken');
                localStorage.removeItem('user');
                window.location.reload();
            });
        } else {
            return $q(function(resolve, reject) {resolve();});
        }
    }]
  }
});