简体   繁体   English

注销后如何同步页面并登录其中之一?

[英]how to sync pages after logout and login on one of them?

After logout and login I try to click something on another page but see endless spinner due to userId null. How to sync Meteor.userId after changing user?注销并登录后,我尝试单击另一个页面上的某些内容,但由于用户 ID 为 null 而看到无尽的微调器。如何在更改用户后同步 Meteor.userId? ON server side:在服务器端:

Meteor.publish('myPublish', function () {
if (!this.userId) {
  return this.ready();
}
return Collection.find();});

On client side:在客户端:

const handles = [
    Meteor.subscribe('settings')
  ];
  const loading = handles.some(handle => !handle.ready());

The Meteor.userId function is a reactive datasource, which allows you to autorun a Tracker and subscribe, when the userId is there. Meteor.userId function 是一个反应式数据源,它允许您在 userId 存在时自动运行 Tracker 并订阅。

Classic Meteor code would look like this:经典的 Meteor 代码如下所示:

Tracker.autorun(() => {
  if (Meteor.userId() && Meteor.user()) {
    Meteor.subscribe('settings') // will only execute, once the user is logged in
  }
})

For React you use withTracker and should include the Meteor.userId to your bindings:对于 React,您使用withTracker并且应该将Meteor.userId包含到您的绑定中:

export default withTracker(({ id }) => {
  const userId = Meteor.userId()
  const handles = userId && [
    Meteor.subscribe('todos.inList', id),
    Meteor.subscribe('otherSub'),
  ];
  const loading = !userId || handles.some(handle => !handle.ready());
  return {
    loading,
  };
})(MyComponent);

The call to Meteor.userId should activate the internal Tracker computation to re-run a cycle, once it returns a different value (a non-null value, once logged in).Meteor.userId的调用应该激活内部 Tracker 计算以重新运行一个循环,一旦它返回一个不同的值(一个非空值,一旦登录)。

You can also use Meteor.loggingIn as reactive data source:您还可以使用Meteor.loggingIn作为反应数据源:

export default withTracker(({ id }) => {
  const loggingIn = Meteor.loggingIn()
  const handles = !loggingIn && [
    Meteor.subscribe('todos.inList', id),
    Meteor.subscribe('otherSub'),
  ];
  const loading = loggingIn || handles.some(handle => !handle.ready());
  return {
    loading,
  };
})(MyComponent);

References:参考:

https://docs.meteor.com/api/tracker.html#Tracker-autorun https://docs.meteor.com/api/tracker.html#Tracker-autorun

https://guide.meteor.com/react.html#using-withTracker https://guide.meteor.com/react.html#using-withTracker

https://docs.meteor.com/api/accounts.html#Meteor-userId https://docs.meteor.com/api/accounts.html#Meteor-userId

https://docs.meteor.com/api/accounts.html#Meteor-loggingIn https://docs.meteor.com/api/accounts.html#Meteor-loggingIn

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

相关问题 会话注销后如何登录而不丢失表格数据 - How to login without loss form data after session Logout 如何在 React Native 中登录和注销后在 2 个不同的导航器之间导航 - How to navigate between 2 different navigators after login and logout in react native JWT Token以及登录后如何使用? - JWT Token and how to use them after login? 如何在成功登录后重定向并在reactjs中阻止页面而无需登录? - how to redirect after successful login and block pages without login in reactjs? 如何使用Facebook Login和ReactJS实现登录和注销? - How to implement login and logout with Facebook Login and ReactJS? 退出并登录后不显示Jquery UI Datepicker - Jquery UI Datepicker not displaying after logout and login 注销后删除Facebook登录按钮SDK - Remove Facebook Login Button SDK after a logout 身份服务器:在 Angular 中登录和注销后重定向? - Identity Server: Redirect after login and logout in Angular? 注销后无法登录 Laravel - Can't login after logout Laravel jQuery未加载到Django管理主页/仪表板,登录和注销页面中 - jQuery not loading in Django admin homepage/dashboard, login and logout pages
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM