简体   繁体   English

Angular4会话处理

[英]Angular4 session handling

I have a sailsjs backend for API's and I want to use Angular4 for my frontend. 我有一个用于API的sailsjs后端,我想在我的前端使用Angular4。 I was wondering, how can I handle sessions on Angular? 我想知道,我如何处理Angular上的会话? I tried reading the documentation and was unable to come up with anything. 我尝试阅读文档并且无法提出任何建议。

Specifically I need to be able to handle the login, log out, CSRF protection, etc. Is it possible to use Angular4 in Sails because Sails has all of that built in? 具体来说,我需要能够处理登录,注销,CSRF保护等。是否可以在Sails中使用Angular4,因为Sails内置了所有内容?

As you want to use sails with Angular4 you may need add sails.io.js in your index.html as <script src="/assets/sails.io/sails.io.js"></script> ,keeping your sails.io.js in assets/sails.io/ folder then import SailsModule and register the sails module in your in app.module.ts .Also add 由于你想在Angular4中使用sails,你可能需要在index.html中添加sails.io.js作为<script src="/assets/sails.io/sails.io.js"></script> ,保持风帆在assets / sails.io /文件夹中的.io.js然后导入SailsModule并在app.module.ts中注册sails模块。还要添加

imports:[SailsModule.forRoot()]

You may need to inject SailsService in the constructor and ngOnInit() call the connect() method of SailsService. 您可能需要在构造函数中注入SailsS​​ervice,并且ngOnInit()调用SailsS​​ervice的connect()方法。 Then you can use request(options):Observable method of SailsService to get the request(check http://sailsjs.com/documentation/reference/web-sockets/socket-client/io-socket-request and check http://sailsjs.com/documentation/concepts/sessions ) with the help of req.session you can get session value and property. 然后你可以使用request(options):Observable SailsS​​ervice的request(options):Observable方法来获取请求(查看http://sailsjs.com/documentation/reference/web-sockets/socket-client/io-socket-request并检查http:// sailsjs.com/documentation/concepts/sessions )的帮助下req.session你可以得到会话的价值和性能。

yes, you can manage session by storing session details through a session service. 是的,您可以通过会话服务存储会话详细信息来管理会话。

for best practice use google firebase auth feature which manages all the session, login and logout. 为了获得最佳实践,请使用google firebase auth功能来管理所有会话,登录和注销。

easy to implement... free from google... maintained by google.... 易于实施...免费谷歌...由谷歌维护....

Currently, I am working on an angular4 app whose backend API's are written in PHP and I am simply hitting them from my angular front end and getting the data, now in order to maintain session, I am simply using the SessionStorage and sometimes LocalStorage properties of javascript that allow us to save key/value pairs in a web browser. 目前,我正在开发一个angular4应用程序,其后端API是用PHP编写的,我只是从我的角度前端点击它们并获取数据,现在为了维护会话,我只是使用SessionStorage,有时使用LocalStorage属性允许我们在Web浏览器中保存键/值对的JavaScript。

ex: sessionStorage.setItem("keyname", "value"); 例如: sessionStorage.setItem(“keyname”,“value”); // to set // 设置

var myvar=sessionStorage.getItem("keyname"); var myvar = sessionStorage.getItem(“keyname”); //to get //要得到

You have two options to do this 您有两种选择

1. window.SessionStorage 2. Angular Service 1. window.SessionStorage 2. Angular Service

1.In your case you can use SessionStroage So whenever user is logged in, just set the data in sessionStroage like this 1.在您的情况下,您可以使用SessionStroage因此,只要用户登录,只需在sessionStroage中设置数据,就像这样

sessionStorage.setItem('userData', userObj);

and on logout 并在注销时

sessionStroage.clear();

2. Second Option is to put your data in a globalService So this service will contain in-app data and as soon as user is logged out, just clear the service Data. 2.第二个选项是将您的数据放入globalService中,因此该服务将包含应用内数据,一旦用户注销,只需清除服务数据即可。 Like this:- 像这样:-

@Injectable()
export class GlobalService {
login(){
  //initialize data here....
}

logout(){
   //remove your data....
 }
}

Note: if browser tab is closed this data will be lost. 注意:如果关闭浏览器选项卡,则此数据将丢失。 if you want to recover this data then put it inside window.localStorage 如果要恢复此数据,请将其放在window.localStorage中

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM