Is there a post createUser hook in meteor when using accounts-ui package?

后端 未结 6 1014
广开言路
广开言路 2020-12-03 06:58

Let\'s say I have a todo app, and I want to make sure that every user that registers has at least one todo to start with, something like \"First todo to cross off!\", how wo

6条回答
  •  执笔经年
    2020-12-03 07:45

    One of the Meteor devs answered this question in Meteor google group: https://groups.google.com/forum/?fromgroups=#!topic/meteor-talk/KSz7O-tt4w8

    Basically, right now, there is no createUser hook when using accounts-ui, only when programmatically doing so via Accounts.createUser. Also, there are no hooks for login, unless using the lower-level login functions like loginWithFacebook, etc. I haven't figured out an ideal way around this yet, but a few ways of handling it:

    • if needing to enter a default value into a collection, in that collection's subscription, use the onComplete argument. In this callback, if there are no entries in collection, add one. This avoids the first problem I mentioned in my post about not knowing when a collection was loaded, though not ideal since collection could be empty because user already removed first default one:

      Meteor.subscribe 'todos', user: Meteor.userId(), () ->
        todo = Todos.findOne()
        unless todo
          Todos.insert user: Meteor.userId()
      
    • you can set up a login hook by using the Meteor.autorun reactive method to check for a change in Meteor.userId(). That'll only get called when the user logs in/reloads the page. This is more useful for non-collection stuff since the collection is not guaranteed to be loaded when Meteor.userId is set:

      Meteor.autorun () ->
        if Meteor.userId()
          console.log 'Do some post login hook'
      

    So I think the efficient solution is still out there somewhere, but wanted to update this post with workarounds I had found in the meantime.

提交回复
热议问题