Skip to content

Instantly share code, notes, and snippets.

@intellix
Created February 13, 2014 09:54
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save intellix/8972488 to your computer and use it in GitHub Desktop.
Save intellix/8972488 to your computer and use it in GitHub Desktop.
'use strict';
angular.module('myApp').service('UserService', function($http, $q, $location) {
var self = {
identity: {
id: 'guest'
},
load: function() {
if (self.identity.id === 'guest') {
var promise = $http({
method: 'GET',
url: 'http://localhost:8080/users/me'
}).then(function(response) {
angular.copy(response.data, self.identity);
return self.identity;
});
return promise;
}
var d = $q.defer();
d.resolve(self.categories);
return d.promise;
},
login: function(username, password) {
// Get token
var promise = $http({
url: 'http://localhost:8080/oauth',
method: 'POST',
data: {
'client_id': 'cherrytech',
'client_secret': 'password',
'grant_type': 'password',
'username': username,
'password': password
}
}).then(function(response) {
$http.defaults.headers.common.Authorization = response.data.token_type + ' ' + response.data.access_token;
self.load();
});
return promise;
},
logout: function() {
self.identity = {
id: 'guest'
};
return self.identity;
},
create: function(user) {
var promise = $http({
url: 'http://localhost:8080/users',
method: 'POST',
data: user
}).then(function(response) {
console.log('response', response.data);
});
return promise;
// Make registration call
//self.load();
}
};
return self;
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment