Last active
February 3, 2016 18:06
-
-
Save KerryRitter/ae00da045eb5ff66427d to your computer and use it in GitHub Desktop.
Angular services for ASP.NET Web API
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
/// <reference path="../app.js" /> | |
'use strict'; | |
app.factory('authService', ['$http', '$q', '$log', 'apiBaseUrl', 'localStorageService', function ($http, $q, $log, apiBaseUrl, localStorageService) { | |
var setupHttp = function() { | |
$http.defaults.headers.common["Content-Type"] = "application/x-www-form-urlencoded"; | |
$http.defaults.headers.common['Authorization'] = "Bearer " + authentication.access_token; | |
} | |
var authServiceFactory = {}; | |
var authentication = {}; | |
var saveAuthentication = function(data) { | |
localStorageService.setObject("authentication", data); | |
authentication = data; | |
setupHttp(); | |
}; | |
var loadAuthentication = function() { | |
authentication = localStorageService.getObject("authentication"); | |
setupHttp(); | |
}; | |
var clearAuthentication = function() { | |
authentication = {}; | |
saveAuthentication({}); | |
$http.defaults.headers.common['Authorization'] = ""; | |
}; | |
authServiceFactory.register = function(email, password, confirmPassword) { | |
var registration = { | |
Email: email, | |
Password: password, | |
ConfirmPassword: confirmPassword | |
}; | |
return $http.post(apiBaseUrl + 'api/account/register', registration).then(function (response) { | |
$log.log("Register response:", response); | |
}); | |
}; | |
authServiceFactory.login = function(email, password) { | |
return $http({method: 'POST', url: apiBaseUrl + 'token', data: { username: email, password: password, grant_type: 'password' }, transformRequest: function (obj) { | |
var str = []; | |
for (var p in obj) { | |
// clean up some of the object values because the API was getting angry sometimes | |
str.push(encodeURIComponent(p) + "=" + encodeURIComponent(obj[p])); | |
} | |
return str.join("&"); | |
}}).success(function (data, status, headers, config) { | |
saveAuthentication(data); | |
}).error(function (data, status, headers, config) { | |
clearAuthentication(); | |
}); | |
}; | |
authServiceFactory.logout = function() { | |
clearAuthentication(); | |
}; | |
authServiceFactory.isAuthenticated = function() { | |
return authentication && authentication.userName; | |
}; | |
authServiceFactory.getApiUrl = function(url) { | |
setupHttp(); | |
return apiBaseUrl + url; | |
} | |
authServiceFactory.authentication = authentication; | |
loadAuthentication(); | |
return authServiceFactory; | |
}]); |
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
/// <reference path="../app.js" /> | |
'use strict'; | |
app.factory('localStorageService', ['$window', function($window) { | |
return { | |
set: function(key, value) { | |
$window.localStorage[key] = value; | |
}, | |
get: function(key, defaultValue) { | |
return $window.localStorage[key] || defaultValue; | |
}, | |
setObject: function(key, value) { | |
$window.localStorage[key] = JSON.stringify(value); | |
}, | |
getObject: function(key) { | |
return JSON.parse($window.localStorage[key] || '{}'); | |
} | |
}; | |
}]); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment