thibaud frere
commited on
Commit
·
1565c58
1
Parent(s):
ef49be8
test hf pro user detector
Browse files- app/src/pages/index.astro +65 -0
app/src/pages/index.astro
CHANGED
|
@@ -208,6 +208,71 @@ const licence = (articleFM as any)?.licence ?? (articleFM as any)?.license ?? (a
|
|
| 208 |
});
|
| 209 |
</script>
|
| 210 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 211 |
|
| 212 |
</body>
|
| 213 |
</html>
|
|
|
|
| 208 |
});
|
| 209 |
</script>
|
| 210 |
|
| 211 |
+
<script is:inline>
|
| 212 |
+
// Minimal HF viewer badge: detect if signed in and Pro status (client-side only)
|
| 213 |
+
(async () => {
|
| 214 |
+
const showBadge = (text) => {
|
| 215 |
+
try {
|
| 216 |
+
const el = document.createElement('div');
|
| 217 |
+
el.id = 'hf-viewer-status';
|
| 218 |
+
el.style.position = 'fixed';
|
| 219 |
+
el.style.top = '0.75rem';
|
| 220 |
+
el.style.right = '0.75rem';
|
| 221 |
+
el.style.zIndex = '9999';
|
| 222 |
+
el.style.fontFamily = 'system-ui, -apple-system, Segoe UI, Roboto, sans-serif';
|
| 223 |
+
el.style.fontSize = '12px';
|
| 224 |
+
el.style.lineHeight = '1.2';
|
| 225 |
+
el.style.background = 'rgba(0, 0, 0, 0.6)';
|
| 226 |
+
el.style.color = '#fff';
|
| 227 |
+
el.style.padding = '6px 8px';
|
| 228 |
+
el.style.borderRadius = '6px';
|
| 229 |
+
el.textContent = text;
|
| 230 |
+
document.body.appendChild(el);
|
| 231 |
+
} catch {}
|
| 232 |
+
};
|
| 233 |
+
try {
|
| 234 |
+
const isHFHost = (() => {
|
| 235 |
+
try {
|
| 236 |
+
const h = location.hostname;
|
| 237 |
+
return (
|
| 238 |
+
h === 'huggingface.co' ||
|
| 239 |
+
h.endsWith('.huggingface.co') ||
|
| 240 |
+
h === 'hf.space' ||
|
| 241 |
+
h.endsWith('.hf.space')
|
| 242 |
+
);
|
| 243 |
+
} catch { return false; }
|
| 244 |
+
})();
|
| 245 |
+
|
| 246 |
+
if (!isHFHost) {
|
| 247 |
+
showBadge('HF: status only on huggingface.co/Spaces');
|
| 248 |
+
return;
|
| 249 |
+
}
|
| 250 |
+
|
| 251 |
+
const res = await fetch('https://huggingface.co/api/whoami-v2', { credentials: 'include' });
|
| 252 |
+
if (!res.ok) {
|
| 253 |
+
showBadge('HF: not logged in');
|
| 254 |
+
return;
|
| 255 |
+
}
|
| 256 |
+
const data = await res.json();
|
| 257 |
+
const username = data?.name || data?.username || data?.user || '';
|
| 258 |
+
let isPro = (data && (data.is_pro ?? data.isPro));
|
| 259 |
+
if ((isPro === undefined || isPro === null) && username) {
|
| 260 |
+
try {
|
| 261 |
+
const r2 = await fetch(`https://huggingface.co/api/users/${encodeURIComponent(username)}`, { credentials: 'include' });
|
| 262 |
+
if (r2.ok) {
|
| 263 |
+
const u = await r2.json();
|
| 264 |
+
isPro = u?.is_pro ?? u?.isPro ?? false;
|
| 265 |
+
}
|
| 266 |
+
} catch {}
|
| 267 |
+
}
|
| 268 |
+
showBadge(`HF: ${username || 'signed in'}${isPro ? ' (Pro)' : ''}`);
|
| 269 |
+
} catch {
|
| 270 |
+
// On error (CORS, network, blocked 3rd-party cookies), still show a fallback badge
|
| 271 |
+
showBadge('HF: status unavailable');
|
| 272 |
+
}
|
| 273 |
+
})();
|
| 274 |
+
</script>
|
| 275 |
+
|
| 276 |
|
| 277 |
</body>
|
| 278 |
</html>
|