Skip to content

Commit

Permalink
Worker persistence (#2907)
Browse files Browse the repository at this point in the history
* Fixes ability to set Auth LOCAL persistence explicitly in a worker environment.
  • Loading branch information
bojeil-google authored Apr 14, 2020
1 parent ce41a64 commit f0a0dc9
Show file tree
Hide file tree
Showing 2 changed files with 59 additions and 2 deletions.
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

0 comments on commit f0a0dc9

Please sign in to comment.