I am facing a strange issue where sometime Meteor.userId() happens to return null once in a while in between multiple request to backend.
example: (all method calls in below example are from same connection)
1st call id printed: ekj2k4h8fh74hd8s
2nd call id printed: ekj2k4h8fh74hd8s
"
"
"
n-1th call id printed: ekj2k4h8fh74hd8s
nth call id printed: null (some where in middle randomly)
n+1th call id printed: ekj2k4h8fh74hd8s
"
"
"
"
2nd last call id printed: ekj2k4h8fh74hd8s
last call id printed: ekj2k4h8fh74hd8s
This issue has already been reported in Meteor 3. We are tracking it and plan to fix it here. In the meantime, we’ve provided a workaround. Could you check if this solved this for your?
If this is another user availability issues on the client, we could address them all in one go.
Hi @nachocodoner , I am using METEOR@2.15, I am not facing any incomplete data issue, for me the issue is that Meteor.userId() itself is null at backend
So, do you have any possibility to create a small repo where it ever happens?
If we can reproduce it constantly, we can also double check if this affects Meteor 3. But maybe is also some specific package or project setup. We need reproducible flows.
@nachocodoner thanks for quick replies, i will try for the setup as am blocked.
on the other hand, i have created a meteor method which i am calling from client to check if user is active or not by checking Meteor.userId() at backend. Am doing all this because of this reported issue
when above package logs out user from backend using code similar to above snippet, Accounts.onLogout(callback) callback is not triggered, so i am manually trying to fix issue by checking userId at backend.
No. If you empty the loginTokens, the user will be logged out when Meteor tries to resume the login and see that the user doesn’t have a valid login token. At this point this message should show up in the console tab.
The Accounts.onLogout() is triggered when you call the Meteor.logout or on specific cases when you call a login method with a user already logged in.
Logout the user such that it triggered Accounts.onLogout() by it self at client side, as of now i am doing hacks which is leading to issue (issue mentioned in this ticket at the start). i,e, i created a method at server and calling it from client to check if user is still logged-in in an interval of 30 seconds.
I do not know a way to do this automatically without a subscription. In Meteor, the user is subscribed anyway and you just have to watch that variable in your subscription and if the variable is unset, then you know that the user is logged out.
Looks like a possibility If i can somehow logout the user at server at first place, i cannot depend on browser to logout as user might have closed the browser.
Wanna logout the user at server, also let client know (if browser open, which can be done using subscription)
I am confused about what is triggering this process.
What is removing the tokens from the DB? That function that removes the tokens from the DB can be the same function that triggers whatever you want to do in the server when this user is logged out
@rjdavid , i want to logout the user from backend when user is inactive and also redirect user to login screen if browser active.
The birth of the process of logging out user and informing client to redirect to login page happens at backend (client will listen only if browser active)
Like you have suggested, I can set user tokens to empty at backend and subscribe to user tokens at client side, then redirect user to login screen whenever tokens are emptied. (I will 100% give this a try)
Isn’t it kind of unclean way to set tokens empty at server and logout user from client (if browser active) by calling Meteor.logout when subscribe triggered, and then listening to Accounts.onLogout to redirect user to login screen? The cleaner way might look like, call Meteor.logout(userId) at backend itself, which triggers Accounts.onLogout at client by itself.
Am curious, if there already exist something like Meteor.logout(userId) at server side.
There is a feature in Accounts that automatically expires login tokens after a set period. So expiring login tokens is the Meteor-way of removing the login session of the user. There is no function as of now that can do that for you in the server (afaik).
You do not have to use Meteor.logout() in the client.
You just need to track Meteor.user() or Meteor.userId(). Once these become null redirect the user to login page