mirror of
https://github.com/4gray/iptvnator.git
synced 2026-10-14 21:26:15 -08:00
The access token is stored with the instance id it was given for, and a page reads it only for its own instance: a tab still open on the old backend after BACKEND_URL changed no longer sends the new server's token there. The API client hands every 401 to reportAccessDenied(), which now also sends a page without a token for its instance to sign-in, while one that holds its token back until the instance is checked keeps it. A closed tab's adopted edits carry `yields`: replayed, they change only what is still as that tab saw it, so they never replace progress this tab or another device saved since. The flag survives the journal. The sync spec's fake server and memory storage move to a test-helpers file to keep the spec under its line limit. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
101 lines
3.2 KiB
TypeScript
101 lines
3.2 KiB
TypeScript
import {
|
|
ChangeDetectionStrategy,
|
|
Component,
|
|
inject,
|
|
signal,
|
|
} from '@angular/core';
|
|
import { MatButtonModule } from '@angular/material/button';
|
|
import { MatFormFieldModule } from '@angular/material/form-field';
|
|
import { MatIconModule } from '@angular/material/icon';
|
|
import { MatInputModule } from '@angular/material/input';
|
|
import { TranslatePipe } from '@ngx-translate/core';
|
|
import {
|
|
clearServerAccessToken,
|
|
writeServerAccessToken,
|
|
} from '@iptvnator/services';
|
|
import {
|
|
SERVER_STORAGE_CONFIG,
|
|
ServerApiError,
|
|
ServerStorageSession,
|
|
} from '@iptvnator/services/server-storage';
|
|
import { rememberServer } from './server-storage-start';
|
|
import { PasswordVisibilityToggleDirective } from '@iptvnator/ui/components/password-visibility-toggle';
|
|
|
|
export type ServerSignInFeedback = 'rejected' | 'unreachable' | 'storage';
|
|
|
|
@Component({
|
|
selector: 'app-server-sign-in',
|
|
imports: [
|
|
MatButtonModule,
|
|
MatFormFieldModule,
|
|
MatIconModule,
|
|
MatInputModule,
|
|
PasswordVisibilityToggleDirective,
|
|
TranslatePipe,
|
|
],
|
|
templateUrl: './server-sign-in.component.html',
|
|
styleUrl: './server-sign-in.component.scss',
|
|
changeDetection: ChangeDetectionStrategy.OnPush,
|
|
})
|
|
export class ServerSignInComponent {
|
|
private readonly session = inject(ServerStorageSession);
|
|
private readonly config = inject(SERVER_STORAGE_CONFIG);
|
|
|
|
readonly token = signal('');
|
|
readonly busy = signal(false);
|
|
readonly feedback = signal<ServerSignInFeedback | null>(null);
|
|
|
|
edit(value: string): void {
|
|
this.token.set(value);
|
|
this.feedback.set(null);
|
|
}
|
|
|
|
async submit(event: Event): Promise<void> {
|
|
event.preventDefault();
|
|
const token = this.token().trim();
|
|
if (!token || this.busy()) {
|
|
return;
|
|
}
|
|
this.busy.set(true);
|
|
this.feedback.set(null);
|
|
try {
|
|
// A page started from memory checks the server first: the token
|
|
// must reach only the one it is meant for.
|
|
await this.session.ensureVerified();
|
|
} catch {
|
|
this.feedback.set('unreachable');
|
|
this.busy.set(false);
|
|
return;
|
|
}
|
|
// Every request and the reload read the token from storage, and the
|
|
// next start keeps it only for the server it remembers.
|
|
if (
|
|
!writeServerAccessToken(token, this.config.instance.instanceId) ||
|
|
!rememberServer(this.config.baseUrl, this.config.instance)
|
|
) {
|
|
clearServerAccessToken();
|
|
this.feedback.set('storage');
|
|
this.busy.set(false);
|
|
return;
|
|
}
|
|
try {
|
|
await this.session.fetchStatus();
|
|
this.openWorkspace();
|
|
} catch (error) {
|
|
clearServerAccessToken();
|
|
this.feedback.set(
|
|
error instanceof ServerApiError && error.status === 401
|
|
? 'rejected'
|
|
: 'unreachable'
|
|
);
|
|
} finally {
|
|
this.busy.set(false);
|
|
}
|
|
}
|
|
|
|
/** Full reload so every service starts with the token. */
|
|
openWorkspace(): void {
|
|
window.location.assign(new URL('workspace', document.baseURI).href);
|
|
}
|
|
}
|