Spaces:
Running
Running
Download auth.js from 7dsolv/Konaet-XRay-Lab: direct link, hf CLI and curl.
- Browser
- Download file 3.26 kB
-
https://huggingface.co/spaces/7dsolv/Konaet-XRay-Lab/resolve/main/auth.js
- Command line
-
hf download hf://spaces/7dsolv/Konaet-XRay-Lab/auth.js
-
curl -L -o auth.js https://huggingface.co/spaces/7dsolv/Konaet-XRay-Lab/resolve/main/auth.js
3.26 kB
| import { oauthLoginUrl, oauthHandleRedirectIfPresent } from '@huggingface/hub'; | |
| const guest = document.querySelector('#account-guest'); | |
| const user = document.querySelector('#account-user'); | |
| const error = document.querySelector('#account-error'); | |
| const cleanRedirect = `${location.origin}${location.pathname}`; | |
| function storedOauth() { | |
| try { | |
| const value = JSON.parse(localStorage.getItem('konaet-xray-oauth')); | |
| if (value?.accessTokenExpiresAt && new Date(value.accessTokenExpiresAt) <= new Date()) { | |
| localStorage.removeItem('konaet-xray-oauth'); | |
| return null; | |
| } | |
| return value; | |
| } catch { | |
| localStorage.removeItem('konaet-xray-oauth'); | |
| return null; | |
| } | |
| } | |
| function showAccount(oauth) { | |
| const info = oauth?.userInfo; | |
| if (!info) { | |
| guest.hidden = false; | |
| user.hidden = true; | |
| return false; | |
| } | |
| document.querySelector('#account-name').textContent = info.preferred_username || info.name || 'Conta Hugging Face'; | |
| const avatar = document.querySelector('#account-avatar'); | |
| avatar.src = info.picture || ''; | |
| avatar.hidden = !info.picture; | |
| guest.hidden = true; | |
| user.hidden = false; | |
| error.hidden = true; | |
| window.dispatchEvent(new Event('konaet-account-change')); | |
| return true; | |
| } | |
| async function beginLogin() { | |
| error.hidden = true; | |
| if (window.self !== window.top) { | |
| window.open(`${cleanRedirect}?login=1`, '_blank', 'noopener'); | |
| document.querySelector('#sign-in').textContent = 'Continuar na nova aba'; | |
| return; | |
| } | |
| document.querySelector('#sign-in').textContent = 'Abrindo…'; | |
| const url = await oauthLoginUrl({ | |
| scopes: window.huggingface?.variables?.OAUTH_SCOPES, | |
| redirectUrl: cleanRedirect, | |
| }); | |
| location.assign(`${url}&prompt=consent`); | |
| } | |
| let oauth = storedOauth(); | |
| const search = new URLSearchParams(location.search); | |
| const callback = search.has('code') || search.has('error'); | |
| if (callback) { | |
| try { | |
| const returned = await oauthHandleRedirectIfPresent(); | |
| if (returned) { | |
| oauth = returned; | |
| localStorage.setItem('konaet-xray-oauth', JSON.stringify(returned)); | |
| } | |
| } catch { | |
| if (!oauth) { | |
| error.textContent = 'A autorização não terminou. Clique em Entrar para tentar novamente.'; | |
| error.hidden = false; | |
| } | |
| } finally { | |
| history.replaceState({}, '', location.pathname + location.hash); | |
| } | |
| } | |
| showAccount(oauth); | |
| if (search.has('login') && !oauth) { | |
| history.replaceState({}, '', cleanRedirect); | |
| beginLogin().catch(() => { | |
| document.querySelector('#sign-in').textContent = 'Entrar'; | |
| error.textContent = 'Login indisponível neste momento.'; | |
| error.hidden = false; | |
| }); | |
| } | |
| document.querySelector('#sign-in').addEventListener('click', () => beginLogin().catch(() => { | |
| document.querySelector('#sign-in').textContent = 'Entrar'; | |
| error.textContent = 'Login indisponível neste momento.'; | |
| error.hidden = false; | |
| })); | |
| window.addEventListener('konaet-login-required', () => beginLogin().catch(() => { | |
| document.querySelector('#sign-in').textContent = 'Entrar'; | |
| error.textContent = 'Login indisponível neste momento.'; | |
| error.hidden = false; | |
| })); | |
| document.querySelector('#sign-out').addEventListener('click', () => { | |
| localStorage.removeItem('konaet-xray-oauth'); | |
| location.replace(cleanRedirect); | |
| }); | |