apolinario commited on
Commit
487bfc6
·
1 Parent(s): 774d848

Persist OAuth session across page reloads

Browse files
Files changed (1) hide show
  1. index.html +33 -4
index.html CHANGED
@@ -1222,21 +1222,50 @@ window.saveVoice = async function() {
1222
  // ── OAuth ──
1223
 
1224
  async function initOAuth() {
 
1225
  const result = await oauthHandleRedirectIfPresent();
1226
  if (result) {
1227
  accessToken = result.accessToken;
1228
  userInfo = result.userInfo;
1229
- // Check Pro status
 
 
 
 
 
 
 
 
 
 
 
 
1230
  try {
1231
  const whoami = await fetch("https://huggingface.co/api/whoami-v2", {
1232
  headers: { Authorization: `Bearer ${accessToken}` }
1233
  }).then(r => r.json());
1234
- isPro = !!whoami.isPro;
1235
- } catch { isPro = false; }
1236
- showApp();
 
 
 
 
 
 
1237
  }
1238
  }
1239
 
 
 
 
 
 
 
 
 
 
 
1240
  window.login = async function() {
1241
  window.location.href = await oauthLoginUrl({ scopes: "openid profile read-billing" });
1242
  };
 
1222
  // ── OAuth ──
1223
 
1224
  async function initOAuth() {
1225
+ // Try restoring from OAuth redirect first
1226
  const result = await oauthHandleRedirectIfPresent();
1227
  if (result) {
1228
  accessToken = result.accessToken;
1229
  userInfo = result.userInfo;
1230
+ // Persist for page reloads
1231
+ sessionStorage.setItem("zl_token", accessToken);
1232
+ sessionStorage.setItem("zl_user", JSON.stringify(userInfo));
1233
+ await checkProAndShow();
1234
+ return;
1235
+ }
1236
+ // Try restoring from sessionStorage
1237
+ const savedToken = sessionStorage.getItem("zl_token");
1238
+ const savedUser = sessionStorage.getItem("zl_user");
1239
+ if (savedToken && savedUser) {
1240
+ accessToken = savedToken;
1241
+ userInfo = JSON.parse(savedUser);
1242
+ // Verify token is still valid
1243
  try {
1244
  const whoami = await fetch("https://huggingface.co/api/whoami-v2", {
1245
  headers: { Authorization: `Bearer ${accessToken}` }
1246
  }).then(r => r.json());
1247
+ if (whoami.name) {
1248
+ isPro = !!whoami.isPro;
1249
+ showApp();
1250
+ return;
1251
+ }
1252
+ } catch {}
1253
+ // Token expired, clear it
1254
+ sessionStorage.removeItem("zl_token");
1255
+ sessionStorage.removeItem("zl_user");
1256
  }
1257
  }
1258
 
1259
+ async function checkProAndShow() {
1260
+ try {
1261
+ const whoami = await fetch("https://huggingface.co/api/whoami-v2", {
1262
+ headers: { Authorization: `Bearer ${accessToken}` }
1263
+ }).then(r => r.json());
1264
+ isPro = !!whoami.isPro;
1265
+ } catch { isPro = false; }
1266
+ showApp();
1267
+ }
1268
+
1269
  window.login = async function() {
1270
  window.location.href = await oauthLoginUrl({ scopes: "openid profile read-billing" });
1271
  };