Reputation: 2971
Guess i didn't have enough sleep and cannot figure out it. Followed web login online samples, login button works, but not the logout button, the logout is not called in server.js.
Thanks very much !
app.config(['$routeProvider',
function($routeProvider) {
$routeProvider.when('/login', {
templateUrl: '/partials/login.html',
controller: 'authController'
});
$routeProvider.when('/logout', {
templateUrl: '/partials/logout.html',
controller: 'authController',
});
...
);
app.controller('authController', function($scope, $http, $location, $window) {
$scope.user = {username:'', password:''};
$scope.alert = '';
$scope.login = function(user) {
$http.post('/auth/login', user).
success(function(data) {
console.log("you are at /auth/login 0");
$scope.loggedUser = data;
$window.location.href = "/index.html#/systemStatus";
}).
error(function() {
console.log("you are at /auth/login 1");
$scope.alert = 'Login failed'
});
};
$scope.logout = function() {
$http.get('/auth/logout').
success(function() {
console.log("you are at /auth/logout 0");
$scope.loggedUser = {};
}).
error(function() {
console.log("you are at /auth/logout 1");
$scope.alert = 'Logout failed'
});
};
});
login.html (This is the default page, always need to login 1st)
<div ng-controller="authController">
<div style="margin-top:10px" class="form-group">
<div class="col-sm-12 controls">
<a id="btn-login" class="btn btn-success" ng-click="login(user)">Login </a>
</div>
</div>
</div>
main.html (This is the 1st page after successful login)
<html ng-app="myModule" ng-controller="MainCtrl">
...
<div class="container" ng-controller="authController">
<a class="btn btn-md btn-info custom-btn" ng-href="#/logout">Logout</a>
</div>
...
</html>
server.js
app.get('/auth/logout', function(req, res)
{
console.log("logging out ......");
req.session.distory();
req.logout();
//res.redirect('/login.html');
res.send(200);
});
Upvotes: 2
Views: 6131
Reputation: 713
You need to notify the browser to refresh the login info. This can be done by sending a 401 error code.
app.get('/auth/logout', function(req, res)
{
console.log("logging out ......");
req.logout();
res.send(401);
});
.
Upvotes: 0
Reputation: 2868
The problem is angular routing takes place instead of express routing. For express to route the request, you need to add target="_self" in your anchor tag of logout link.
<a class="btn btn-md btn-info custom-btn" ng-href="#/logout" target="_self">Logout</a>
Upvotes: 2
Reputation: 426
Use
req.logout();
req.session.destroy();
res.redirect("/login");
I am using this in my nodejs app and its working fine.
Upvotes: 0
Reputation: 5657
You spelled destroy wrong:
req.session.destroy();
not
req.session.distory();
Upvotes: 0