javascript - 一旦用户在 Angular js 中注销,重定向到登录页面而不是仪表板
问题描述
我使用 api 创建了一个 http 发布请求,现在我遇到了一个小问题。注销表单后,我仍然可以访问主页而无需输入登录详细信息。即使在注销后会话也不会过期。这是我在登录控制器中使用的代码:
var app = angular.module('LoginApp');
app.controller('loginController', function($scope,$http,$rootScope,$window) {
$scope.postdata = function () {
var post = $http({
method: "POST",
url: "https://api-stg.martcart.pk/api/v1/user/login",
dataType: 'json',
data: JSON.stringify({
userName: $scope.username,
password: $scope.password
}),
headers: { "Content-Type": "application/json" }
});
post.success(function (data) {
window.location='components/home/home.html';
sessionStorage.setItem("authdata",JSON.stringify(data.token));
});
post.error(function () {
alert("wrong credentials");
});
}
$scope.logout = function(){
// console.log("hello")
window.location.href = 'http://localhost:5500/LoginAngularJS-API-master/index.html#/login';
$window.sessionStorage.removeItem("authdata");
//localStorage.removeItem("authdata");
// $route.reload();
// delete $window.sessionStorage;
// $window.localStorage.clear();
}
});
单击注销按钮后,将调用 logout.js 函数:
function logout() {
window.location.href = 'http://127.0.0.1:5500/LoginAngularJS-API-master/index.html#/login';
sessionStorage.removeItem("authdata");
// sessionStorage.destroy();
// localStorage.removeItem("authdata");
// delete $window.sessionStorage;
// $window.localStorage.clear();
}
如果有人可以帮助解决问题,我会很高兴。非常感谢
解决方案
您的代码看起来不错。也许试试sessionStorage.removeItem("authdata");
没有 $window。
推荐阅读
- python-2.7 - 从文件中读取数字到多维数组
- android - 有没有办法在执行登录按钮意图之前检查电子邮件和密码编辑文本是否已填写?
- excel - 将 Google Apps 脚本转换为 VBA
- c++ - 比较 Char 数组
- java - 尝试将本机 SQL 转换为 HQL 时出现异常?
- python - 为什么 python 2 和 3 对于某些十六进制值有不同的打印输出?
- python-3.x - 使用 Pandas 找到每个唯一组的最高值
- javascript - 我的请假申请表中的回形针选项以附加文件
- android - 如何在滑动刷新布局中检索特定用户
- sql-server - DBeaver 连接已关闭 MS SQL