通过用户名或id访问配置文件页面

Access profile page by username or id

本文关键字:访问 配置文件 id 用户      更新时间:2023-09-26

我使用Meteor中的用户系统。帐户是用电子邮件创建的,但我想让用户在设置中设置唯一的用户名。

我有这条路线

Router.route('userProfile', {
  path: '/users/:userId',
  template: 'userProfile',
  fastRender: true,
  waitOn: function() {
    return Meteor.subscribe('singleUser', this.params.userId);
  },
  data: function() {
    return {
      user: Meteor.users.findOne(this.params.userId)
    };
  }
});

,但是如果用户设置了他/她的用户名,路由路径应该改为path: '/:username',。我想Facebook、LinkedIn等也是这么做的。

只需选择单个参数并在出版物和数据函数中检查它:

Meteor.publish('singleUser', function (userIdOrName) {
  return Meteor.users.find({ $or: [ { _id: userIdOrName }, { username: userIdOrName } ] }, {limit: 1});
});
Router.route('userProfile', {
  path: '/users/:userIdOrName',
  template: 'userProfile',
  fastRender: true,
  waitOn: function() {
    return Meteor.subscribe('singleUser', this.params.userIdOrName);
  },
  data: function() {
    return {
      user: Meteor.users.findOne({ $or: [ { _id: this.params.userIdOrName }, { username: this.params.userIdOrName } ] })
    };
  }
});

注意,在这种情况下,您应该确保用户不能将另一个人的ID设置为他或她的用户名,否则他/她可以冒充这个人。