Commit f9f3529745edbcdea3ae80337d2ac4d404a031b1

Authored by Victor Costa
1 parent b2a04c53

Implement login page

src/app/components/auth/auth.controller.js 0 → 100644
@@ -0,0 +1,17 @@ @@ -0,0 +1,17 @@
  1 +(function() {
  2 + 'use strict';
  3 +
  4 + angular
  5 + .module('angular')
  6 + .controller('AuthController', AuthController);
  7 +
  8 +
  9 + /** @ngInject */
  10 + function AuthController(noosfero, $log, $stateParams, AuthService) {
  11 + var vm = this;
  12 + vm.credentials = {};
  13 + vm.login = function() {
  14 + AuthService.login(vm.credentials);
  15 + }
  16 + }
  17 +})();
src/app/components/auth/auth.service.js
@@ -7,7 +7,7 @@ @@ -7,7 +7,7 @@
7 .factory('AuthService', AuthService); 7 .factory('AuthService', AuthService);
8 8
9 /** @ngInject */ 9 /** @ngInject */
10 - function AuthService($q, $http, $rootScope, Session, $log) { 10 + function AuthService($q, $http, $rootScope, Session, $log, AUTH_EVENTS) {
11 11
12 function login (credentials) { 12 function login (credentials) {
13 var url = '/api/v1/login'; 13 var url = '/api/v1/login';
@@ -24,13 +24,13 @@ @@ -24,13 +24,13 @@
24 $log.debug('AuthService.login [SUCCESS] response', response); 24 $log.debug('AuthService.login [SUCCESS] response', response);
25 var currentUser = Session.create(response.data); 25 var currentUser = Session.create(response.data);
26 $rootScope.currentUser = currentUser; 26 $rootScope.currentUser = currentUser;
27 - $rootScope.$broadcast('login-success', currentUser); 27 + $rootScope.$broadcast(AUTH_EVENTS.loginSuccess, currentUser);
28 return currentUser; 28 return currentUser;
29 } 29 }
30 30
31 function loginFailedCallback(response) { 31 function loginFailedCallback(response) {
32 $log.debug('AuthService.login [FAIL] response', response); 32 $log.debug('AuthService.login [FAIL] response', response);
33 - $rootScope.$broadcast('login-failed'); 33 + $rootScope.$broadcast(AUTH_EVENTS.loginFailed);
34 // return $q.reject(response); 34 // return $q.reject(response);
35 return null; 35 return null;
36 } 36 }
@@ -38,7 +38,7 @@ @@ -38,7 +38,7 @@
38 function logout () { 38 function logout () {
39 Session.destroy(); 39 Session.destroy();
40 $rootScope.currentUser = undefined; 40 $rootScope.currentUser = undefined;
41 - $rootScope.$broadcast('logout-success'); 41 + $rootScope.$broadcast(AUTH_EVENTS.logoutSuccess);
42 } 42 }
43 43
44 function isAuthenticated () { 44 function isAuthenticated () {
src/app/components/auth/login.html 0 → 100644
@@ -0,0 +1,16 @@ @@ -0,0 +1,16 @@
  1 +<div class="modal-header">
  2 + <h3 class="modal-title">Login</h3>
  3 +</div>
  4 +<div class="modal-body">
  5 + <form>
  6 + <div class="form-group">
  7 + <label for="exampleInputEmail1">Login / Email address</label>
  8 + <input type="text" class="form-control" id="exampleInputEmail1" placeholder="Login / Email" ng-model="vm.credentials.username">
  9 + </div>
  10 + <div class="form-group">
  11 + <label for="exampleInputPassword1">Password</label>
  12 + <input type="password" class="form-control" id="exampleInputPassword1" placeholder="Password" ng-model="vm.credentials.password">
  13 + </div>
  14 + <button type="submit" class="btn btn-default" ng-click="vm.login()">Login</button>
  15 + </form>
  16 +</div>
src/app/components/navbar/navbar.directive.js
@@ -21,11 +21,27 @@ @@ -21,11 +21,27 @@
21 return directive; 21 return directive;
22 22
23 /** @ngInject */ 23 /** @ngInject */
24 - function NavbarController(moment) { 24 + function NavbarController(moment, $modal, AuthService, Session, $scope, AUTH_EVENTS) {
25 var vm = this; 25 var vm = this;
26 26
27 // "vm.creation" is avaible by directive option "bindToController: true" 27 // "vm.creation" is avaible by directive option "bindToController: true"
28 vm.relativeDate = moment(vm.creationDate).fromNow(); 28 vm.relativeDate = moment(vm.creationDate).fromNow();
  29 + vm.currentUser = Session.getCurrentUser();
  30 +
  31 + vm.openLogin = function() {
  32 + var modalInstance = $modal.open({
  33 + templateUrl: 'app/components/auth/login.html',
  34 + controller: 'AuthController',
  35 + controllerAs: 'vm',
  36 + bindToController: true
  37 + });
  38 + $scope.$on(AUTH_EVENTS.loginSuccess, function() {
  39 + modalInstance.close();
  40 + });
  41 + };
  42 + vm.logout = function() {
  43 + AuthService.logout();
  44 + };
29 } 45 }
30 } 46 }
31 47
src/app/components/navbar/navbar.html
@@ -12,7 +12,7 @@ @@ -12,7 +12,7 @@
12 </ul> 12 </ul>
13 13
14 <ul class="nav navbar-nav navbar-right"> 14 <ul class="nav navbar-nav navbar-right">
15 - <li><a ng-href="#">Login</a></li> 15 + <li><a ng-href="#" ng-show="!vm.currentUser" ng-click="vm.openLogin()">Login</a></li>
16 </ul> 16 </ul>
17 </div> 17 </div>
18 </div> 18 </div>
src/app/index.config.js
@@ -6,10 +6,11 @@ @@ -6,10 +6,11 @@
6 .config(config); 6 .config(config);
7 7
8 /** @ngInject */ 8 /** @ngInject */
9 - function config($logProvider, $locationProvider, RestangularProvider) { 9 + function config($logProvider, $locationProvider, RestangularProvider, $httpProvider) {
10 $logProvider.debugEnabled(true); 10 $logProvider.debugEnabled(true);
11 $locationProvider.html5Mode({enabled: true}); 11 $locationProvider.html5Mode({enabled: true});
12 RestangularProvider.setBaseUrl('/api/v1'); 12 RestangularProvider.setBaseUrl('/api/v1');
  13 + $httpProvider.defaults.headers.post = {'Content-Type': 'application/x-www-form-urlencoded; charset=utf-8'};
13 } 14 }
14 15
15 })(); 16 })();
src/app/index.constants.js
@@ -4,6 +4,11 @@ @@ -4,6 +4,11 @@
4 4
5 angular 5 angular
6 .module('angular') 6 .module('angular')
7 - .constant('moment', moment); 7 + .constant('moment', moment)
  8 + .constant('AUTH_EVENTS', {
  9 + loginSuccess: 'auth-login-success',
  10 + loginFailed: 'auth-login-failed',
  11 + logoutSuccess: 'auth-logout-success'
  12 + });
8 13
9 })(); 14 })();