Created
February 9, 2015 18:37
-
-
Save leonardorame/762f4978edbf7a4633b0 to your computer and use it in GitHub Desktop.
ng-admin login
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/* | |
Small ng-admin config showing how to inject a Login controller when | |
a 401 error is returned by the server. | |
*/ | |
(function () { | |
"use strict"; | |
var app = angular.module('myApp', [ | |
'ng-admin', | |
'login' | |
]); | |
app.run(['Restangular', '$location', function(Restangular, $location){ | |
// ==== CODE TO DO 401 NOT LOGGED IN CHECKING | |
//This code will intercept 401 unauthorized errors returned from web requests. | |
//On default any 401 will make the app think it is not logged in. | |
Restangular.setErrorInterceptor(function(response, deferred, responseHandler) { | |
if(response.status === 401){ | |
$location.path('/login'); | |
return false; | |
} | |
}); | |
}]); | |
app.config( | |
function ($stateProvider, NgAdminConfigurationProvider, Application, Entity, Field, Reference, ReferencedList, ReferenceMany, RestangularProvider) { | |
function truncate(value) { | |
if (!value) { | |
return ''; | |
} | |
return value.length > 50 ? value.substr(0, 50) + '...' : value; | |
} | |
$stateProvider | |
.state('login', { | |
url: '/login', | |
templateUrl: 'login.html', | |
controller: 'loginController' | |
}); | |
// use the custom query parameters function to format the API request correctly | |
RestangularProvider.addFullRequestInterceptor(function(element, operation, what, url, headers, params) { | |
if (operation == "getList") { | |
// custom pagination params | |
params._start = (params._page - 1) * params._perPage; | |
params._end = params._page * params._perPage; | |
delete params._page; | |
delete params._perPage; | |
// custom sort params | |
if (params._sortField) { | |
params._sort = params._sortField; | |
delete params._sortField; | |
} | |
// custom filters | |
if (params._filters) { | |
for (var filter in params._filters) { | |
params[filter] = params._filters[filter]; | |
} | |
delete params._filters; | |
} | |
} | |
return { params: params }; | |
}); | |
var admin = new Application('Control Panel') // application main title | |
.baseApiUrl(location.protocol + '//' + location.hostname + '/cgi-bin/mypanel/') | |
// define all entities at the top to allow references between them | |
var item = new Entity('item') // the API endpoint for posts will be http://localhost/cgi-bin/posts/:id | |
.identifier(new Field('IdItem')); | |
// set the application entities | |
admin | |
.addEntity(item); | |
// customize entities and views | |
// ... | |
NgAdminConfigurationProvider.configure(admin); | |
}); | |
}()); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
I am getting this error:
Error: [$injector:modulerr] Failed to instantiate module login due to:
Error: [$injector:nomod] Module 'login' is not available! You either misspelled the module name or forgot to load it. If registering a module ensure that you specify the dependencies as the second argument.