Sessions in Meteor

本小妞迷上赌 提交于 2019-12-21 21:04:53

问题


After a research it seems that Meteor Sessions are reset after refreshing page or opening the website in new tab, i.e. they are not usual server-side sessions but something like global javascript variables on client-side. Some people advice to use AmplifyJS, but I'm not sure that it will work like usual session in other frameworks/languages and also it is a third party library, so is there any normal way to use sessions in Meteor, i.e. keep user-specific data on server?

At this moment I'm handling that by using custom Collections, but it is not an ideal way of doing that because it is needed to remove expired values from Collection manually, which makes additional troubles.


回答1:


Yes this is correct. Despite the name Session is nothing like a cookie, but just a reactive form of a variable stored in a hashmap

To keep data persistent across tabs you need to use a Collections (as this is the only way to reactively share data across tabs) - Cookies can't work because they can't be made reactive as data needs to be sent to the server to notify the client when there is a change. There really wouldn't be another way at the moment as the publish/subscribe methods can only send down data from collections at the moment.

You can use your setup you have now with your custom collection. You can use a server side cron job to remove expired data (either with Meteor.setInterval or Tom Coleman's cron.




回答2:


There is a package developed just for that: https://atmospherejs.com/u2622/persistent-session

After installation you can use the following functions to set sessions which are persistent:

//store a persistent session variable which is stored across templates
Session.setPersistent(key, value);

//same as above, but automatically deletes session data when user logs out
Session.setAuth(key, value);

I've tried the package and it works like charm.



来源:https://stackoverflow.com/questions/15877554/sessions-in-meteor

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!