Created
September 23, 2014 17:12
-
-
Save cerkit/51e8e76cb0f0f85073d9 to your computer and use it in GitHub Desktop.
AngularJS Client for Basic Authentication - SampleController
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
var baseUrl = 'http://localhost:49587/api'; | |
angular.module('app').controller('SampleController', function ($scope, $http, $cookieStore, Base64) { | |
$scope.refreshData = function () { | |
//Used to display the data | |
if ($cookieStore.get('basicCredentials')) | |
{ | |
$http.defaults.headers.common['Authorization'] = 'Basic ' + $cookieStore.get('basicCredentials'); | |
} | |
$http.get(baseUrl + '/Values').success(function (data) { | |
$scope.Model = data; | |
$scope.loading = false; | |
$scope.currentUser = Base64.decode($cookieStore.get('basicCredentials')).split(":")[0]; | |
}) | |
.error(function () { | |
$scope.error = "An Error has occured while loading data"; | |
$scope.loading = false; | |
}); | |
} | |
$scope.loading = true; | |
$scope.refreshData(); | |
$scope.logout = function () { | |
$cookieStore.remove('basicCredentials'); | |
$scope.currentUser = null; | |
$scope.Model = null; | |
$http.defaults.headers.common.Authorization = ''; | |
$scope.refreshData(); | |
} | |
$scope.updateValue = function (model) { | |
$http.put(baseUrl + '/Values', model); | |
window.setTimeout(function () { $scope.refreshData() }, 1000); | |
; | |
} | |
$scope.$on('event:auth-loginConfirmed', function () { | |
$scope.refreshData(); | |
}); | |
}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment