-
Notifications
You must be signed in to change notification settings - Fork 0
/
auth.js
47 lines (41 loc) · 1.33 KB
/
auth.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
// Credit to meteor faster render https://github.com/kadirahq/fast-render/blob/master/lib/client/auth.js
import { Meteor } from 'meteor/meteor';
import { Cookie } from 'meteor/chuangbo:cookie';
// Getting tokens for the first time
// Meteor calls Meteor._localStorage.setItem() on the boot
// But we can do it ourselves also with this
const setToken = (loginToken, expires) => {
Cookie.set('meteor_login_token', loginToken, {
path: '/',
expires
});
};
const resetToken = () => {
const loginToken = Meteor._localStorage.getItem('Meteor.loginToken');
const loginTokenExpires = new Date(
Meteor._localStorage.getItem('Meteor.loginTokenExpires')
);
if (loginToken) {
setToken(loginToken, loginTokenExpires);
} else {
setToken(null, -1);
}
};
// override Meteor._localStorage methods and resetToken accordingly
const originalSetItem = Meteor._localStorage.setItem;
Meteor._localStorage.setItem = (key, value) => {
if (key === 'Meteor.loginToken') {
Meteor.defer(resetToken);
}
originalSetItem.call(Meteor._localStorage, key, value);
};
const originalRemoveItem = Meteor._localStorage.removeItem;
Meteor._localStorage.removeItem = key => {
if (key === 'Meteor.loginToken') {
Meteor.defer(resetToken);
}
originalRemoveItem.call(Meteor._localStorage, key);
};
Meteor.startup(() => {
resetToken();
});