如何存储在角用户会话? [英] How to store user session in Angular?

查看:135
本文介绍了如何存储在角用户会话?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我刚刚使用AngularJS开始,我试图存储用户会话我AngularApp。

I have just started using AngularJS and I'm trying to store user session on my AngularApp.

第一步提交用户名和密码的作品。
在那之后,我存储用户名 $ rootScope 服务检索。
下一个页面可以显示用户名存储

First step to submit username and password works. After that, I store the username retrieved from the service in the $rootScope. The next page can display the username stored.

不过,在刷新后,在 $ rootScope 是空的。

But after a refresh, the $rootScope is empty.

我试图做的尽可能简单的认证系统。

I'm trying to do an authentication system as simple as possible.

myApp.controller('loginController', ['$scope', '$rootScope', '$location', 'AuthService', '$route',
  function ($scope, $rootScope, $location, AuthService, $route) {

      $scope.login = function (credentials) {
        AuthService.login(credentials).then(function (response) {
          if(response.data == "0"){
            alert("Identifiant ou mot de passe incorrect");
          }
          else {
            // response.data is the JSON below 
            $rootScope.credentials = response.data;           
            $location.path("/");
          }
        });
      };

}]);

AuthService.login()发出 $ HTTP 请求。

JSON

 {user_id: 1, user_name: "u1", user_display_name: "Steffi"} 

HTML

 <div>Welcome {{ credentials.user_display_name }}!</div>

我试过很多教程,但我不能让会议的工作。
我已经习惯UserApp但它并不确定我。我想创建自己的简单的身份验证。

推荐答案

$ rootScope将始终重置页面刷新的时候,因为它是一个单页的应用程序。

$rootScope will always reset when the page refreshes, since it's a single-page app.

您需要使用的东西,仍然存在客户端,如一个cookie或sessionStorage的(因为它们都具有过期时间)。看看为$的CookieStore的文档:<一href=\"https://docs.angularjs.org/api/ngCookies/service/\">https://docs.angularjs.org/api/ngCookies/service/$cookieStore

You need to use something that persists client-side, such as a cookie or sessionStorage (as they both have an expiration time). Take a look at the documentation for $cookieStore: https://docs.angularjs.org/api/ngCookies/service/$cookieStore

记住,敏感的会话信息进行加密。

Remember, sensitive session information should be encrypted.

这篇关于如何存储在角用户会话?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆