Skip to content

Commit abb89de

Browse files
authored
Merge 02778cd into fd8cd3e
2 parents fd8cd3e + 02778cd commit abb89de

File tree

2 files changed

+45
-21
lines changed

2 files changed

+45
-21
lines changed

.changeset/cuddly-birds-stare.md

+6
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
---
2+
"@firebase/auth-compat": patch
3+
"@firebase/auth": patch
4+
---
5+
6+
Fix persistence selection in compatibility layer in worker scripts

packages/auth-compat/src/auth.ts

+39-21
Original file line numberDiff line numberDiff line change
@@ -57,26 +57,6 @@ export class Auth
5757
appName: app.name
5858
});
5959

60-
let persistences: exp.Persistence[] = [exp.inMemoryPersistence];
61-
62-
// Only deal with persistences in web environments
63-
if (typeof window !== 'undefined') {
64-
// Note this is slightly different behavior: in this case, the stored
65-
// persistence is checked *first* rather than last. This is because we want
66-
// to prefer stored persistence type in the hierarchy.
67-
persistences = _getPersistencesFromRedirect(apiKey, app.name);
68-
69-
for (const persistence of [
70-
exp.indexedDBLocalPersistence,
71-
exp.browserLocalPersistence,
72-
exp.browserSessionPersistence
73-
]) {
74-
if (!persistences.includes(persistence)) {
75-
persistences.push(persistence);
76-
}
77-
}
78-
}
79-
8060
// TODO: platform needs to be determined using heuristics
8161
_assert(apiKey, exp.AuthErrorCode.INVALID_API_KEY, {
8262
appName: app.name
@@ -87,7 +67,7 @@ export class Auth
8767
typeof window !== 'undefined' ? CompatPopupRedirectResolver : undefined;
8868
this._delegate = provider.initialize({
8969
options: {
90-
persistence: persistences,
70+
persistence: buildPersistenceHierarchy(apiKey, app.name),
9171
popupRedirectResolver: resolver
9272
}
9373
}) as exp.AuthImpl;
@@ -382,3 +362,41 @@ function wrapObservers(
382362
complete
383363
};
384364
}
365+
366+
function buildPersistenceHierarchy(
367+
apiKey: string,
368+
appName: string
369+
): exp.Persistence[] {
370+
// Note this is slightly different behavior: in this case, the stored
371+
// persistence is checked *first* rather than last. This is because we want
372+
// to prefer stored persistence type in the hierarchy. This is an empty
373+
// array if window is not available or there is no pending redirect
374+
const persistences = _getPersistencesFromRedirect(apiKey, appName);
375+
376+
// If "self" is available, add indexedDB
377+
if (
378+
typeof self !== 'undefined' &&
379+
!persistences.includes(exp.indexedDBLocalPersistence)
380+
) {
381+
persistences.push(exp.indexedDBLocalPersistence);
382+
}
383+
384+
// If "window" is available, add HTML Storage persistences
385+
if (typeof window !== 'undefined') {
386+
for (const persistence of [
387+
exp.browserLocalPersistence,
388+
exp.browserSessionPersistence
389+
]) {
390+
if (!persistences.includes(persistence)) {
391+
persistences.push(persistence);
392+
}
393+
}
394+
}
395+
396+
// Add in-memory as a final fallback
397+
if (!persistences.includes(exp.inMemoryPersistence)) {
398+
persistences.push(exp.inMemoryPersistence);
399+
}
400+
401+
return persistences;
402+
}

0 commit comments

Comments
 (0)