From 501505048927aaa0d82d156a9296d67d61d40964 Mon Sep 17 00:00:00 2001 From: Barrett Alexander Date: Fri, 27 Mar 2015 12:52:36 -0500 Subject: [PATCH] Added Spark dependency to AuthService --- source/app/auth/auth.service.js | 22 +++++----- .../app/auth/cloud-auth-modal.controller.js | 42 +++++++++---------- source/app/choosatrons/spark.factory.js | 21 ++++++++++ 3 files changed, 50 insertions(+), 35 deletions(-) diff --git a/source/app/auth/auth.service.js b/source/app/auth/auth.service.js index a4f17a9..7f66cec 100644 --- a/source/app/auth/auth.service.js +++ b/source/app/auth/auth.service.js @@ -4,9 +4,12 @@ angular.module('storyApp') .service('authService', AuthService); - AuthService.$inject = []; + AuthService.$inject = ['Spark']; - function AuthService() { + function AuthService(Spark) { + this.spark = function(aAuth) { + return new Spark(aAuth && aAuth.token); + }; this.authStatus = { remoteState: 'idle', status: 'ready', @@ -28,7 +31,7 @@ this.authStatus.status = 'ready'; this.authStatus.remoteState = 'working'; var login = this.login.bind(this, aAuth, aPassword); - return spark + return this.spark(aAuth) .createUser(aAuth.username, aPassword) .then(login) .catch(onError); @@ -48,8 +51,8 @@ var saveToken = this.saveToken.bind(this, aAuth); var onError = this.onError.bind(this); - return spark - .login(params) + return this.spark(aAuth) + .login(aAuth.username, aPassword) .then(saveToken) .catch(onError); }; @@ -70,21 +73,16 @@ AuthService.prototype.logout = function() { this.reset(); - // TODO: This needs to be park of the actual library, not included when built. - //spark.logout(); - spark.accessToken = null; }; AuthService.prototype.loadDevices = function(aAuth) { - spark.accessToken = aAuth.token; - return spark.listDevices.then(function(devices) { + return this.spark(aAuth).listDevices.then(function(devices) { aAuth.devices = devices; }); }; AuthService.prototype.claimDevice = function(aAuth, coreId) { - spark.accessToken = aAuth.token; - return spark.claimCore(coreId); + return this.spark(aAuth).claimCore(coreId); }; })(); diff --git a/source/app/auth/cloud-auth-modal.controller.js b/source/app/auth/cloud-auth-modal.controller.js index 34c82d1..a66dab2 100644 --- a/source/app/auth/cloud-auth-modal.controller.js +++ b/source/app/auth/cloud-auth-modal.controller.js @@ -31,19 +31,17 @@ console.log("Logging in to cloud"); var onComplete = function() { - $scope.$apply(function() { - if (vm.authStatus.status === 'error') { - if (vm.authStatus.error) { - console.log('API call completed on promise fail: ', vm.authStatus.error); - vm.authStatus.error.message = 'Unable to login, try again later.'; - } - console.log(authService.authStatus.remoteState); - } else { - console.log(authService.authStatus.remoteState); - console.log("Cloud Auth Logged in.", vm.profile.cloud); - vm.info = { message: "Logged in to the cloud!" }; + if (vm.authStatus.status === 'error') { + if (vm.authStatus.error) { + console.log('API call completed on promise fail: ', vm.authStatus.error); + vm.authStatus.error.message = 'Unable to login, try again later.'; } - }); + console.log(authService.authStatus.remoteState); + } else { + console.log(authService.authStatus.remoteState); + console.log("Cloud Auth Logged in.", vm.profile.cloud); + vm.info = { message: "Logged in to the cloud!" }; + } }; console.log(authService.authStatus.remoteState); authService.login(profiles.editing.cloud, vm.password) @@ -55,18 +53,16 @@ console.log("Registering in cloud"); var onComplete = function() { - $scope.$apply(function() { - if (vm.authStatus.status === 'error') { - if (vm.authStatus.error) { - console.log('API call completed on promise fail: ', vm.authStatus.error); - vm.authStatus.error.message = 'Unable to register, try again later.'; - } - console.log(authService.authStatus.remoteState); - } else { - console.log("Cloud Auth Registered", vm.profile.cloud); - vm.info = { message: "You've been registered in the cloud!" }; + if (vm.authStatus.status === 'error') { + if (vm.authStatus.error) { + console.log('API call completed on promise fail: ', vm.authStatus.error); + vm.authStatus.error.message = 'Unable to register, try again later.'; } - }); + console.log(authService.authStatus.remoteState); + } else { + console.log("Cloud Auth Registered", vm.profile.cloud); + vm.info = { message: "You've been registered in the cloud!" }; + } }; authService.register(profiles.editing.cloud, vm.password) diff --git a/source/app/choosatrons/spark.factory.js b/source/app/choosatrons/spark.factory.js index a5bd5ac..9998980 100644 --- a/source/app/choosatrons/spark.factory.js +++ b/source/app/choosatrons/spark.factory.js @@ -66,6 +66,27 @@ angular.module('storyApp') return this.baseUrl + '/' + this.version + '/' + path + token; }; + // Creates a new user and logs them in + Spark.prototype.createUser = function(un, pw) { + var deferred = this.$q.defer(); + var data = { + username : un, + password : pw + }; + var self = this; + + function setToken(data) { + self.accessToken = data.access_token || data.accessToken; + deferred.resolve(data); + } + + this.promise('post', this.endpoint('users'), data) + .then(setToken) + .catch(deferred.reject); + + return deferred.promise; + }; + // Logs a member in Spark.prototype.login = function(un, pw) { var deferred = this.$q.defer();