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