-
Notifications
You must be signed in to change notification settings - Fork 45
User Context For SSR #116
Comments
Nope, I think the server would need to set an auth cookie, so that when the browser does a |
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. |
@smeijer |
I needed to adjust my code a bit, as I'm using the // 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 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');
}
}); |
Thanks!
SCOTT TOLINSKI
www.scotttolinski.com
…On Tue, Mar 6, 2018 at 1:49 PM, Stephan Meijer ***@***.***> wrote:
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(d => {
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(d => {
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');
}
});
—
You are receiving this because you authored the thread.
Reply to this email directly, view it on GitHub
<#116 (comment)>,
or mute the thread
<https://github.com/notifications/unsubscribe-auth/AAo2x6mcJ2w3A3yZ0RoOyoxktCy62eBFks5tbvZmgaJpZM4SJDlm>
.
|
New issue for tracking documenting SSR: #126 |
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.
The text was updated successfully, but these errors were encountered: