00-startup.js 2.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364
  1. // PWA
  2. if ('serviceWorker' in navigator) {
  3. window.addEventListener('load', function() {
  4. navigator.serviceWorker.register('/pwa-service-worker.js');
  5. });
  6. }
  7. // Import board converter for on-demand conversion
  8. import '/client/lib/boardConverter';
  9. import '/client/components/boardConversionProgress';
  10. // Import migration manager and progress UI
  11. import '/client/lib/migrationManager';
  12. import '/client/components/migrationProgress';
  13. // Import cron settings
  14. import '/client/components/settings/cronSettings';
  15. // Mirror Meteor login token into a cookie for server-side file route auth
  16. // This enables cookie-based auth for /cdn/storage/* without leaking ROOT_URL
  17. // Token already lives in localStorage; cookie adds same-origin send-on-request semantics
  18. Meteor.startup(() => {
  19. const COOKIE_NAME = 'meteor_login_token';
  20. const cookieAttrs = () => {
  21. const attrs = ['Path=/', 'SameSite=Lax'];
  22. try {
  23. if (window.location && window.location.protocol === 'https:') {
  24. attrs.push('Secure');
  25. }
  26. } catch (_) {}
  27. return attrs.join('; ');
  28. };
  29. const setCookie = (name, value) => {
  30. if (!value) return;
  31. document.cookie = `${encodeURIComponent(name)}=${encodeURIComponent(value)}; ${cookieAttrs()}`;
  32. };
  33. const clearCookie = (name) => {
  34. document.cookie = `${encodeURIComponent(name)}=; Expires=Thu, 01 Jan 1970 00:00:00 GMT; ${cookieAttrs()}`;
  35. };
  36. const syncCookie = () => {
  37. try {
  38. const token = Accounts && typeof Accounts._storedLoginToken === 'function' ? Accounts._storedLoginToken() : null;
  39. if (token) setCookie(COOKIE_NAME, token); else clearCookie(COOKIE_NAME);
  40. } catch (e) {
  41. // ignore
  42. }
  43. };
  44. // Initial sync on startup
  45. syncCookie();
  46. // Keep cookie in sync on login/logout
  47. if (Accounts && typeof Accounts.onLogin === 'function') Accounts.onLogin(syncCookie);
  48. if (Accounts && typeof Accounts.onLogout === 'function') Accounts.onLogout(syncCookie);
  49. // Sync across tabs/windows when localStorage changes
  50. window.addEventListener('storage', (ev) => {
  51. if (ev && typeof ev.key === 'string' && ev.key.indexOf('Meteor.loginToken') !== -1) {
  52. syncCookie();
  53. }
  54. });
  55. });