Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
21 changes: 17 additions & 4 deletions src/LoginManager.ts
Original file line number Diff line number Diff line change
Expand Up @@ -351,6 +351,10 @@ export class LoginManager extends Observable<LoginManager> {
const result = await this.endpointManager.validateAndSetEndpoints(timeoutMs);

if (result.success && this.endpointManager.hasValidatedEndpoints()) {
// Clean up old PocketBase instance before creating new one
this.pb.cancelAllRequests();
this.pb.realtime.unsubscribe();

// Recreate PocketBase instance with new auth URL
const pbLog = curryLog("[Pocketbase]", "debug");
this.pb = new PocketBase(this.endpointManager.getAuthUrl(), this.authStore);
Expand Down Expand Up @@ -389,6 +393,7 @@ export class LoginManager extends Observable<LoginManager> {

logout() {
this.pb.cancelAllRequests();
this.pb.realtime.unsubscribe();
this.pb.authStore.clear();
this.user = undefined;
this.notifyListeners();
Expand Down Expand Up @@ -549,10 +554,18 @@ export class LoginManager extends Observable<LoginManager> {

async login(provider: string): Promise<boolean> {
this.beforeLogin();
const authData = await this.pb.collection("users").authWithOAuth2({
provider: provider,
});
return this.setup(authData, provider);
try {
const authData = await this.pb.collection("users").authWithOAuth2({
provider: provider,
});
return this.setup(authData, provider);
} catch (e) {
// Clean up realtime subscription to prevent reconnection loops
// authWithOAuth2 internally subscribes to @oauth2 via SSE, and if it fails,
// PocketBase's realtime client will keep trying to reconnect indefinitely
this.pb.realtime.unsubscribe();
throw e;
}
}

async openLoginPage() {
Expand Down