Skip to content

Commit

Permalink
Added Spark dependency to AuthService
Browse files Browse the repository at this point in the history
  • Loading branch information
Barrett Alexander committed Mar 27, 2015
1 parent e087f15 commit 5015050
Show file tree
Hide file tree
Showing 3 changed files with 50 additions and 35 deletions.
22 changes: 10 additions & 12 deletions source/app/auth/auth.service.js
Expand Up @@ -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',
Expand All @@ -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);
Expand All @@ -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);
};
Expand All @@ -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);
};

})();
42 changes: 19 additions & 23 deletions source/app/auth/cloud-auth-modal.controller.js
Expand Up @@ -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)
Expand All @@ -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)
Expand Down
21 changes: 21 additions & 0 deletions source/app/choosatrons/spark.factory.js
Expand Up @@ -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();
Expand Down

0 comments on commit 5015050

Please sign in to comment.