Persistent Login Help

Maybe I’m missing something. But when I call Meteor.loginWithPassword() the user logs in and all is well. But I hit F5 and the login disappears and the user is logged out.

Using React with Meteor

What am I missing?

Thanks :slight_smile:

Not much info to have a guess, but as a wild punt:
If you are saying that the user is logged out because you see the login screen after the reload, I would look at your triggers for showing the login screen (what is the trigger? Is it ensuring that the initial user subscription is ready? Is it reactive?)… maybe the user is logged in but the app hasn’t detected it properly?

Without routes doing anything, after logging in using Meteor.loginWithPassword and redirecting to another page. Meteor.user() returns the user as it should but on refresh I get undefined.

I can’t post my code right now but that’s the general idea.

I just have 2 containers at the moment, login and a landing page. Login page (React component) calls Meteor.loginWithPassword and then redirects to the landing page. On the landing page I’m looking at the Meteor.user result and getting their name, and it works after the first redirect. Then refreshing the page while staying in the landing component clears Meteor.user and it’s undefined.

I’m guessing there’s probably something I’m missing with react. I’m pretty new to react so it’s highly likely I’m just an idiot.

It could be that you’re not waiting for the logging in process to complete as Meteor.user() will be undefined while this process is running. How are you getting Meteor.user()? In a tracker or something reactive or just in your component directly (constructor or render)?

I believe you need to get it in a reactive way for this to work, but the way I do it is something like the following:

export default withTracker(() => {
	const systemValsHandle = Meteor.subscribe('systemValues'),
			systemValsReady = systemValsHandle.ready(),
			userHandle = Meteor.subscribe('userData'),
			userReady = userHandle.ready(),
			loggingIn = Meteor.loggingIn(),
			userID = Meteor.userId();
	return {
		authenticated: !loggingIn && !!Meteor.userId(),
		pageReady: systemValsReady && userReady,

The above uses tracker to check Meteor.loggingIn(). Once done done logging in, then I check for the presence of Meteor.user(). Otherwise, you will always get undefined when it initially loads. Hope that helps!