Skip to content
This repository has been archived by the owner on Jan 15, 2020. It is now read-only.

User Context For SSR #116

Closed
stolinski opened this issue Feb 16, 2018 · 6 comments
Closed

User Context For SSR #116

stolinski opened this issue Feb 16, 2018 · 6 comments
Labels
feature Feature: new addition or enhancement to existing solutions

Comments

@stolinski
Copy link

Is this possible with this package? If not maybe we can explore a way to implement that feature. I was reading a bit about it with how fast-render managed it, but didn't get much further then that.

@lorensr
Copy link
Contributor

lorensr commented Feb 16, 2018

Nope, I think the server would need to set an auth cookie, so that when the browser does a GET /, it sends along enough information for the server to look up the user. Would be nice, and would be applicable to Meteor in general (outside of Apollo), so most of it should probably be implemented in a separate package.

@lorensr lorensr added the feature Feature: new addition or enhancement to existing solutions label Feb 16, 2018
@smeijer
Copy link

smeijer commented Mar 6, 2018

I'm currently keeping a cookie on the client synced with the login token in local storage. This cookie will be sent as a header with the initial request. Which is being used to lookup the current user.

Maybe not so pretty, but it works.

@stolinski
Copy link
Author

@smeijer
Do you have any available examples of your implementation? I'd love to see how you're doing that.

@smeijer
Copy link

smeijer commented Mar 6, 2018

I needed to adjust my code a bit, as I'm using the onTokenChange hook from meteor-apollo-accounts. But this should work. If not, it for sure only need a little bit finetuning.

// client, set and unset token in cookie

import { Accounts } from 'meteor/accounts-base';
import Cookie from 'js-cookie';

Accounts.onLogin(() => {
  const token = Accounts._storedLoginToken();
  const expires = Accounts._storedLoginTokenExpires();

  if (token) {
    const expireDate = new Date(expires);
    const today = new Date();
    const days = Math.floor((expireDate - today) / 1000 / 3600 / 24);

    Cookie.set('loginToken', token, { expires: days });
  } else {
    Cookie.remove('loginToken');
  }
});

Accounts.onLogout(() => {
  Cookie.remove('loginToken');
  window.location = '/';
});
// server, retrieve token from cookie

import { onPageLoad } from 'meteor/server-render';
import { getUserForContext } from 'meteor/apollo';

onPageLoad(async sink => {
  const { loginToken } = sink.getCookies();
  const { user } = await getUserForContext(loginToken);
});

When using meteor-apollo-accounts, like I am; you could use the onTokenChange instead of the Accounts.onLogin as written above:

import { onTokenChange } from 'meteor-apollo-accounts';
import Cookie from 'js-cookie';

onTokenChange(({ userId, token, tokenExpires } = {}) => {
  if (token) {
    const expireDate = new Date(tokenExpires);
    const today = new Date();
    const days = Math.floor((expireDate - today) / 1000 / 3600 / 24);

    Cookie.set('loginToken', token, { expires: days });
  } else {
    Cookie.remove('loginToken');
  }
});

@stolinski
Copy link
Author

stolinski commented Mar 12, 2018 via email

@lorensr
Copy link
Contributor

lorensr commented Aug 17, 2018

New issue for tracking documenting SSR: #126

@lorensr lorensr closed this as completed Aug 17, 2018
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
feature Feature: new addition or enhancement to existing solutions
Projects
None yet
Development

No branches or pull requests

3 participants