Skip to content

Worker persistence #2907

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
Apr 14, 2020
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
12 changes: 11 additions & 1 deletion packages/auth/src/authstorage.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
/**
* @license
* Copyright 2017 Google Inc.
* Copyright 2017 Google LLC
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
Expand All @@ -27,6 +27,7 @@ goog.provide('fireauth.authStorage.Persistence');
goog.require('fireauth.AuthError');
goog.require('fireauth.authenum.Error');
goog.require('fireauth.storage.Factory');
goog.require('fireauth.storage.IndexedDB');
goog.require('fireauth.storage.Storage');
goog.require('fireauth.util');
goog.require('goog.Promise');
Expand Down Expand Up @@ -109,6 +110,15 @@ fireauth.authStorage.validatePersistenceArgument =
throw unsupportedTypeError;
}
break;
case fireauth.util.Env.WORKER:
// In a worker environment, either LOCAL or NONE are supported.
// If indexedDB not supported and LOCAL provided, throw an error.
if (arg === fireauth.authStorage.Persistence.SESSION ||
(!fireauth.storage.IndexedDB.isAvailable() &&
arg !== fireauth.authStorage.Persistence.NONE)) {
throw unsupportedTypeError;
}
break;
case fireauth.util.Env.BROWSER:
default:
// This is restricted by what the browser supports.
Expand Down
49 changes: 48 additions & 1 deletion packages/auth/test/authstorage_test.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
/**
* @license
* Copyright 2017 Google Inc.
* Copyright 2017 Google LLC
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
Expand Down Expand Up @@ -135,6 +135,53 @@ function testValidatePersistenceArgument_node() {
});
}

function testValidatePersistenceArgument_worker() {
// Simulate worker environment.
stubs.replace(
fireauth.util,
'getEnvironment',
function() {
return fireauth.util.Env.WORKER;
});
// Simulate indexedDB supported.
stubs.replace(
fireauth.storage.IndexedDB,
'isAvailable',
function() {
return true;
});
var unsupportedTypeError = new fireauth.AuthError(
fireauth.authenum.Error.UNSUPPORTED_PERSISTENCE);
// Session should throw an error.
fireauth.common.testHelper.assertErrorEquals(
unsupportedTypeError,
assertThrows(function() {
fireauth.authStorage.validatePersistenceArgument('session');
}));
// Local should not throw an error when indexedDB is supported.
assertNotThrows(function() {
fireauth.authStorage.validatePersistenceArgument('local');
});
// None should be supported.
assertNotThrows(function() {
fireauth.authStorage.validatePersistenceArgument('none');
});

// Simulate indexedDB not supported.
stubs.replace(
fireauth.storage.IndexedDB,
'isAvailable',
function() {
return false;
});
// Local should throw an error when indexedDB not supported.
fireauth.common.testHelper.assertErrorEquals(
unsupportedTypeError,
assertThrows(function() {
fireauth.authStorage.validatePersistenceArgument('local');
}));
}


function testValidatePersistenceArgument_reactNative() {
// Simulate React-Native.
Expand Down