var app = angular.module("securityApp", ["ngRoute"]);
app.config(["$routeProvider",function ($routeProvider) {
$routeProvider.when("/", {
templateUrl: "templates/home.html",
controller: "HomeController",
resolve: {
auth: function ($q, authenticationSvc) {
var userInfo = authenticationSvc.getUserInfo();
if (userInfo) {
return $q.when(userInfo);
} else {
return $q.reject({ authenticated: false });
}
}
}
}).when("/login", {
templateUrl: "templates/login.html",
controller: "LoginController"
});
}]);
app.run(["$rootScope", "$location", function ($rootScope, $location) {
$rootScope.$on("$routeChangeSuccess", function (userInfo) {
console.log(userInfo);
});
$rootScope.$on("$routeChangeError", function (event, current, previous, eventObj) {
if (eventObj.authenticated === false) {
$location.path("/login");
}
});
}]);
app.factory("authenticationSvc", ["$http","$q","$window",function ($http, $q, $window) {
var userInfo;
function login(userName, password) {
var deferred = $q.defer();
$http.post("/api/login", { userName: userName, password: password })
.then(function (result) {
userInfo = {
accessToken: result.data.access_token,
userName: result.data.userName
};
$window.sessionStorage["userInfo"] = JSON.stringify(userInfo);
deferred.resolve(userInfo);
}, function (error) {
deferred.reject(error);
});
return deferred.promise;
}
function logout() {
var deferred = $q.defer();
$http({
method: "POST",
url: "/api/logout",
headers: {
"access_token": userInfo.accessToken
}
}).then(function (result) {
userInfo = null;
$window.sessionStorage["userInfo"] = null;
deferred.resolve(result);
}, function (error) {
deferred.reject(error);
});
return deferred.promise;
}
function getUserInfo() {
return userInfo;
}
function init() {
if ($window.sessionStorage["userInfo"]) {
userInfo = JSON.parse($window.sessionStorage["userInfo"]);
}
}
init();
return {
login: login,
logout: logout,
getUserInfo: getUserInfo
};
}]);
app.controller("LoginController", ["$scope", "$location", "$window", "authenticationSvc",function ($scope, $location, $window, authenticationSvc) {
$scope.userInfo = null;
$scope.login = function () {
authenticationSvc.login($scope.userName, $scope.password)
.then(function (result) {
$scope.userInfo = result;
$location.path("/");
}, function (error) {
$window.alert("Invalid credentials");
console.log(error);
});
};
$scope.cancel = function () {
$scope.userName = "";
$scope.password = "";
};
}]);
app.controller("HomeController", ["$scope", "$location", "authenticationSvc", "auth",function ($scope, $location, authenticationSvc, auth) {
$scope.userInfo = auth;
$scope.logout = function () {
authenticationSvc.logout()
.then(function (result) {
$scope.userInfo = null;
$location.path("/login");
}, function (error) {
console.log(error);
});
};
}]);