Created
February 13, 2014 09:54
-
-
Save intellix/8972488 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
'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