Install with Bower:, (*1)
bower install angular-worldskills-utils --save
Load the JavaScript in your HTML file:, (*2)
<script src="bower_components/angular-worldskills-utils/src/angular-worldskills-utils.js"></script>
Add the dependency to your AngularJS module:, (*3)
angular.module('yourApp', ['worldskills.utils']);
Define the following constants (don't forget to replace your client id):, (*4)
angular.module('yourApp').constant('WORLDSKILLS_CLIENT_ID', '<your client id>'); angular.module('yourApp').constant('WORLDSKILLS_API_AUTH', 'https://api.worldskills.org/auth'); angular.module('yourApp').constant('WORLDSKILLS_AUTHORIZE_URL', 'https://auth.worldskills.org/oauth/authorize'); angular.module('yourApp').constant('LOAD_CHILD_ENTITY_ROLES', false); angular.module('yourApp').constant('FILTER_AUTH_ROLES', [<array of application codes>]);
To use any of the following authentication functions, make sure to use the auth
object in at least one controller, e.g.:, (*5)
angular.module('yourApp').controller('ContainerCtrl', function($scope, auth) { $scope.auth = auth; });
To load the previous state after the user has logged in on WorldSkills Auth, you can use sessionStorage.getItem('redirect_to_state')
.
Configure the $urlRouterProvider
as following:, (*6)
angular.module('yourApp').config(function($urlRouterProvider) { $urlRouterProvider.otherwise(function ($injector, $location) { // check for existing redirect var $state = $injector.get('$state'); var redirectToState = sessionStorage.getItem('redirect_to_state'); var redirectToParams = sessionStorage.getItem('redirect_to_params'); sessionStorage.removeItem('redirect_to_state'); sessionStorage.removeItem('redirect_to_params'); if (redirectToState) { if (redirectToParams) { redirectToParams = angular.fromJson(redirectToParams); } else { redirectToParams = {}; } $state.go(redirectToState, redirectToParams); } else { $state.go('your.home.state'); } }); });
To require an authenticated user for certain states, add requireLoggedIn: true
in the state data:, (*7)
.state('your.state', { url: '/your/url', templateUrl: 'views/template.html', controller: 'YourCtrl', data: { requireLoggedIn: true } })
You can also require specific roles for a state with requiredRoles
:, (*8)
.state('your.state', { url: '/your/url', templateUrl: 'views/template.html', controller: 'YourCtrl', data: { requireLoggedIn: true, requiredRoles: [ {code: 100, role: 'Admin'} ] } })
Custom fallback in order to handle forbidden redirect manually rather than to redirect to login url automatically.
Add forbiddenFallbad: function
to app.js
, (*9)
.state('people', { url: '/people?search', templateUrl: 'views/people.html', controller: 'PersonnelCtrl', data:{ requireLoggedIn: true, forbiddenCallback: function(auth, $state){ //state passed from $rootScope.$state $state.go('person.view', {'pid': auth.user.person_id}); }, requiredRoles: [ {code: 600, role: APP_ROLES.ADMIN}, {code: 600, role: APP_ROLES.MANAGER} ] } })
Also now allows custom fallbacks for login error state, use case: redirect to a signup page instead of auth.loginUrl, (*10)
.state('restrictedState', { url: '/needsLogin', templateUrl: 'views/restricted.html', controller: 'RestrictedCtrl', data: { requireLoggedIn: true, unAuthenticatedCallback: function(auth, $state){ $state.go('signup'); }, requiredRoles: [ {code: 1800, role: APP_ROLES.ADMIN}, {code: 1800, role: APP_ROLES.MANAGER}, {code: 1800, role: APP_ROLES.USER} ] } }) .state('signup', { url: '/signup', controller: 'SignupCtrl', templateUrl: 'views/signup.html', data: { requireLoggedIn: false } })
Show an animated loading indicator graphic., (*11)
<ws-spinner ng=show="loading"></ws-spinner>
Show a loading indicator on a button., (*12)
<button type="submit" class="btn btn-success"> Save <ws-text-spinner ng-show="loading" class="ng-hide"></ws-text-spinner> </button>
Added small and big versions, (*13)
<ws-spinner class='bigSpinner'></ws-spinner> <ws-spinner class='smallSpinner'></ws-spinner>