|
| 1 | +/** |
| 2 | + * Auth0 (auth.coderic.org) — Login/Logout en el tema Coderic. |
| 3 | + * Requiere: window.CodericAuth0Config = { domain, clientId, baseurl } y Auth0 SPA SDK cargado. |
| 4 | + */ |
| 5 | +(function () { |
| 6 | + "use strict"; |
| 7 | + |
| 8 | + var config = window.CodericAuth0Config; |
| 9 | + if (!config || !config.domain || !config.clientId) return; |
| 10 | + |
| 11 | + var container = document.getElementById("coderic-auth-buttons"); |
| 12 | + if (!container) return; |
| 13 | + |
| 14 | + var baseurl = (config.baseurl || "").replace(/\/$/, ""); |
| 15 | + var redirectUri = window.location.origin + baseurl + "/"; |
| 16 | + |
| 17 | + function renderLoggedOut() { |
| 18 | + container.innerHTML = |
| 19 | + '<button type="button" id="coderic-login-btn" class="px-4 py-2 text-sm font-medium text-white bg-orange-600 rounded-md hover:bg-orange-700 focus:outline-none focus:ring-2 focus:ring-orange-500">Iniciar sesión</button>'; |
| 20 | + document.getElementById("coderic-login-btn").addEventListener("click", function () { |
| 21 | + window.CodericAuth0Client.loginWithRedirect(); |
| 22 | + }); |
| 23 | + } |
| 24 | + |
| 25 | + function renderLoggedIn(user) { |
| 26 | + var name = (user && (user.name || user.nickname || user.email)) || "Usuario"; |
| 27 | + container.innerHTML = |
| 28 | + '<span class="text-sm text-gray-600 mr-2">' + |
| 29 | + escapeHtml(name) + |
| 30 | + '</span><button type="button" id="coderic-logout-btn" class="px-4 py-2 text-sm font-medium text-gray-700 bg-gray-100 rounded-md hover:bg-gray-200 focus:outline-none focus:ring-2 focus:ring-orange-500">Cerrar sesión</button>'; |
| 31 | + document.getElementById("coderic-logout-btn").addEventListener("click", function () { |
| 32 | + window.CodericAuth0Client.logout({ |
| 33 | + logoutParams: { returnTo: redirectUri }, |
| 34 | + }); |
| 35 | + }); |
| 36 | + } |
| 37 | + |
| 38 | + function escapeHtml(s) { |
| 39 | + var div = document.createElement("div"); |
| 40 | + div.textContent = s; |
| 41 | + return div.innerHTML; |
| 42 | + } |
| 43 | + |
| 44 | + function init() { |
| 45 | + if (typeof createAuth0Client === "undefined") { |
| 46 | + container.innerHTML = ""; |
| 47 | + return; |
| 48 | + } |
| 49 | + createAuth0Client({ |
| 50 | + domain: config.domain, |
| 51 | + clientId: config.clientId, |
| 52 | + authorizationParams: { |
| 53 | + redirect_uri: redirectUri, |
| 54 | + audience: config.audience || undefined, |
| 55 | + }, |
| 56 | + cacheLocation: "memory", |
| 57 | + }).then(function (client) { |
| 58 | + window.CodericAuth0Client = client; |
| 59 | + var q = new URLSearchParams(window.location.search); |
| 60 | + if (q.has("code") && q.has("state")) { |
| 61 | + client.handleRedirectCallback().then(function () { |
| 62 | + window.history.replaceState({}, document.title, baseurl + "/"); |
| 63 | + return client.getUser(); |
| 64 | + }).then(function (user) { |
| 65 | + renderLoggedIn(user); |
| 66 | + }).catch(function () { |
| 67 | + renderLoggedOut(); |
| 68 | + }); |
| 69 | + return; |
| 70 | + } |
| 71 | + client.isAuthenticated().then(function (ok) { |
| 72 | + if (ok) { |
| 73 | + client.getUser().then(renderLoggedIn); |
| 74 | + } else { |
| 75 | + renderLoggedOut(); |
| 76 | + } |
| 77 | + }); |
| 78 | + }).catch(function () { |
| 79 | + renderLoggedOut(); |
| 80 | + }); |
| 81 | + } |
| 82 | + |
| 83 | + if (document.readyState === "loading") { |
| 84 | + document.addEventListener("DOMContentLoaded", init); |
| 85 | + } else { |
| 86 | + init(); |
| 87 | + } |
| 88 | +})(); |
0 commit comments