docula 1.2.0 → 1.5.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -90,43 +90,32 @@
90
90
  })();
91
91
  </script>
92
92
  {{#if cookieAuth}}
93
- <div id="cookie-auth-config" hidden data-cookie-name="{{#if cookieAuth.cookieName}}{{cookieAuth.cookieName}}{{else}}token{{/if}}"{{#if cookieAuth.logoutUrl}} data-logout-url="{{cookieAuth.logoutUrl}}"{{/if}}></div>
93
+ <div id="cookie-auth-config" hidden{{#if cookieAuth.logoutUrl}} data-logout-url="{{cookieAuth.logoutUrl}}"{{/if}}{{#if cookieAuth.authCheckUrl}} data-auth-check-url="{{cookieAuth.authCheckUrl}}"{{/if}}{{#if cookieAuth.authCheckMethod}} data-auth-check-method="{{cookieAuth.authCheckMethod}}"{{/if}}{{#if cookieAuth.authCheckUserPath}} data-auth-check-user-path="{{cookieAuth.authCheckUserPath}}"{{/if}}></div>
94
94
  <script>
95
95
  (function() {
96
96
  var configEl = document.getElementById('cookie-auth-config');
97
97
  if (!configEl) return;
98
- var cookieName = configEl.getAttribute('data-cookie-name');
99
98
  var logoutUrl = configEl.getAttribute('data-logout-url');
100
- function isSafeUrl(url) {
101
- if (!url) return false;
102
- try {
103
- var parsed = new URL(url, window.location.origin);
104
- return parsed.origin === window.location.origin;
105
- } catch (e) {
106
- return false;
99
+ var authCheckUrl = configEl.getAttribute('data-auth-check-url');
100
+ var authCheckMethod = configEl.getAttribute('data-auth-check-method') || 'GET';
101
+ var authCheckUserPath = configEl.getAttribute('data-auth-check-user-path');
102
+ function getNestedValue(obj, path) {
103
+ if (!path) return null;
104
+ var parts = path.split('.');
105
+ var current = obj;
106
+ for (var i = 0; i < parts.length; i++) {
107
+ if (current == null || typeof current !== 'object') return null;
108
+ current = current[parts[i]];
107
109
  }
110
+ return current || null;
108
111
  }
109
- function getCookieValue() {
110
- var match = document.cookie.split(';').find(function(c) {
111
- return c.trim().startsWith(cookieName + '=');
112
- });
113
- return match ? match.trim().substring(cookieName.length + 1) : null;
114
- }
115
- function getDisplayName(token) {
116
- try {
117
- var payload = token.split('.')[1];
118
- if (!payload) return null;
119
- var json = atob(payload.replace(/-/g, '+').replace(/_/g, '/'));
120
- var claims = JSON.parse(json);
121
- return claims.name || claims.preferred_username || claims.email || null;
122
- } catch (e) {
123
- return null;
124
- }
125
- }
126
- function updateAuthUI() {
127
- var token = getCookieValue();
128
- var loggedIn = !!token;
129
- var displayName = loggedIn ? getDisplayName(token) : null;
112
+ var cachedAuth = null;
113
+ try { cachedAuth = JSON.parse(localStorage.getItem('docula-auth-state')); } catch(e) {}
114
+ window.__doculaAuth = cachedAuth || { loggedIn: false, displayName: null };
115
+ function setAuthUI(loggedIn, displayName) {
116
+ window.__doculaAuth = { loggedIn: loggedIn, displayName: displayName };
117
+ try { localStorage.setItem('docula-auth-state', JSON.stringify(window.__doculaAuth)); } catch(e) {}
118
+ document.dispatchEvent(new CustomEvent('docula-auth-change'));
130
119
  var els = [
131
120
  { login: document.getElementById('cookie-auth-login'), logout: document.getElementById('cookie-auth-logout'), user: document.getElementById('cookie-auth-user') },
132
121
  { login: document.getElementById('cookie-auth-login-mobile'), logout: document.getElementById('cookie-auth-logout-mobile'), user: document.getElementById('cookie-auth-user-mobile') }
@@ -140,8 +129,29 @@
140
129
  }
141
130
  });
142
131
  }
132
+ function checkAuth() {
133
+ if (!authCheckUrl) return;
134
+ fetch(authCheckUrl, { method: authCheckMethod, credentials: 'include' }).then(function(res) {
135
+ if (!res.ok) {
136
+ setAuthUI(false, null);
137
+ return;
138
+ }
139
+ if (authCheckUserPath) {
140
+ res.json().then(function(data) {
141
+ setAuthUI(true, getNestedValue(data, authCheckUserPath));
142
+ }).catch(function() {
143
+ setAuthUI(true, null);
144
+ });
145
+ } else {
146
+ setAuthUI(true, null);
147
+ }
148
+ }).catch(function() {
149
+ setAuthUI(false, null);
150
+ });
151
+ }
143
152
  document.addEventListener('DOMContentLoaded', function() {
144
- updateAuthUI();
153
+ if (cachedAuth) setAuthUI(cachedAuth.loggedIn, cachedAuth.displayName);
154
+ checkAuth();
145
155
  var logoutEls = [
146
156
  document.getElementById('cookie-auth-logout'),
147
157
  document.getElementById('cookie-auth-logout-mobile')
@@ -149,10 +159,9 @@
149
159
  logoutEls.forEach(function(el) {
150
160
  if (el) {
151
161
  el.addEventListener('click', function() {
152
- if (isSafeUrl(logoutUrl)) {
162
+ if (logoutUrl) {
153
163
  window.location.href = logoutUrl;
154
164
  } else {
155
- document.cookie = cookieName + '=; expires=Thu, 01 Jan 1970 00:00:00 GMT; path=/';
156
165
  window.location.reload();
157
166
  }
158
167
  });
@@ -130,11 +130,9 @@ document.addEventListener('DOMContentLoaded', function() {
130
130
  authValueInput.value = '';
131
131
  if (cookieStatusEl) {
132
132
  cookieStatusEl.classList.remove('api-auth__cookie-status--hidden');
133
- var configEl = document.getElementById('cookie-auth-config');
134
- var cookieName = configEl ? configEl.getAttribute('data-cookie-name') : (data.name || 'token');
135
- var hasCookie = document.cookie.split(';').some(function(c) { return c.trim().startsWith(cookieName + '='); });
136
- cookieStatusEl.textContent = hasCookie ? 'Logged in' : 'Not logged in — use Login button above';
137
- cookieStatusEl.className = 'api-auth__cookie-status' + (hasCookie ? ' api-auth__cookie-status--ok' : ' api-auth__cookie-status--warn');
133
+ var auth = window.__doculaAuth || { loggedIn: false };
134
+ cookieStatusEl.textContent = auth.loggedIn ? 'Logged in' : 'Not logged in — use Login button above';
135
+ cookieStatusEl.className = 'api-auth__cookie-status' + (auth.loggedIn ? ' api-auth__cookie-status--ok' : ' api-auth__cookie-status--warn');
138
136
  }
139
137
  } else {
140
138
  authValueInput.classList.remove('api-auth__value--hidden');
@@ -148,7 +146,20 @@ document.addEventListener('DOMContentLoaded', function() {
148
146
  if (cookieStatusEl) cookieStatusEl.classList.add('api-auth__cookie-status--hidden');
149
147
  }
150
148
  }
151
- authTypeSelect.addEventListener('change', updateAuthUI);
149
+ var savedAuth = localStorage.getItem('docula-api-auth-type');
150
+ if (savedAuth) {
151
+ for (var i = 0; i < authTypeSelect.options.length; i++) {
152
+ if (authTypeSelect.options[i].value === savedAuth) {
153
+ authTypeSelect.selectedIndex = i;
154
+ break;
155
+ }
156
+ }
157
+ }
158
+ authTypeSelect.addEventListener('change', function() {
159
+ localStorage.setItem('docula-api-auth-type', authTypeSelect.value);
160
+ updateAuthUI();
161
+ });
162
+ document.addEventListener('docula-auth-change', updateAuthUI);
152
163
  updateAuthUI();
153
164
  }
154
165