A reset ended every session but left the API key and Subsonic password working, and anyone signed in to the account can make either. So whoever had the account kept it through a Subsonic app after the owner reset the password. The forgotten-password reset and an admin's reset now replace the API key with one nobody holds and clear the Subsonic password. A self-service change keeps Subsonic apps signed in by default, since it would sign out the owner's own apps too. The web and Android password forms gain "Sign out Subsonic apps too", sent as sign_out_subsonic. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
128 lines
4.4 KiB
TypeScript
128 lines
4.4 KiB
TypeScript
import { createQuery } from '@tanstack/svelte-query';
|
|
import { api } from './client';
|
|
import { qk } from './queries';
|
|
|
|
export type SystemPlaylistsStatus = {
|
|
in_flight: boolean;
|
|
last_run_at: string | null;
|
|
last_error: string | null;
|
|
};
|
|
|
|
export async function getSystemPlaylistsStatus(): Promise<SystemPlaylistsStatus> {
|
|
return api.get<SystemPlaylistsStatus>('/api/me/system-playlists-status');
|
|
}
|
|
|
|
// Refresh aggressively while a build is in_flight so the building → built
|
|
// transition appears live on the home page. Stale-time is short; manual
|
|
// invalidations on the server's POST endpoints are out of scope.
|
|
export function createSystemPlaylistsStatusQuery() {
|
|
return createQuery({
|
|
queryKey: qk.systemPlaylistsStatus(),
|
|
queryFn: getSystemPlaylistsStatus,
|
|
staleTime: 10_000
|
|
});
|
|
}
|
|
|
|
// Self-service profile / password / API-token endpoints (U3) ---------------
|
|
|
|
export type MyProfile = {
|
|
id: string;
|
|
username: string;
|
|
display_name: string | null;
|
|
email: string | null;
|
|
is_admin: boolean;
|
|
};
|
|
|
|
export type APITokenResponse = { api_token: string };
|
|
|
|
// signOutSubsonic also ends every Subsonic app's access (the API token and
|
|
// the Subsonic password stop working), for a password someone else may know.
|
|
export async function changePassword(
|
|
currentPassword: string,
|
|
newPassword: string,
|
|
signOutSubsonic = false,
|
|
): Promise<void> {
|
|
await api.put('/api/me/password', {
|
|
current_password: currentPassword,
|
|
new_password: newPassword,
|
|
sign_out_subsonic: signOutSubsonic,
|
|
});
|
|
}
|
|
|
|
// The signed-in user's profile, to fill the Account form with what is
|
|
// stored: a form that starts blank sends blanks, and a blank clears.
|
|
export async function getMyProfile(): Promise<MyProfile> {
|
|
return api.get<MyProfile>('/api/me');
|
|
}
|
|
|
|
export async function updateProfile(input: { display_name?: string; email?: string }): Promise<MyProfile> {
|
|
return api.put<MyProfile>('/api/me/profile', input);
|
|
}
|
|
|
|
/**
|
|
* Mints a new API key and returns it. The server keeps only its hash, so
|
|
* this is the one time the key can be read; there is no way to fetch it
|
|
* again later.
|
|
*/
|
|
export async function regenerateAPIToken(): Promise<APITokenResponse> {
|
|
return api.post<APITokenResponse>('/api/me/api-token', {});
|
|
}
|
|
|
|
// Subsonic password ------------------------------------------------
|
|
// For Subsonic clients that only sign in with a username and password (the
|
|
// t/s scheme). The server generates it, so it is never the login password;
|
|
// like the API key it is shown once, when generated.
|
|
|
|
export type SubsonicPasswordStatus = { enabled: boolean };
|
|
|
|
export async function getSubsonicPasswordStatus(): Promise<SubsonicPasswordStatus> {
|
|
return api.get<SubsonicPasswordStatus>('/api/me/subsonic-password');
|
|
}
|
|
|
|
export async function generateSubsonicPassword(): Promise<{ password: string }> {
|
|
return api.post<{ password: string }>('/api/me/subsonic-password', {});
|
|
}
|
|
|
|
export async function clearSubsonicPassword(): Promise<void> {
|
|
await api.del('/api/me/subsonic-password');
|
|
}
|
|
|
|
// Submits the browser's current IANA timezone for the authenticated
|
|
// user. Called from the auth store on login + bootstrap + once weekly
|
|
// (cadence tracked client-side in localStorage). Failures are
|
|
// non-fatal — the server's UTC fallback keeps the scheduler working
|
|
// until the next attempt.
|
|
export async function putMyTimezone(timezone: string): Promise<void> {
|
|
await api.put<void>('/api/me/timezone', { timezone });
|
|
}
|
|
|
|
// Active sessions ---------------------------------------------------
|
|
|
|
// created_ip is frozen at issue time; last_ip moves with the session. The
|
|
// pair is the signal — the same device string arriving from an address you
|
|
// don't recognise is what a stolen token looks like from the inside.
|
|
export type ActiveSession = {
|
|
id: string;
|
|
user_agent: string;
|
|
created_ip: string;
|
|
last_ip: string;
|
|
created_at: string;
|
|
last_seen_at: string;
|
|
current: boolean;
|
|
};
|
|
|
|
export async function listSessions(): Promise<ActiveSession[]> {
|
|
return api.get<ActiveSession[]>('/api/me/sessions');
|
|
}
|
|
|
|
export async function revokeSession(id: string): Promise<void> {
|
|
await api.del(`/api/me/sessions/${id}`);
|
|
}
|
|
|
|
// Returns how many were ended. The server excludes the caller's own session,
|
|
// so this never signs you out of the page you pressed it on.
|
|
export async function revokeOtherSessions(): Promise<number> {
|
|
const body = await api.post<{ revoked: number }>('/api/me/sessions/logout-others', {});
|
|
return body.revoked;
|
|
}
|