FlowRouter React Auth


#1

Hello! I have a problem with auth. I want to close several pages for unregistered users. My router.

const authenticatedRedirect = () => {
    if (!Meteor.loggingIn() && !Meteor.userId()) {
        FlowRouter.go( 'SignIn' );
    }
};

const authenticatedRoutes = FlowRouter.group({
    name: 'authenticated',
    triggersEnter: [ authenticatedRedirect ]
});

FlowRouter.route('/login', {
	name: 'SignIn',
	action() {
		mount(Auth, {
			content: (<Login />)
		})
	}
});

FlowRouter.route('/register', {
	name: 'SignUp',
	action() {
		mount(Auth, {
			content: (<Register />)
		})
	}
});

authenticatedRoutes.route('/', {
	name: 'Dashboard',
	action() {
		mount(App, {
			content: (<Dashboard />)
		})
	}
});

The problem is when I create a new user with Accounts.createUser it’s automatically logged me in and redirects to Dashboard. It’s good.
But consol says Error logging in with token: Error: You’ve been logged out by the server. Please log in again. [403]
And server return errors in console. Thats because i’m not logged in and server don’t know userId. But if i reload page with F5 all works well.

I20180711-13:51:08.103(3)? Exception from sub contracts.getAllSuccessfullyClosedContracts id yw4PgTphAHwefm5q9 TypeError: Cannot read property '_id' of undefined
I20180711-13:51:08.104(3)?     at Subscription.getAllSuccessfullyClosedContracts (imports/api/contracts/server/publications.js:23:47)
I20180711-13:51:08.105(3)?     at Subscription.newPublishFunction [as _handler] (packages/peerlibrary_reactive-publish/server.coffee:198:30)
I20180711-13:51:08.106(3)?     at maybeAuditArgumentChecks (packages/ddp-server/livedata_server.js:1767:12)
I20180711-13:51:08.106(3)?     at DDP._CurrentPublicationInvocation.withValue (packages/ddp-server/livedata_server.js:1043:15)
I20180711-13:51:08.107(3)?     at Meteor.EnvironmentVariable.EVp.withValue (packages/meteor.js:1189:12)
I20180711-13:51:08.107(3)?     at Subscription._runHandler (packages/ddp-server/livedata_server.js:1041:51)
I20180711-13:51:08.108(3)?     at packages/ddp-server/livedata_server.js:826:41
I20180711-13:51:08.108(3)?     at Function._.each._.forEach (packages/underscore.js:147:22)
I20180711-13:51:08.109(3)?     at packages/ddp-server/livedata_server.js:822:9
I20180711-13:51:08.109(3)?     at Meteor.EnvironmentVariable.EVp.withValue (packages/meteor.js:1189:12)
I20180711-13:51:08.109(3)?     at Session._setUserId (packages/ddp-server/livedata_server.js:816:34)
I20180711-13:51:08.110(3)?     at MethodInvocation.setUserId [as _setUserId] (packages/ddp-server/livedata_server.js:677:14)
I20180711-13:51:08.110(3)?     at MethodInvocation.setUserId (packages/ddp-common/method_invocation.js:92:10)
I20180711-13:51:08.110(3)?     at MethodInvocation.methods.logout (packages/accounts-base/accounts_server.js:554:10)
I20180711-13:51:08.111(3)?     at maybeAuditArgumentChecks (packages/ddp-server/livedata_server.js:1767:12)
I20180711-13:51:08.112(3)?     at DDP._CurrentMethodInvocation.withValue (packages/ddp-server/livedata_server.js:719:19)

Can you help me?
Regards, Evgeny.
P.S. Sorry for my English.


#2

I recommend to not do redirects based on login state. Instead, just show a different content:

// may contain typos...

const NeedsAuth = withTracker(() => {
  const isLoggingIn = Meteor.loggingIn();
  const isLoggedIn = Boolean(Meteor.userId())
  return { isLoggingIn, isLoggedIn }
})(
( {children, isLoggedIn, isLoggingIn} ) => (
 isLoggingIn ? <LoadingSpinner /> : isLoggedIn ? children : <Login />
)

// then

FlowRouter.route('/', {
	name: 'Dashboard',
	action() {
		mount(App, {
			content: (<NeedsAuth><Dashboard /></NeedsAuth>)
		})
	}
});


#3

Thank you very much! I’ll try later. Not at office now.