注销后销毁会话

Destroying session after logging out

我在我的应用程序中使用 JavaScript、Angular 和 Java。我需要帮助开发破坏会话的注销操作。现在,在注销时 link 我正在调用登录页面,这就是单击浏览器上的“返回”按钮时用户仍然登录的方式。

<li><a href="login.html">Log Out</a></li>

假设您正在使用 /sessions api 创建会话。让会话的响应是一个 id 作为

{id : ghjdgsajdfsgafdfdgasfdafsdfsafdjsa}

您现在可以将此 ID 作为

存储在会话存储中
window.sessionStorage.setItem('currentSession', response.id);

现在注销后,您可以在

上发送删除呼叫
/sessions/window.sessionStorage.getItem('currentSession');

这将破坏会话

而不是从 HTML 更改状态:

<li><a href="login.html">Log Out</a></li>

尝试这样的事情:

<li><a ng-click="logout()">Log Out</a></li>

假设您已经在成功登录时在会话存储中设置了键(用户标识)和值(动态)

storageService.setItem('userid','1');

现在,您可以在注销时执行如下操作:

控制器:

$scope.logout = function() {

  // To remove specify key/value pair from session storage, you need to do something like this.
  storageService.removeItem('userid');

  // Redirection on home page after remove the item from session storage.
  $state.go('home'); 
     ----  OR -----
  $location.path('/home');

}

storageService工厂:

app.factory('storageService', function () {

    return {

        getItem: function (key) {
            return sessionStorage.getItem(key);
        },

        setItem: function (key,data) {
            sessionStorage.setItem(key, data);
        },

        removeItem: function (key) {
            sessionStorage.removeItem(key);
        }
    };
});