feat: enhance authentication handling and UI feedback in SurfSense plugin

- Introduced an authentication block mechanism to prevent repeated invalid token submissions.
- Updated notification logic to provide clearer feedback on API token status.
- Refactored status visuals for better clarity and consistency in the user interface.
- Improved connection handling in the sync engine to ensure robust error management.
This commit is contained in:
Anish Sarkar 2026-04-25 01:07:02 +05:30
parent e84dc87c5b
commit 1c18735d38
7 changed files with 42 additions and 49 deletions

View file

@ -1,4 +1,4 @@
import { Notice, requestUrl, type RequestUrlParam, type RequestUrlResponse } from "obsidian";
import { requestUrl, type RequestUrlParam, type RequestUrlResponse } from "obsidian";
import type {
ConnectResponse,
DeleteAck,
@ -72,8 +72,11 @@ export interface ApiClientOptions {
onAuthError?: () => void;
}
const AUTH_BLOCK_MS = 60_000;
export class SurfSenseApiClient {
private readonly opts: ApiClientOptions;
private authBlockedUntil = 0;
constructor(opts: ApiClientOptions) {
this.opts = opts;
@ -83,6 +86,10 @@ export class SurfSenseApiClient {
Object.assign(this.opts, partial);
}
resetAuthBlock(): void {
this.authBlockedUntil = 0;
}
async health(): Promise<HealthResponse> {
return await this.request<HealthResponse>("GET", "/api/v1/obsidian/health");
}
@ -198,6 +205,9 @@ export class SurfSenseApiClient {
if (!token) {
throw new AuthError("Missing API token. Open SurfSense settings to paste one.");
}
if (Date.now() < this.authBlockedUntil) {
throw new AuthError("Token rejected. Paste a fresh one in settings.");
}
const headers: Record<string, string> = {
Authorization: `Bearer ${token}`,
Accept: "application/json",
@ -224,8 +234,8 @@ export class SurfSenseApiClient {
const detail = extractDetail(resp);
if (resp.status === 401) {
this.authBlockedUntil = Date.now() + AUTH_BLOCK_MS;
this.opts.onAuthError?.();
new Notice("Surfsense: token expired or invalid. Paste a fresh token in settings.");
throw new AuthError(detail || "Unauthorized");
}