|
107 | 107 | "account", "company", "community", "legal", |
108 | 108 | "admin", "docs", "games", "learn", "marketplace", "toolbox" |
109 | 109 | ]); |
110 | | - const mockDbStorageKey = "gamefoundry.mockDb.v1"; |
111 | | - const mockDbSessionStorageKey = "gamefoundry.mockDb.sessionUser.v1"; |
112 | | - const mockDbSessionModeStorageKey = "gamefoundry.mockDb.sessionMode.v1"; |
113 | 110 |
|
114 | 111 | const currentScript = document.currentScript || document.querySelector("script[src*='gamefoundry-partials.js']"); |
115 | 112 | const assetRoot = currentScript ? new URL("../", currentScript.src) : null; |
|
151 | 148 | }); |
152 | 149 | } |
153 | 150 |
|
154 | | - function readMockDbState() { |
155 | | - try { |
156 | | - const raw = window.localStorage.getItem(mockDbStorageKey); |
157 | | - return raw ? JSON.parse(raw) : null; |
158 | | - } catch { |
159 | | - return null; |
160 | | - } |
161 | | - } |
162 | | - |
163 | | - function selectedLocalSessionKey() { |
164 | | - try { |
165 | | - return window.localStorage.getItem(mockDbSessionStorageKey) || ""; |
166 | | - } catch { |
167 | | - return ""; |
168 | | - } |
169 | | - } |
170 | | - |
171 | | - function selectedSessionModeId() { |
172 | | - try { |
173 | | - return window.localStorage.getItem(mockDbSessionModeStorageKey) || "local"; |
174 | | - } catch { |
175 | | - return "local"; |
176 | | - } |
177 | | - } |
178 | | - |
179 | | - function rolesForUser(state, userKey) { |
180 | | - const rows = state?.tables?.user_roles || []; |
181 | | - const roles = new Map((state?.tables?.roles || []).map(function (role) { |
182 | | - return [role.key, role.roleSlug || role.name]; |
183 | | - })); |
184 | | - return rows |
185 | | - .filter(function (row) { |
186 | | - return row.userKey === userKey && roles.has(row.roleKey); |
187 | | - }) |
188 | | - .map(function (row) { |
189 | | - return roles.get(row.roleKey); |
190 | | - }) |
191 | | - .filter(Boolean); |
192 | | - } |
193 | | - |
194 | 151 | function localDevLoginState() { |
195 | | - if (selectedSessionModeId() === "dev") { |
196 | | - return { |
197 | | - authenticated: false, |
198 | | - diagnostic: "DEV mode uses read-only/demo JSON access. Switch to Local to use persisted Memory DB sessions.", |
199 | | - displayName: "Login", |
200 | | - mode: "dev", |
201 | | - roleSlugs: [] |
202 | | - }; |
203 | | - } |
204 | | - |
205 | | - const sessionUserKey = selectedLocalSessionKey(); |
206 | | - if (!sessionUserKey) { |
207 | | - return { |
208 | | - authenticated: false, |
209 | | - diagnostic: "", |
210 | | - displayName: "Login", |
211 | | - mode: "local", |
212 | | - roleSlugs: [] |
213 | | - }; |
214 | | - } |
215 | | - |
216 | | - const state = readMockDbState(); |
217 | | - if (!state) { |
218 | | - return { |
219 | | - authenticated: false, |
220 | | - diagnostic: "Persisted Memory DB users and roles are not seeded. Open Login and choose Local to seed local users.", |
221 | | - displayName: "Login", |
222 | | - mode: "local", |
223 | | - roleSlugs: [] |
224 | | - }; |
225 | | - } |
226 | | - |
227 | | - const user = (state?.tables?.users || []).find(function (record) { |
228 | | - return record.key === sessionUserKey && record.isActive !== false; |
229 | | - }); |
230 | | - if (!user) { |
| 152 | + try { |
| 153 | + const request = new XMLHttpRequest(); |
| 154 | + request.open("GET", "/api/session/current", false); |
| 155 | + request.setRequestHeader("Accept", "application/json"); |
| 156 | + request.send(null); |
| 157 | + const payload = request.responseText ? JSON.parse(request.responseText) : null; |
| 158 | + if (request.status < 200 || request.status >= 300 || payload?.ok === false) { |
| 159 | + throw new Error(payload?.error || "Session API did not return a valid current session."); |
| 160 | + } |
| 161 | + const session = payload?.data || {}; |
231 | 162 | return { |
232 | | - authenticated: false, |
233 | | - diagnostic: `Selected Local user key ${sessionUserKey} is missing from persisted Memory DB users.`, |
234 | | - displayName: "Login", |
235 | | - mode: "local", |
236 | | - roleSlugs: [] |
| 163 | + authenticated: Boolean(session.authenticated), |
| 164 | + diagnostic: session.diagnostic || "", |
| 165 | + displayName: session.authenticated ? session.displayName || session.label || "Account" : "Login", |
| 166 | + mode: session.mode || "local", |
| 167 | + roleSlugs: Array.isArray(session.roleSlugs) ? session.roleSlugs : [] |
237 | 168 | }; |
238 | | - } |
239 | | - |
240 | | - const roleSlugs = rolesForUser(state, sessionUserKey); |
241 | | - if (!roleSlugs.length) { |
| 169 | + } catch (error) { |
242 | 170 | return { |
243 | 171 | authenticated: false, |
244 | | - diagnostic: `Selected Local user ${user.displayName || sessionUserKey} has no persisted Memory DB roles.`, |
| 172 | + diagnostic: "Server session API is unavailable. Start the Local/DEV server API before using protected pages.", |
245 | 173 | displayName: "Login", |
246 | | - mode: "local", |
| 174 | + mode: "missing-api", |
247 | 175 | roleSlugs: [] |
248 | 176 | }; |
249 | 177 | } |
250 | | - |
251 | | - return { |
252 | | - authenticated: true, |
253 | | - diagnostic: "", |
254 | | - displayName: user.displayName || sessionUserKey, |
255 | | - mode: "local", |
256 | | - roleSlugs |
257 | | - }; |
258 | 178 | } |
259 | 179 |
|
260 | 180 | function directSubMenu(navItem) { |
|
438 | 358 | function logoutCurrentSession(event) { |
439 | 359 | event.preventDefault(); |
440 | 360 | try { |
441 | | - window.localStorage.removeItem(mockDbSessionStorageKey); |
| 361 | + const request = new XMLHttpRequest(); |
| 362 | + request.open("POST", "/api/session/logout", false); |
| 363 | + request.setRequestHeader("Accept", "application/json"); |
| 364 | + request.send(null); |
442 | 365 | window.dispatchEvent(new CustomEvent("gamefoundry:mock-db-session-user-changed", { |
443 | 366 | detail: { |
444 | 367 | authenticated: false, |
|
0 commit comments