diff --git a/src/js/app.js b/src/js/app.js index d7a61f3..85f5226 100644 --- a/src/js/app.js +++ b/src/js/app.js @@ -20,20 +20,10 @@ var angular = require('angular'); -var ngRoute = require('angular-route'), - ngBootbox = require('ngbootbox'), - ngStorage = require('meanie-angular-storage'), - ngHttpAuth = require('angular-http-auth'); - -var base64 =require('base64util'); - -// Note: ngBootbox seems to have no module.exports. -ngBootbox = 'ngBootbox'; - -// Note: ngHttpAuth seems to have no module.exports. -ngHttpAuth = 'http-auth-interceptor'; +var ngRoute = require('angular-route'); var accountModule = require('./accounts'), + loginModule = require('./login'), operationModule = require('./operations'), schedulerModule = require('./scheduler'); @@ -43,42 +33,14 @@ var operationsTmpl = require('./operations/operations.html'); var accountsTmpl = require('./accounts/accounts.html'); var schedulerTmpl = require('./scheduler/scheduler.html'); -var loginTmpl = require('../views/login.tmpl.html'); - var app = angular.module('accountant', [ accountModule.name, + loginModule.name, operationModule.name, schedulerModule.name, ngRoute, - ngBootbox, - ngHttpAuth, - ngStorage ]) -.factory('sessionInjector', function($storage) { - var sessionInjector = { - request: function(config) { - var access_token = $storage.session.get('access_token'); - - if (access_token) { - //var tokenType = $storage.get('token_type'); - var tokenType = 'Bearer'; - var authorization = tokenType + ' ' + access_token; - config.headers.authorization = authorization; - } - - return config; - } - }; - - return sessionInjector; -}) - -.config(function($httpProvider) { - // Define interceptors. - $httpProvider.interceptors.push('sessionInjector'); -}) - .config(function($routeProvider) { // Defining template and controller in function of route. $routeProvider @@ -102,98 +64,11 @@ var app = angular.module('accountant', [ }); }) -.config(function($storageProvider) { - // Configure storage - // Set global prefix for stored keys - $storageProvider.setPrefix('accountant'); - - // Change the default storage engine - // Defaults to 'local' - $storageProvider.setDefaultStorageEngine('session'); - - // Change the enabled storage engines - // Defaults to ['memory', 'cookie', 'session', 'local'] - $storageProvider.setEnabledStorageEngines(['local', 'session']); -}) - .run(function(editableOptions) { editableOptions.theme = 'bs3'; // bootstrap3 theme. Can be also 'bs2', 'default' }) -.factory('LoginService', function($http) { - var login = function(email, password) { - var authdata = base64.encode(email + ':' + password); - - return $http.post('/api/user/login', {}, { - headers: { - 'authorization': 'Basic ' + authdata - } - }); - }; - - return { - 'login': login - }; -}) - -.controller('MainController', function($rootScope, LoginService, authService, $storage, $ngBootbox, $document) { - var vm = this; - - vm.dialogShown = false; - - vm.showLoginForm = function() { - // First, if there are registered credentials, use them - if (vm.dialogShown) { - return; - } - - vm.dialogShown = true; - - $storage.session.clear(); - - $ngBootbox.customDialog({ - title: 'Authentification requise', - templateUrl: loginTmpl, - buttons: { - login: { - label: 'Login', - className: 'btn-primary', - callback: function() { - vm.dialogShown = false; - - var email = angular.element($document[0].querySelector('#email')).val(); - var password = angular.element($document[0].querySelector('#password')).val(); - LoginService.login( - email, password - ).then(function(result) { - // TODO Alexis Lahouze 2015-08-28 Handle callback. - // Call to /api/login to retrieve the token - $storage.session.set('refresh_token', result.data.refresh_token); - $storage.session.set('access_token', result.data.access_token); - - authService.loginConfirmed(); - }); - } - }, - cancel: { - label: 'Annuler', - className: 'btn-default', - callback: function() { - authService.loginCancelled(null, 'Login cancelled by user action.'); - vm.dialogShown = false; - } - } - } - }); - }; - - vm.onAuthLoginRequired = $rootScope.$on('event:auth-loginRequired', vm.showLoginForm); - - $rootScope.$on('$destroy', function() { - vm.onAuthLoginRequired = angular.noop(); - }); -}) - -; +.controller('MainController', function() { +}); module.exports = app; diff --git a/src/js/login/index.js b/src/js/login/index.js new file mode 100644 index 0000000..b478e12 --- /dev/null +++ b/src/js/login/index.js @@ -0,0 +1,157 @@ +// vim: set tw=80 ts=4 sw=4 sts=4: +/* + This file is part of Accountant. + + Accountant is free software: you can redistribute it and/or modify + it under the terms of the GNU Affero General Public License as published by + the Free Software Foundation, either version 3 of the License, or + (at your option) any later version. + + Accountant is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + GNU Affero General Public License for more details. + + You should have received a copy of the GNU Affero General Public License + along with Accountant. If not, see . + */ +/* jshint node: true */ +'use strict'; + +var angular = require('angular'); + +var ngBootbox = require('ngbootbox'), + ngStorage = require('meanie-angular-storage'), + ngHttpAuth = require('angular-http-auth'); + +// Note: ngBootbox seems to have no module.exports. +ngBootbox = 'ngBootbox'; + +// Note: ngHttpAuth seems to have no module.exports. +ngHttpAuth = 'http-auth-interceptor'; + +var loginTmpl = require('./login.tmpl.html'); + +var base64 = require('base64util'); + +var loginModule = angular.module('accountant.login', [ + ngBootbox, + ngHttpAuth, + ngStorage +]) + +.factory('LoginService', function($http) { + var login = function(email, password) { + var authdata = base64.encode(email + ':' + password); + + return $http.post('/api/user/login', {}, { + headers: { + 'authorization': 'Basic ' + authdata + } + }); + }; + + return { + 'login': login + }; +}) + +.factory('sessionInjector', function($storage) { + var sessionInjector = { + request: function(config) { + var access_token = $storage.session.get('access_token'); + + if (access_token) { + //var tokenType = $storage.get('token_type'); + var tokenType = 'Bearer'; + var authorization = tokenType + ' ' + access_token; + config.headers.authorization = authorization; + } + + return config; + } + }; + + return sessionInjector; +}) + +.config(function($httpProvider) { + // Define interceptors. + $httpProvider.interceptors.push('sessionInjector'); +}) + +.config(function($storageProvider) { + // Configure storage + // Set global prefix for stored keys + $storageProvider.setPrefix('accountant'); + + // Change the default storage engine + // Defaults to 'local' + $storageProvider.setDefaultStorageEngine('session'); + + // Change the enabled storage engines + // Defaults to ['memory', 'cookie', 'session', 'local'] + $storageProvider.setEnabledStorageEngines(['local', 'session']); +}) + +.value('dialogShow', false) + +.run(function($rootScope, LoginService, authService, $storage, $ngBootbox, $document) { + + //var dialogShown = false; + + var showLoginForm = function() { + // First, if there are registered credentials, use them + //if (dialogShown) { + // return; + //} + + //dialogShown = true; + + $storage.session.clear(); + + $ngBootbox.customDialog({ + title: 'Authentification requise', + templateUrl: loginTmpl, + buttons: { + login: { + label: 'Login', + className: 'btn-primary', + callback: function() { + //dialogShown = false; + + var email = angular.element($document[0].querySelector('#email')).val(); + var password = angular.element($document[0].querySelector('#password')).val(); + LoginService.login( + email, password + ).then(function(result) { + // TODO Alexis Lahouze 2015-08-28 Handle callback. + // Call to /api/login to retrieve the token + $storage.session.set('refresh_token', result.data.refresh_token); + $storage.session.set('access_token', result.data.access_token); + + authService.loginConfirmed(); + }); + } + }, + cancel: { + label: 'Annuler', + className: 'btn-default', + callback: function() { + authService.loginCancelled(null, 'Login cancelled by user action.'); + //dialogShown = false; + } + } + } + }); + }; + + var onAuthLoginRequired = $rootScope.$on('event:auth-loginRequired', showLoginForm); + + $rootScope.$on('$destroy', function() { + onAuthLoginRequired = angular.noop(); + }); +}); + + +module.exports = loginModule; diff --git a/src/views/login.tmpl.html b/src/js/login/login.tmpl.html similarity index 100% rename from src/views/login.tmpl.html rename to src/js/login/login.tmpl.html