From 59efecef7b4f513369b95de20507a2a8da7640f3 Mon Sep 17 00:00:00 2001 From: "Spratley, Jonnie" Date: Fri, 19 Dec 2014 15:50:34 -0800 Subject: [PATCH] renamed files --- app/scripts/services/cms-authservice.js | 80 +++++++++++++++++++ .../services/cms-dataservicefactory.js | 10 +++ .../services/cms-dataserviceprovider.js | 15 ++++ app/scripts/services/cms-notify.js | 27 +++++++ app/scripts/services/cms-sessionservice.js | 79 ++++++++++++++++++ app/scripts/services/cms-socketservice.js | 48 +++++++++++ app/scripts/services/cms-usersfactory.js | 19 +++++ 7 files changed, 278 insertions(+) create mode 100644 app/scripts/services/cms-authservice.js create mode 100644 app/scripts/services/cms-dataservicefactory.js create mode 100644 app/scripts/services/cms-dataserviceprovider.js create mode 100644 app/scripts/services/cms-notify.js create mode 100644 app/scripts/services/cms-sessionservice.js create mode 100644 app/scripts/services/cms-socketservice.js create mode 100644 app/scripts/services/cms-usersfactory.js diff --git a/app/scripts/services/cms-authservice.js b/app/scripts/services/cms-authservice.js new file mode 100644 index 0000000..9c1c2c0 --- /dev/null +++ b/app/scripts/services/cms-authservice.js @@ -0,0 +1,80 @@ +'use strict'; + +/** +@module AuthService +@description +This service will take care of authentication of a user, common methods include: +* authorize +* logout +* register +* forgot +* currentUser + */ +angular.module('angularCmsApp').service('cmsAuthService', function($q, $http, $log, $rootScope, $cookieStore, $location, cmsSessionService, cmsNotify) { + var cmsAuthService; + return cmsAuthService = { + endpoint: '/api/v2', + + /** + authorize - I handle authorizing a user. + */ + authorize: function(user) { + return $http.post(this.endpoint + "/login", user); + }, + + /** + session - I handle getting a session. + */ + session: function() { + return $http.get(this.endpoint + "/session"); + }, + + /** + register - I handle register a user. + */ + register: function(user) { + $log.info('trying to register', user); + return $http.post(this.endpoint + "/register", user).then((function(_this) { + return function(res) { + $log.info(res); + return _this.authorize(res.data); + }; + })(this), function(err) { + $log.error(err); + return cmsNotify('.message', 'danger', 'Error!', err.data.message, 4000); + }); + }, + + /** + Logout method to clear the session. + @param {Object} user - A user model containing remember + */ + logout: function(user) { + cmsSessionService.setUserAuthenticated(null); + return $rootScope.apply(function() { + return $location.reload(); + }); + }, + + /** + Login + */ + login: function(user) { + return this.authorize(user).then(function(res) { + var session; + cmsNotify('.login-message', 'success', 'Success!', "Welcome back.", 5000); + session = { + user: res.data, + authorized: true + }; + cmsSessionService.setSession(session); + $rootScope.App.session = session; + $log.info('login-result', res); + return $rootScope.App.location.path('/dashboard'); + }, function(err) { + $log.error(err); + return cmsNotify('.login-message', 'danger', 'Error!', err.data.message); + }); + } + }; +}); diff --git a/app/scripts/services/cms-dataservicefactory.js b/app/scripts/services/cms-dataservicefactory.js new file mode 100644 index 0000000..39c1cd9 --- /dev/null +++ b/app/scripts/services/cms-dataservicefactory.js @@ -0,0 +1,10 @@ +'use strict'; +angular.module('angularCmsApp').factory('cmsDataServiceFactory', function() { + var meaningOfLife; + meaningOfLife = 42; + return { + someMethod: function() { + return meaningOfLife; + } + }; +}); diff --git a/app/scripts/services/cms-dataserviceprovider.js b/app/scripts/services/cms-dataserviceprovider.js new file mode 100644 index 0000000..560439d --- /dev/null +++ b/app/scripts/services/cms-dataserviceprovider.js @@ -0,0 +1,15 @@ +var DataServiceProvider; + +angular.module("angularCmsApp").provider("cmsDataServiceProvider", DataServiceProvider = function() { + var DataServiceFactory, options; + DataServiceFactory = void 0; + options = void 0; + this.options = function(value) { + return options = !!value; + }; + this.$get = [ + "options", DataServiceFactory = function(options) { + return new cmsDataService(options); + } + ]; +}); diff --git a/app/scripts/services/cms-notify.js b/app/scripts/services/cms-notify.js new file mode 100644 index 0000000..dc2517e --- /dev/null +++ b/app/scripts/services/cms-notify.js @@ -0,0 +1,27 @@ +'use strict'; +angular.module('angularCmsApp').factory('cmsNotify', [ + '$timeout', '$q', function($timeout, $q) { + var notices, notify; + notices = []; + notify = function(el, type, title, msg, timeout) { + var alert; + notices.push({ + type: type, + title: title, + msg: msg + }); + alert = "
\n \n " + title + " " + msg + "\n
"; + if (el) { + angular.element(el).prepend(alert); + } else { + angular.element('.container').prepend(alert); + } + if (timeout) { + return $timeout(function() { + return angular.element('.alert').fadeOut().remove(); + }, timeout); + } + }; + return notify; + } +]); diff --git a/app/scripts/services/cms-sessionservice.js b/app/scripts/services/cms-sessionservice.js new file mode 100644 index 0000000..dea9b60 --- /dev/null +++ b/app/scripts/services/cms-sessionservice.js @@ -0,0 +1,79 @@ +angular.module('angularCmsApp').service('cmsSessionService', [ + '$q', '$rootScope', '$cookieStore', '$location', '$log', function($q, $rootScope, $cookieStore, $location, $log) { + var SessionService, getUserAuthenticated, setUserAuthenticated, userIsAuthenticated; + userIsAuthenticated = $cookieStore.get('App.session'); + setUserAuthenticated = function(value) { + window.sessionStorage.setItem('userIsAuthenticated', value); + $cookieStore.put('App.session', value); + userIsAuthenticated = value; + return $log.info("user is authorized: " + userIsAuthenticated.authorized); + }; + getUserAuthenticated = function() { + window.sessionStorage.getItem('userIsAuthenticated'); + $log.info("user is authorized: " + userIsAuthenticated.authorized); + return userIsAuthenticated.authorized; + }; + $rootScope.$on("$locationChangeStart", function(event, next, current) { + var i, msg, _results; + $rootScope.$emit('session:route:start', { + event: event, + next: next, + current: current + }); + angular.element('.active').removeClass('active'); + _results = []; + for (i in window.routes) { + if (next.indexOf(i) !== -1) { + if (window.routes[i].requireLogin && !getUserAuthenticated()) { + msg = "You need to be authenticated to see this page!"; + $log.warn(msg); + event.preventDefault(); + $rootScope.$emit('session:unauthorized', event); + _results.push($location.path('/')); + } else { + angular.element('a[href="#' + $location.path() + '"]').addClass('active'); + _results.push($rootScope.$emit('session:authorized', event)); + } + } else { + _results.push(void 0); + } + } + return _results; + }); + SessionService = { + adapter: null, + session: $cookieStore.get('App.session'), + isAuthenticated: false, + getUserAuthenticated: getUserAuthenticated, + setUserAuthenticated: setUserAuthenticated, + getSession: function() { + if ($cookieStore.get('App.session')) { + return $cookieStore.get('App.session'); + } else { + return {}; + } + }, + setSession: function(value) { + return $cookieStore.put('App.session', value); + }, + login: function(user) { + var _ref; + $rootScope.$emit('session:login', user); + return (_ref = SessionService.adapter) != null ? typeof _ref.login === "function" ? _ref.login(user) : void 0 : void 0; + }, + logout: function(user) { + var _ref; + $rootScope.$emit('session:logout', user); + SessionService.setUserAuthenticated(user); + return (_ref = SessionService.adapter) != null ? typeof _ref.logout === "function" ? _ref.logout(user) : void 0 : void 0; + }, + register: function(user) { + var _ref; + $rootScope.$emit('session:register', user); + return (_ref = SessionService.adapter) != null ? typeof _ref.register === "function" ? _ref.register(user) : void 0 : void 0; + }, + routeResolver: function() {} + }; + return SessionService; + } +]); diff --git a/app/scripts/services/cms-socketservice.js b/app/scripts/services/cms-socketservice.js new file mode 100644 index 0000000..d9ade8e --- /dev/null +++ b/app/scripts/services/cms-socketservice.js @@ -0,0 +1,48 @@ +'use strict'; + +/** + * @ngdoc service + * @name angularCmsApp.cmsSocketService + * @description + * # cmsSocketService + * Service in the angularCmsApp. + */ +angular.module('angularCmsApp').factory('cmsSocketService', function ($rootScope) { + /** + * I am a WebSocketClient + * @param options + * @returns {{instance: WebSocket, close: Function, send: Function}} + * @constructor + */ + var WebSocketClient = function (options) { + var _ws = new WebSocket(options.endpoint, options.protocol); + _ws.onmessage = function (e) { + $rootScope.$emit(options.protocol, e); + return console.log(e.data); + }; + _ws.onerror = function (e) { + return console.log(e); + }; + _ws.onclose = function (e) { + return console.log(e); + }; + _ws.onopen = function (e) { + _ws.send('update'); + }; + return { + instance: _ws, + close: function () { + return _ws.close(); + }, + send: function (obj) { + try { + _ws.send(JSON.stringify(obj)); + } catch (err) { + + throw err; + } + } + }; + }; + return WebSocketClient; +}); diff --git a/app/scripts/services/cms-usersfactory.js b/app/scripts/services/cms-usersfactory.js new file mode 100644 index 0000000..b7892e4 --- /dev/null +++ b/app/scripts/services/cms-usersfactory.js @@ -0,0 +1,19 @@ + +/** +@ngdoc service +@name angularCmsApp.service:cmsUsersFactory +@function + +@description + This is the UsersFactory. + */ +'use strict'; +angular.module('angularCmsApp').factory('cmsUsersFactory', function() { + var meaningOfLife; + meaningOfLife = 42; + return { + someMethod: function() { + return meaningOfLife; + } + }; +});