SSR and this.userId


#1

I’ve got a UI problem. When I use SSR to render a public page, there doesn’t appear to be a way to check if the user is logged in or not. As a result the navigation bar always renders with login/register links and then after the client side loads, which is a split second later, if the user is logged in the navbar updates accordingly. This isn’t a huge issue, it just doesn’t look very clean.

Is there a clean solution to fix this?


#2

With my knowledge, there is no solution. Maybe somehow you store the token in cookie and try to read it in server side.
But, I use SSR for SEO purpose only, then it’s not the problem.


#3

My solution is to skip SSR for authenticated users, figuring they are going to stick around, even with a slower page load (maybe this is a faulty assumption, but it’s what I do). What I do is set a cookie (called sssr - skip server side rendering) on sign in, and unset it on sign out. In my server-render script, I simply checks that, and doesn’t bother rendering on the server if it’s set.


#4

Thanks @captainn I’ve read about the use of cookies. @minhna suggested the same thing.

Can you share your code for doing this?


#5

Meteor store the login token in local storage:

  • Meteor.loginToken
  • Meteor.loginTokenExpires
  • Meteor.userId

You can read them and set the cookie value.


#6

I haven’t actually done this, but there is a package called fast-render which has done it. Take a look at the source code there for some guidance. Most of what you need is probably in auth.js.


#7

You haven’t specified which view layer you are using, but if it’s React then you could check out https://atmospherejs.com/communitypackages/react-router-ssr.