// 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 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 accountModule = require('./accounts'), operationModule = require('./operations'), schedulerModule = require('./scheduler'); require('bootstrap-webpack!./bootstrap.config.js'); 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, 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 .when('/account/:accountId/operations', { templateUrl: operationsTmpl, controller: 'OperationController', controllerAs: 'operationsCtrl' }) .when('/account/:accountId/scheduler', { templateUrl: schedulerTmpl, controller: 'SchedulerController', controllerAs: 'schedulerCtrl' }) .when('/accounts', { templateUrl: accountsTmpl, controller: 'AccountController', controllerAs: 'accountsCtrl' }) .otherwise({ redirectTo: '/accounts' }); }) .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(); }); }) ; module.exports = app;