Commit 31af5bec authored by Evren Kutar's avatar Evren Kutar

logout directive

httpprovider with credentials
parent 7ae6ea4d
......@@ -31,7 +31,7 @@ var app = angular.module(
config(['$ocLazyLoadProvider', function ($ocLazyLoadProvider) {
$ocLazyLoadProvider.config({
// todo: turn debug false on prod
debug: true
debug: false
});
}]).
/**
......@@ -66,16 +66,16 @@ var app = angular.module(
// todo: not working properly, get it done!
directive('activeLink', ['$location', function($location) {
directive('activeLink', ['$location', function ($location) {
return {
restrict: 'A',
link: function($scope, $element, $attrs) {
link: function ($scope, $element, $attrs) {
var clazz = $attrs.activeLink;
var path = $location.path();
path = path //hack because path does not
// return including hashbang
$scope.location = $location;
$scope.$watch('location.path()', function(newPath) {
$scope.$watch('location.path()', function (newPath) {
if (path === newPath) {
console.log(path, newPath);
$element.addClass(clazz);
......@@ -86,7 +86,23 @@ var app = angular.module(
});
}
};
}]);
}]).
/**
* logout directive
*/
directive('logout', function($http, $location){
return {
link: function($scope, $element, $rootScope){
$element.on('click', function(){
$http.post('http://127.0.0.1:9001/logout', {}).then(function () {
$rootScope.loggedInUser = false;
$location.path("/login");
});
});
}
}
});
// test the code with strict di mode to see if it works when minified
//angular.bootstrap(document, ['zaerp'], {
......
......@@ -180,4 +180,7 @@ app.config(['$routeProvider', function ($routeProvider) {
}
}
});
});
\ No newline at end of file
}).config(['$httpProvider', function($httpProvider) {
// to send cookies CORS
$httpProvider.defaults.withCredentials = true;
}]);
\ No newline at end of file
......@@ -17,13 +17,13 @@ auth.factory('LoginService', function ($http, $rootScope, $location, $log, $cook
return $http
.post(RESTURL.url + url, credentials)
.success(function (data, status, headers, config) {
$window.sessionStorage.token = data.token;
//$window.sessionStorage.token = data.token;
$rootScope.loggedInUser = true;
$location.path("/dashboard");
})
.error(function (data, status, headers, config) {
// Erase the token if the user fails to log in
delete $window.sessionStorage.token;
//delete $window.sessionStorage.token;
// Handle login errors here
$scope.message = 'Error: Invalid user or password';
......@@ -44,6 +44,16 @@ auth.factory('LoginService', function ($http, $rootScope, $location, $log, $cook
//});
};
loginService.logout = function() {
console.log("logout");
$http.post(RESTURL.url + 'logout', {}).then(function(){
$rootScope.loggedInUser = false;
$location.path("/login");
});
console.log("loggedout");
};
loginService.isAuthenticated = function () {
return !!Session.userId;
};
......
<div ng-app="zaerp.login">
<div class="col-md-6" ng-controller="LoginCtrl">
<h1>Zaerp Login Form</h1>
<div ng-app="zaerp.auth">
<div class="col-md-6">
<h1>Ulakbüs Login Form</h1>
<form name="loginForm" sf-schema="schema" sf-form="form" sf-model="model" ng-submit="onSubmit(loginForm)"></form>
</div>
</div>
\ No newline at end of file
......@@ -37,6 +37,8 @@
<ul class="nav navbar-nav navbar-right">
<li><a href="#/dashboard">Dashboard</a></li>
<li><a href="#/login">Login</a></li>
<li><a href="javascript:void(0);" logout>Logout
</a></li>
</ul>
<form class="navbar-form navbar-right">
<input type="text" class="form-control" placeholder="Search...">
......
......@@ -5,12 +5,12 @@
* (GPLv3). See LICENSE.txt for details.
*/
app.config(['$httpProvider', function ($httpProvider, $rootScope) {
app.config(['$httpProvider', function ($httpProvider) {
/**
* the interceptor for all requests to check response
* 4xx - 5xx errors will be handled here
*/
$httpProvider.interceptors.push(function ($q) {
$httpProvider.interceptors.push(function ($q, $rootScope, $location) {
return {
'request': function(config){
// todo: delete console logs
......@@ -23,23 +23,24 @@ app.config(['$httpProvider', function ($httpProvider, $rootScope) {
},
'response': function (response) {
//Will only be called for HTTP up to 300
if(response.is_login){
$rootScope.loggedInUser = response.is_login;
console.log("login", response.is_login);
if(response.data.is_login){
$rootScope.loggedInUser = response.data.is_login;
console.log("login", response.data.is_login);
$location.path("/dashboard");
}
if(response.screen) {
location.path(response.screen);
$location.path(response.screen);
}
console.log(response);
console.log("login", response);
return response;
},
'responseError': function (rejection) {
// if unauthorized then redirect to login page
if(rejection.status === 400) {
location.reload();
$location.reload();
}
if(rejection.status === 401) {
location.path('#/login');
$location.path('/login');
}
return $q.reject(rejection);
}
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment