1// Safe Base64 for UTF-8 credentials 2function btoaUtf8(str) { 3 return btoa(unescape(encodeURIComponent(str))); 4} 5 6/** 7 * Logs in using Basic Auth and stores tokens in localStorage. 8 * @param {Object} opts 9 * @param {string} opts.username 10 * @param {string} opts.password 11 * @param {string} [opts.url='/backoffice/next/api/auth/login'] 12 * @returns {Promise<{authToken: string, refreshToken: string}>} 13 */ 14async function loginAndStoreTokens({ 15 username, 16 password, 17 url = "/backoffice/next/api/auth/token", 18}) { 19 if (!username || !password) { 20 throw new Error("Username and password are required."); 21 } 22 const accessTokenKey = "presscentricBackOffice.token"; 23 const refreshTokenKey = "presscentricBackOffice.refreshToken"; 24 const usernameKey = "presscentricBackOffice.username"; 25 26 const headers = new Headers(); 27 headers.set("Authorization", `Basic ${btoaUtf8(`${username}:${password}`)}`); 28 headers.set("Accept", "application/json"); 29 headers.set("Content-Type", "application/json"); 30 31 const response = await fetch(url, { 32 method: "POST", 33 headers, 34 // Set to 'omit' if your API doesnât need cookies. Keep as-is if it does. 35 credentials: "omit", 36 body: "{}", 37 }); 38 39 // Helpful error detail for debugging (won't expose stack traces) 40 if (!response.ok) { 41 const text = await response.text().catch(() => ""); 42 throw new Error( 43 `Login failed (${response.status}): ${text || response.statusText}` 44 ); 45 } 46 47 const data = await response.json(); 48 49 const authToken = data.accessToken; 50 const refreshToken = data.refreshToken; 51 52 if (!authToken || !refreshToken) { 53 throw new Error("Missing tokens in response payload."); 54 } 55 56 localStorage.setItem(accessTokenKey, authToken); 57 localStorage.setItem(refreshTokenKey, refreshToken); 58 localStorage.setItem(usernameKey, username); 59 60 return { authToken, refreshToken }; 61}
Line numbers count LF bytes from the start of the resource, as the search results do. Vendor segments are library code the classifier recognised; they are stored but not indexed. Bytes are shown as Latin1 characters, one per byte.