|
7 | 7 | import { getMetadata } from '../../scripts/aem.js'; |
8 | 8 | import { loadFragment } from '../fragment/fragment.js'; |
9 | 9 | import { getBlockContext } from '../../scripts/shared.js'; |
| 10 | +import { |
| 11 | + getLoginUrl, |
| 12 | + getLogoutUrl, |
| 13 | + getDefaultAuthLabel, |
| 14 | + getSessionState, |
| 15 | +} from '../../scripts/shared/auth-api.js'; |
10 | 16 |
|
11 | 17 | const DESKTOP = window.matchMedia('(min-width: 900px)'); |
12 | 18 | const THEME_KEY = 'diyfire-theme'; |
@@ -231,6 +237,99 @@ function getCookie(name) { |
231 | 237 | return decodeURIComponent(cookie.split('=').slice(1).join('=')); |
232 | 238 | } |
233 | 239 |
|
| 240 | +function hasCookieStartingWith(prefix) { |
| 241 | + return document.cookie |
| 242 | + .split(';') |
| 243 | + .map((entry) => decodeURIComponent(entry.split('=')[0] || '').trim()) |
| 244 | + .some((cookieName) => cookieName.startsWith(prefix)); |
| 245 | +} |
| 246 | + |
| 247 | +function isLoggedIn() { |
| 248 | + return hasCookieStartingWith('CF_Authorization'); |
| 249 | +} |
| 250 | + |
| 251 | +async function resolveAuthState() { |
| 252 | + try { |
| 253 | + const session = await getSessionState(); |
| 254 | + return { |
| 255 | + authenticated: Boolean(session?.authenticated), |
| 256 | + email: session?.email || '', |
| 257 | + }; |
| 258 | + } catch (e) { |
| 259 | + return { |
| 260 | + authenticated: isLoggedIn(), |
| 261 | + email: '', |
| 262 | + }; |
| 263 | + } |
| 264 | +} |
| 265 | + |
| 266 | +function setAuthUserInfo(link, email) { |
| 267 | + link.querySelector('.nav-auth-info')?.remove(); |
| 268 | + link.removeAttribute('title'); |
| 269 | + link.removeAttribute('data-auth-email'); |
| 270 | + |
| 271 | + if (!email) return; |
| 272 | + |
| 273 | + link.dataset.authEmail = email; |
| 274 | + link.setAttribute('title', email); |
| 275 | + const info = document.createElement('span'); |
| 276 | + info.className = 'nav-auth-info'; |
| 277 | + info.setAttribute('aria-hidden', 'true'); |
| 278 | + info.setAttribute('title', email); |
| 279 | + info.textContent = 'i'; |
| 280 | + link.append(info); |
| 281 | +} |
| 282 | + |
| 283 | +async function initAuth(nav, tools) { |
| 284 | + const loginLabel = getDefaultAuthLabel('login'); |
| 285 | + const logoutLabel = getDefaultAuthLabel('logout'); |
| 286 | + |
| 287 | + const loginCandidate = tools.querySelector('a[href*="login" i], a[data-auth-link]'); |
| 288 | + const shouldCreateLink = !loginCandidate; |
| 289 | + |
| 290 | + const desktopLink = loginCandidate || document.createElement('a'); |
| 291 | + if (shouldCreateLink) { |
| 292 | + desktopLink.href = getLoginUrl(); |
| 293 | + desktopLink.className = 'button nav-auth-link nav-auth-desktop'; |
| 294 | + tools.append(desktopLink); |
| 295 | + } |
| 296 | + |
| 297 | + desktopLink.dataset.authLink = 'true'; |
| 298 | + if (!desktopLink.classList.contains('button')) desktopLink.classList.add('button'); |
| 299 | + desktopLink.classList.add('nav-auth-link', 'nav-auth-desktop'); |
| 300 | + |
| 301 | + let mobileLink = nav.querySelector('.nav-auth-mobile-item a'); |
| 302 | + if (!mobileLink) { |
| 303 | + const mobileList = nav.querySelector('.nav-sections .default-content-wrapper > ul'); |
| 304 | + if (mobileList) { |
| 305 | + const li = document.createElement('li'); |
| 306 | + li.className = 'nav-auth-mobile-item'; |
| 307 | + const p = document.createElement('p'); |
| 308 | + mobileLink = document.createElement('a'); |
| 309 | + mobileLink.className = 'button nav-auth-link nav-auth-mobile'; |
| 310 | + mobileLink.dataset.authLink = 'true'; |
| 311 | + p.append(mobileLink); |
| 312 | + li.append(p); |
| 313 | + mobileList.append(li); |
| 314 | + } |
| 315 | + } |
| 316 | + if (mobileLink && !mobileLink.classList.contains('button')) mobileLink.classList.add('button'); |
| 317 | + |
| 318 | + const authState = await resolveAuthState(); |
| 319 | + const loggedIn = authState.authenticated; |
| 320 | + const loginHref = getLoginUrl(); |
| 321 | + const logoutHref = getLogoutUrl(); |
| 322 | + const targetHref = loggedIn ? logoutHref : loginHref; |
| 323 | + const label = loggedIn ? logoutLabel : loginLabel; |
| 324 | + |
| 325 | + [desktopLink, mobileLink].filter(Boolean).forEach((link) => { |
| 326 | + link.setAttribute('href', targetHref); |
| 327 | + link.textContent = label; |
| 328 | + link.setAttribute('aria-label', label); |
| 329 | + setAuthUserInfo(link, loggedIn ? authState.email : ''); |
| 330 | + }); |
| 331 | +} |
| 332 | + |
234 | 333 | function ensureGoogleTranslateScript() { |
235 | 334 | if (window.__googleTranslateScriptLoaded) return Promise.resolve(); |
236 | 335 | if (window.__googleTranslateScriptPromise) return window.__googleTranslateScriptPromise; |
@@ -437,6 +536,7 @@ export default async function decorate(block) { |
437 | 536 | if (tools) { |
438 | 537 | initTheme(tools); |
439 | 538 | initSearch(tools); |
| 539 | + await initAuth(nav, tools); |
440 | 540 | initLanguage(tools, eventRoot); |
441 | 541 | hydrateTranslateFromCookie(); |
442 | 542 | } |
|
0 commit comments