Skip to content

[Auth] Fix persistence selection in compatibility layer in worker scripts #5811

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Dec 10, 2021
Merged
Show file tree
Hide file tree
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
6 changes: 6 additions & 0 deletions .changeset/cuddly-birds-stare.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
---
"@firebase/auth-compat": patch
"@firebase/auth": patch
---

Fix persistence selection in compatibility layer in worker scripts
60 changes: 39 additions & 21 deletions packages/auth-compat/src/auth.ts
Original file line number Diff line number Diff line change
Expand Up @@ -57,26 +57,6 @@ export class Auth
appName: app.name
});

let persistences: exp.Persistence[] = [exp.inMemoryPersistence];

// Only deal with persistences in web environments
if (typeof window !== 'undefined') {
// Note this is slightly different behavior: in this case, the stored
// persistence is checked *first* rather than last. This is because we want
// to prefer stored persistence type in the hierarchy.
persistences = _getPersistencesFromRedirect(apiKey, app.name);

for (const persistence of [
exp.indexedDBLocalPersistence,
exp.browserLocalPersistence,
exp.browserSessionPersistence
]) {
if (!persistences.includes(persistence)) {
persistences.push(persistence);
}
}
}

// TODO: platform needs to be determined using heuristics
_assert(apiKey, exp.AuthErrorCode.INVALID_API_KEY, {
appName: app.name
Expand All @@ -87,7 +67,7 @@ export class Auth
typeof window !== 'undefined' ? CompatPopupRedirectResolver : undefined;
this._delegate = provider.initialize({
options: {
persistence: persistences,
persistence: buildPersistenceHierarchy(apiKey, app.name),
popupRedirectResolver: resolver
}
}) as exp.AuthImpl;
Expand Down Expand Up @@ -382,3 +362,41 @@ function wrapObservers(
complete
};
}

function buildPersistenceHierarchy(
apiKey: string,
appName: string
): exp.Persistence[] {
// Note this is slightly different behavior: in this case, the stored
// persistence is checked *first* rather than last. This is because we want
// to prefer stored persistence type in the hierarchy. This is an empty
// array if window is not available or there is no pending redirect
const persistences = _getPersistencesFromRedirect(apiKey, appName);

// If "self" is available, add indexedDB
if (
typeof self !== 'undefined' &&
!persistences.includes(exp.indexedDBLocalPersistence)
) {
persistences.push(exp.indexedDBLocalPersistence);
}

// If "window" is available, add HTML Storage persistences
if (typeof window !== 'undefined') {
for (const persistence of [
exp.browserLocalPersistence,
exp.browserSessionPersistence
]) {
if (!persistences.includes(persistence)) {
persistences.push(persistence);
}
}
}

// Add in-memory as a final fallback
if (!persistences.includes(exp.inMemoryPersistence)) {
persistences.push(exp.inMemoryPersistence);
}

return persistences;
}