Skip to content

Commit deb8b99

Browse files
committed
Auto-login to FamilySearch when sync detects missing auth token
1 parent d41dbbf commit deb8b99

2 files changed

Lines changed: 47 additions & 17 deletions

File tree

‎.changelog/v0.6.x.md‎

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,12 @@ This release normalizes FamilySearch as a downstream provider (equal to Ancestry
3737
- Augmentation service now detects and recovers from stale connections before scraping
3838
- Fixes silent failures when browser was closed but connection state was still "connected"
3939

40+
### Auto-Login on Sync (v0.6.x)
41+
- FamilySearch sync now attempts automatic login when auth token is missing
42+
- Uses `providerService.ensureAuthenticated()` to try Google SSO or stored credentials
43+
- Eliminates need for manual browser login before sync operations
44+
- Applies to both `refreshPerson()` and `fetchPersonDisplayName()` methods
45+
4046
### "Set as Primary" Photo Not Updating (v0.6.x)
4147
- Fixed photo not updating after clicking "Set as Primary" on provider photos
4248
- Root causes fixed:

‎server/src/services/familysearch-refresh.service.ts‎

Lines changed: 41 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ import FamilySearch from 'fs-js-lite';
99
import fs from 'fs';
1010
import path from 'path';
1111
import { browserService } from './browser.service.js';
12+
import { providerService } from './provider.service.js';
1213
import { idMappingService } from './id-mapping.service.js';
1314
import { sqliteWriter } from '../lib/sqlite-writer.js';
1415
// eslint-disable-next-line @typescript-eslint/ban-ts-comment
@@ -121,24 +122,36 @@ export const familySearchRefreshService = {
121122
};
122123
}
123124

124-
// Ensure browser is connected
125-
if (!browserService.isConnected()) {
126-
const connected = await browserService.connect().catch(() => null);
127-
if (!connected) {
128-
return {
129-
success: false,
130-
error: 'Browser not connected. Please connect browser in Settings.',
131-
};
132-
}
125+
// Verify browser connection is truly active (not stale) and reconnect if needed
126+
const browserReady = await browserService.verifyAndReconnect();
127+
if (!browserReady) {
128+
return {
129+
success: false,
130+
error: 'Browser not connected. Please connect browser in Settings.',
131+
};
133132
}
134133

135134
// Extract auth token from browser session
136-
const { token } = await browserService.getFamilySearchToken();
135+
let { token } = await browserService.getFamilySearchToken();
136+
137+
// If no token, attempt auto-login and retry
137138
if (!token) {
138-
return {
139-
success: false,
140-
error: 'No FamilySearch authentication found. Please log in to FamilySearch via the browser.',
141-
};
139+
logger.auth('fs-refresh', 'No auth token found, attempting auto-login...');
140+
const authResult = await providerService.ensureAuthenticated('familysearch');
141+
142+
if (authResult.authenticated) {
143+
// Retry getting the token after successful login
144+
const retryResult = await browserService.getFamilySearchToken();
145+
token = retryResult.token;
146+
}
147+
148+
if (!token) {
149+
const errorMsg = authResult.error || 'No FamilySearch authentication found. Please log in to FamilySearch via the browser.';
150+
return {
151+
success: false,
152+
error: errorMsg,
153+
};
154+
}
142155
}
143156

144157
// Fetch fresh data from FamilySearch API
@@ -255,10 +268,21 @@ export const familySearchRefreshService = {
255268
const cached = this.getParsedCachedData(fsId);
256269
if (cached?.name) return cached.name;
257270

258-
// Need browser auth for API call
259-
if (!browserService.isConnected()) return null;
271+
// Verify browser connection and reconnect if needed
272+
const browserReady = await browserService.verifyAndReconnect();
273+
if (!browserReady) return null;
274+
275+
let { token } = await browserService.getFamilySearchToken().catch(() => ({ token: null }));
276+
277+
// If no token, attempt auto-login and retry
278+
if (!token) {
279+
const authResult = await providerService.ensureAuthenticated('familysearch').catch(() => ({ authenticated: false }));
280+
if (authResult.authenticated) {
281+
const retryResult = await browserService.getFamilySearchToken().catch(() => ({ token: null }));
282+
token = retryResult.token;
283+
}
284+
}
260285

261-
const { token } = await browserService.getFamilySearchToken().catch(() => ({ token: null }));
262286
if (!token) return null;
263287

264288
const fetchResult = await fetchPersonFromApi(fsId, token).catch(() => null);

0 commit comments

Comments
 (0)