Something went wrong. Try again.
a simple but full-featured web player for OpenSubsonic music servers (Navidrome, Gonic, Ampache, etc) devins.page/tinysub
Something went wrong. Try again.
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274<script lang="ts"> import { tick } from "svelte"; import { slide } from "svelte/transition"; import { auth, getSessionCredentials, login } from "../api/auth.svelte.js"; import { asArray } from "../api/client.svelte.js"; import { anonymousGetExtensions, anonymousPing, deploymentConfig } from "../api/connection.js";
import { t } from "../languages/translate.svelte.js"; import { slideMotion } from "../ui/motion.svelte.js"; import Modal from "./Modal.svelte";
const saved = getSessionCredentials();
let server = $state(saved?.server || deploymentConfig.server), authMode = $state<"auto" | "apikey">(saved?.authMode === "apikey" ? "apikey" : "auto"), user = $state(saved?.username || deploymentConfig.username), password = $state(saved?.password || saved?.apiKey || deploymentConfig.password);
let pingStatus = $state<"none" | "loading" | "ok" | "fail">("none"); let serverLabel = $state(""); let isApiKeySupported = $state(saved?.authMode === "apikey"); const passwordLabel = $derived( authMode === "apikey" ? t("auth.apikey") : t("auth.password_placeholder"), ); const projectUrl = "https://tangled.org/devins.page/tinysub"; const footerLinks: Record<string, [label: string, href: string]> = { "{tangledLink}": ["Tangled", projectUrl], "{authorLink}": ["devins.page", "https://devins.page"], }; const footerParts = $derived(t("auth.footer").split(/(\{tangledLink\}|\{authorLink\})/));
$effect(() => { isApiKeySupported = false; if (!server.trim()) { pingStatus = "none"; authMode = "auto"; return; }
pingStatus = "loading";
const requestServer = server; const timer = setTimeout(async () => { try { const { status, type, serverVersion, openSubsonic } = (await anonymousPing(requestServer)) ?? {}; if (server !== requestServer) return; const isSubsonic = status === "ok" || status === "failed"; const name = typeof type === "string" ? type.trim() : ""; const version = typeof serverVersion === "string" ? serverVersion.trim() : ""; serverLabel = name && version ? `${name} - ${version}` : name; pingStatus = isSubsonic ? "ok" : "fail";
if (isSubsonic && openSubsonic) { try { const extRes = await anonymousGetExtensions(requestServer); if (server !== requestServer) return; isApiKeySupported = asArray( extRes.openSubsonicExtensions?.extension || extRes.openSubsonicExtensions, ).some((e: { name?: string }) => e?.name?.toLowerCase() === "apikeyauthentication"); } catch (err) { console.warn("failed to fetch extensions anonymously:", err); } } } catch { if (server !== requestServer) return; pingStatus = "fail"; }
if (!isApiKeySupported && authMode === "apikey") { authMode = "auto"; } }, 500);
return () => clearTimeout(timer); });</script>
<Modal open={!auth.loggedIn && !auth.autoLogin && !auth.authFailed} title={t("auth.login")} closable={false} style="inline-size: 30rem"> <section class="intro" class:branded={deploymentConfig.branding}> {#if deploymentConfig.branding} <header> <img src={`${import.meta.env.BASE_URL}favicon.svg`} alt="" /> <h2>tinysub</h2> </header> {/if} {#if !deploymentConfig.branding || !auth.busy} <p transition:slide={slideMotion()}> {deploymentConfig.branding ? t("auth.welcome_server") : t("auth.welcome")} </p> {/if} </section> <form class="auth-form" onsubmit={async (e) => { e.preventDefault(); const form = e.currentTarget; if (auth.busy || (await login(server, user, password, authMode))) return; await tick(); form.querySelector<HTMLInputElement>("#auth-password")?.focus({ preventScroll: true }); }} > {#if !auth.busy} <div class="auth-fields" transition:slide={slideMotion()}> {#if !deploymentConfig.serverLock} <input id="auth-server" name="server" bind:value={server} aria-label={t("auth.server_placeholder")} placeholder={t("auth.server_placeholder")} required autocomplete="url" /> {/if}
{#if pingStatus !== "none"} <div class:ok={pingStatus === "ok"} class:fail={pingStatus === "fail"} transition:slide={slideMotion()} > {#if pingStatus === "loading"} ... {:else if pingStatus === "ok"} <bdi>{serverLabel || t("auth.server_found")}</bdi> {:else} {t("auth.server_not_found")} - <a target="_blank" href={`${projectUrl}#i-cant-log-in`}>{t("auth.login_notice")} </a> {/if} </div> {/if}
{#if isApiKeySupported} <select id="auth-mode" name="authMode" bind:value={authMode} aria-label={t("auth.method")} > <option value="auto">{t("auth.username_password")}</option> <option value="apikey">{t("auth.apikey")}</option> </select> {/if}
{#if authMode !== "apikey"} <input id="auth-username" name="username" bind:value={user} aria-label={t("auth.username_placeholder")} placeholder={t("auth.username_placeholder")} required autocomplete="username" /> {/if} <input id="auth-password" name="password" bind:value={password} type="password" aria-label={passwordLabel} placeholder={passwordLabel} required autocomplete={authMode === "apikey" ? "off" : "current-password"} /> {#if auth.err} <div class="fail">{auth.err}</div> {/if} </div> {/if} <button class="native" type="submit" disabled={auth.busy}> {auth.busy ? t("auth.logging_in") : t("auth.login")} </button> </form> {#if deploymentConfig.branding} <footer> {#each footerParts as part, i (i)} {@const link = footerLinks[part]} {#if link} <a href={link[1]} target="_blank" rel="noopener">{link[0]}</a> {:else} {part} {/if} {/each} </footer> {/if}</Modal>
<style> .intro, footer { padding: 1rem; background-color: var(--layer); text-wrap: pretty; } .intro { border-block-end: var(--border-subtle);
&.branded { text-align: center; isolation: isolate;
p { padding-block-start: 1rem; } } header { position: relative; display: flex; justify-content: center; align-items: center; gap: 0.5rem;
&::after { content: ""; position: absolute; inset-inline: -12.5%; inset-block-start: 100%; block-size: 125%; transform: translateY(-75%); background: radial-gradient(ellipse, currentColor, transparent 70%); opacity: 0.125; pointer-events: none; z-index: -1; } } h2 { font-size: 2em; font-weight: 500; font-stretch: expanded; line-height: 1.25; } img { flex: none; inline-size: 4rem; } :is(h2, p) { margin: 0; } } footer { text-align: center; border-block-start: var(--border-subtle); } .auth-form { padding: 1rem; gap: 0; } .auth-fields { display: flex; flex-direction: column; gap: 0.5rem; padding-block-end: 0.5rem;
> div { font-size: smaller; overflow-wrap: anywhere; } } .ok { color: green; } .fail { color: red; }</style>