Commit 4ac4b39b359aa5f92ef0c67e8115b75b442fa7e3

Authored by Victor Costa
1 parent 984e478b
Exists in master and in 1 other branch dev-fixes

Try to authenticate the user before load

bower.json
... ... @@ -17,7 +17,8 @@
17 17 "moment": "~2.10.6",
18 18 "animate.css": "~3.4.0",
19 19 "angular": "~1.4.2",
20   - "font-awesome": "fontawesome#~4.5.0"
  20 + "font-awesome": "fontawesome#~4.5.0",
  21 + "ngstorage": "~0.3.10"
21 22 },
22 23 "devDependencies": {
23 24 "angular-mocks": "~1.4.2"
... ...
src/app/components/auth/auth.service.js 0 → 100644
... ... @@ -0,0 +1,86 @@
  1 +(function() {
  2 + 'use strict';
  3 +
  4 + angular
  5 + .module('angular')
  6 + .factory('Session', Session)
  7 + .factory('AuthService', AuthService);
  8 +
  9 + /** @ngInject */
  10 + function AuthService($q, $http, $rootScope, Session, $log) {
  11 +
  12 + function login (credentials) {
  13 + var url = '/api/v1/login';
  14 + var encodedData = 'login=' + credentials.username + '&password=' + credentials.password;
  15 + return $http.post(url, encodedData).then(loginSuccessCallback, loginFailedCallback);
  16 + }
  17 +
  18 + function loginFromCookie() {
  19 + var url = '/api/v1/login_from_cookie';
  20 + return $http.post(url).then(loginSuccessCallback, loginFailedCallback);
  21 + }
  22 +
  23 + function loginSuccessCallback(response) {
  24 + $log.debug('AuthService.login [SUCCESS] response', response);
  25 + var currentUser = Session.create(response.data);
  26 + $rootScope.currentUser = currentUser;
  27 + $rootScope.$broadcast('login-success', currentUser);
  28 + return currentUser;
  29 + }
  30 +
  31 + function loginFailedCallback(response) {
  32 + $log.debug('AuthService.login [FAIL] response', response);
  33 + $rootScope.$broadcast('login-failed');
  34 + return $q.reject(response);
  35 + }
  36 +
  37 + function logout () {
  38 + Session.destroy();
  39 + $rootScope.currentUser = undefined;
  40 + $rootScope.$broadcast('logout-success');
  41 + }
  42 +
  43 + function isAuthenticated () {
  44 + return !!Session.userId;
  45 + }
  46 +
  47 + function isAuthorized (authorizedRoles) {
  48 + if (!angular.isArray(authorizedRoles)) {
  49 + authorizedRoles = [authorizedRoles];
  50 + }
  51 + return (service.isAuthenticated() && authorizedRoles.indexOf(Session.userRole) !== -1);
  52 + }
  53 +
  54 + var service = {
  55 + login: login,
  56 + loginFromCookie: loginFromCookie,
  57 + logout: logout,
  58 + isAuthenticated: isAuthenticated,
  59 + isAuthorized: isAuthorized
  60 + };
  61 + return service;
  62 + }
  63 +
  64 + /** @ngInject */
  65 + function Session($localStorage, $log) {
  66 + var service = {};
  67 +
  68 + service.create = function(data) {
  69 + $localStorage.currentUser = data.user;
  70 + $log.debug('User session created.', $localStorage.currentUser);
  71 + return $localStorage.currentUser;
  72 + };
  73 +
  74 + service.destroy = function() {
  75 + delete $localStorage.currentUser;
  76 + $log.debug('User session destroyed.');
  77 + };
  78 +
  79 + service.getCurrentUser = function () {
  80 + return $localStorage.currentUser;
  81 + };
  82 +
  83 + return service;
  84 + }
  85 +
  86 +})();
... ...
src/app/index.module.js
... ... @@ -2,6 +2,6 @@
2 2 'use strict';
3 3  
4 4 angular
5   - .module('angular', ['ngAnimate', 'ngCookies', 'ngTouch', 'ngSanitize', 'ngMessages', 'ngAria', 'restangular', 'ui.router', 'ui.bootstrap', 'toastr']);
  5 + .module('angular', ['ngAnimate', 'ngCookies', 'ngStorage', 'ngTouch', 'ngSanitize', 'ngMessages', 'ngAria', 'restangular', 'ui.router', 'ui.bootstrap', 'toastr']);
6 6  
7 7 })();
... ...
src/app/index.route.js
... ... @@ -13,7 +13,12 @@
13 13 url: '/:profile',
14 14 templateUrl: 'app/profile/profile.html',
15 15 controller: 'ProfileController',
16   - controllerAs: 'vm'
  16 + controllerAs: 'vm',
  17 + resolve: {
  18 + currentUser: function(AuthService) {
  19 + return AuthService.loginFromCookie();
  20 + }
  21 + }
17 22 })
18 23 .state('profile.page', {
19 24 url: '/{page:.*}',
... ...
src/app/index.run.js
... ... @@ -6,9 +6,13 @@
6 6 .run(runBlock);
7 7  
8 8 /** @ngInject */
9   - function runBlock($log) {
10   -
11   - $log.debug('runBlock end');
  9 + function runBlock($log, Restangular, Session, AuthService) {
  10 + Restangular.addFullRequestInterceptor(function(element, operation, route, url, headers, params, httpConfig) {
  11 + if(Session.getCurrentUser()) {
  12 + headers['Private-Token'] = Session.getCurrentUser().private_token;
  13 + }
  14 + return { headers: headers };
  15 + });
12 16 }
13 17  
14 18 })();
... ...
src/app/profile/profile.controller.js
... ... @@ -7,13 +7,13 @@
7 7  
8 8  
9 9 /** @ngInject */
10   - function ProfileController(noosfero, $log, $stateParams) {
  10 + function ProfileController(noosfero, $log, $stateParams, $http) {
11 11 var vm = this;
12 12 vm.boxes = [];
13 13 activate();
14 14  
15 15 function activate() {
16   - noosfero.communities.one().get({private_token: '1b00325e5f769a0c38550bd35b3f1d64', identifier: $stateParams.profile}).then(function(communities) {
  16 + noosfero.communities.one().get({identifier: $stateParams.profile}).then(function(communities) {
17 17 vm.owner = communities.communities[0];
18 18 });
19 19 }
... ...