gpt4 book ai didi

angularjs - logout() 无法使用 Passport.js、express.js、AngularJs 工作

转载 作者:太空宇宙 更新时间:2023-11-03 23:34:50 24 4
gpt4 key购买 nike

我猜我 sleep 不足,无法弄清楚。遵循Web登录在线示例,登录按钮可以工作,但注销按钮不起作用,server.js中没有调用注销。

非常感谢!

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(这是默认页面,始终需要先登录)

<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(这是登录成功后的第一个页面)

<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>

服务器.js

app.get('/auth/logout', function(req, res) 
{
console.log("logging out ......");
req.session.distory();
req.logout();
//res.redirect('/login.html');
res.send(200);
});

最佳答案

问题是发生的是 Angular 路由而不是快速路由。为了快速路由请求,您需要在注销链接的 anchor 标记中添加 target="_self"。

<a class="btn btn-md btn-info custom-btn" ng-href="#/logout" target="_self">Logout</a>

关于angularjs - logout() 无法使用 Passport.js、express.js、AngularJs 工作,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33882850/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com