zofiasmolenasana commited on
Commit
6606bb6
Β·
unverified Β·
1 Parent(s): 3195abb

Fix auth: no Content-Type on GET (mobile/HF); jsonAuthHeaders for POST; remove fetch monkey-patch

Browse files
Files changed (1) hide show
  1. static/index.html +22 -18
static/index.html CHANGED
@@ -324,17 +324,6 @@
324
  <script>
325
  (function(){
326
 
327
- /* Ensure cookies (session) are sent on /api/* β€” HF proxy may strip Authorization on GET. */
328
- const _nativeFetch = window.fetch;
329
- window.fetch = function(input, init) {
330
- init = init ? {...init} : {};
331
- const u = typeof input === "string" ? input : (input && input.url) || "";
332
- if (u.startsWith("/api") && init.credentials === undefined) {
333
- init.credentials = "include";
334
- }
335
- return _nativeFetch.call(window, input, init);
336
- };
337
-
338
  /* ── i18n ───────────────────────────────────────────────────────────── */
339
  const I18N = {
340
  en: {
@@ -897,7 +886,10 @@
897
  saveBtn.disabled = true; skipBtn.disabled = true;
898
  try {
899
  const username = usernameInput.value.trim() || "anonymous";
900
- const resp = await fetch(`/api/next_sheet?labeler=${encodeURIComponent(username)}`, {headers: authHeaders()});
 
 
 
901
  const data = await safeJson(resp);
902
  if (!resp.ok) throw new Error(data.detail || `HTTP ${resp.status}`);
903
  if (data.done) {
@@ -1484,7 +1476,8 @@
1484
  try {
1485
  const resp = await fetch("/api/skip", {
1486
  method: "POST",
1487
- headers: authHeaders(),
 
1488
  body: JSON.stringify({
1489
  drive_file_id: currentSheet.drive_file_id || "",
1490
  meta_row_index: currentSheet.meta_row_index || 0,
@@ -1584,7 +1577,8 @@
1584
  try {
1585
  const resp = await fetch("/api/save_and_next", {
1586
  method: "POST",
1587
- headers: authHeaders(),
 
1588
  body: JSON.stringify({
1589
  spreadsheet_id: currentSheet.spreadsheet_id,
1590
  sheet_name: currentSheet.sheet_name,
@@ -1818,7 +1812,8 @@
1818
  try {
1819
  const resp = await fetch("/api/rag_label/save", {
1820
  method: "POST",
1821
- headers: authHeaders(),
 
1822
  body: JSON.stringify({
1823
  sheet_id: currentSheet.rag_sheet_id,
1824
  spreadsheet_id: currentSheet.spreadsheet_id,
@@ -1975,7 +1970,8 @@
1975
  try {
1976
  const resp = await fetch("/api/save_review", {
1977
  method: "POST",
1978
- headers: authHeaders(),
 
1979
  body: JSON.stringify({
1980
  spreadsheet_id: currentSheet.spreadsheet_id,
1981
  sheet_name: currentSheet.sheet_name,
@@ -2011,12 +2007,19 @@
2011
 
2012
  let authToken = localStorage.getItem("labeler_token") || "";
2013
 
 
2014
  function authHeaders() {
2015
- const h = {"Content-Type": "application/json"};
2016
  if (authToken) h["Authorization"] = `Bearer ${authToken}`;
2017
  return h;
2018
  }
2019
 
 
 
 
 
 
 
2020
  async function safeJson(resp) {
2021
  const text = await resp.text();
2022
  try { return JSON.parse(text); }
@@ -2040,7 +2043,7 @@
2040
  }
2041
 
2042
  try {
2043
- const r = await fetch("/api/auth/check", {headers: authHeaders()});
2044
  const ct = r.headers.get("content-type") || "";
2045
  if (!r.ok || !ct.includes("application/json")) {
2046
  authToken = "";
@@ -2071,6 +2074,7 @@
2071
  const r = await fetch("/api/auth", {
2072
  method: "POST",
2073
  headers: {"Content-Type": "application/json"},
 
2074
  body: JSON.stringify({password: loginPwd.value}),
2075
  });
2076
  if (r.ok) {
 
324
  <script>
325
  (function(){
326
 
 
 
 
 
 
 
 
 
 
 
 
327
  /* ── i18n ───────────────────────────────────────────────────────────── */
328
  const I18N = {
329
  en: {
 
886
  saveBtn.disabled = true; skipBtn.disabled = true;
887
  try {
888
  const username = usernameInput.value.trim() || "anonymous";
889
+ const resp = await fetch(`/api/next_sheet?labeler=${encodeURIComponent(username)}`, {
890
+ headers: authHeaders(),
891
+ credentials: "include",
892
+ });
893
  const data = await safeJson(resp);
894
  if (!resp.ok) throw new Error(data.detail || `HTTP ${resp.status}`);
895
  if (data.done) {
 
1476
  try {
1477
  const resp = await fetch("/api/skip", {
1478
  method: "POST",
1479
+ headers: jsonAuthHeaders(),
1480
+ credentials: "include",
1481
  body: JSON.stringify({
1482
  drive_file_id: currentSheet.drive_file_id || "",
1483
  meta_row_index: currentSheet.meta_row_index || 0,
 
1577
  try {
1578
  const resp = await fetch("/api/save_and_next", {
1579
  method: "POST",
1580
+ headers: jsonAuthHeaders(),
1581
+ credentials: "include",
1582
  body: JSON.stringify({
1583
  spreadsheet_id: currentSheet.spreadsheet_id,
1584
  sheet_name: currentSheet.sheet_name,
 
1812
  try {
1813
  const resp = await fetch("/api/rag_label/save", {
1814
  method: "POST",
1815
+ headers: jsonAuthHeaders(),
1816
+ credentials: "include",
1817
  body: JSON.stringify({
1818
  sheet_id: currentSheet.rag_sheet_id,
1819
  spreadsheet_id: currentSheet.spreadsheet_id,
 
1970
  try {
1971
  const resp = await fetch("/api/save_review", {
1972
  method: "POST",
1973
+ headers: jsonAuthHeaders(),
1974
+ credentials: "include",
1975
  body: JSON.stringify({
1976
  spreadsheet_id: currentSheet.spreadsheet_id,
1977
  sheet_name: currentSheet.sheet_name,
 
2007
 
2008
  let authToken = localStorage.getItem("labeler_token") || "";
2009
 
2010
+ /** Only Authorization β€” never send ``Content-Type: application/json`` on GET (breaks some proxies / mobile). */
2011
  function authHeaders() {
2012
+ const h = {};
2013
  if (authToken) h["Authorization"] = `Bearer ${authToken}`;
2014
  return h;
2015
  }
2016
 
2017
+ function jsonAuthHeaders() {
2018
+ const h = authHeaders();
2019
+ h["Content-Type"] = "application/json";
2020
+ return h;
2021
+ }
2022
+
2023
  async function safeJson(resp) {
2024
  const text = await resp.text();
2025
  try { return JSON.parse(text); }
 
2043
  }
2044
 
2045
  try {
2046
+ const r = await fetch("/api/auth/check", {headers: authHeaders(), credentials: "include"});
2047
  const ct = r.headers.get("content-type") || "";
2048
  if (!r.ok || !ct.includes("application/json")) {
2049
  authToken = "";
 
2074
  const r = await fetch("/api/auth", {
2075
  method: "POST",
2076
  headers: {"Content-Type": "application/json"},
2077
+ credentials: "include",
2078
  body: JSON.stringify({password: loginPwd.value}),
2079
  });
2080
  if (r.ok) {