Skip to content

Commit f0a0dc9

Browse files
Worker persistence (#2907)
* Fixes ability to set Auth LOCAL persistence explicitly in a worker environment.
1 parent ce41a64 commit f0a0dc9

File tree

2 files changed

+59
-2
lines changed

2 files changed

+59
-2
lines changed

packages/auth/src/authstorage.js

+11-1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
/**
22
* @license
3-
* Copyright 2017 Google Inc.
3+
* Copyright 2017 Google LLC
44
*
55
* Licensed under the Apache License, Version 2.0 (the "License");
66
* you may not use this file except in compliance with the License.
@@ -27,6 +27,7 @@ goog.provide('fireauth.authStorage.Persistence');
2727
goog.require('fireauth.AuthError');
2828
goog.require('fireauth.authenum.Error');
2929
goog.require('fireauth.storage.Factory');
30+
goog.require('fireauth.storage.IndexedDB');
3031
goog.require('fireauth.storage.Storage');
3132
goog.require('fireauth.util');
3233
goog.require('goog.Promise');
@@ -109,6 +110,15 @@ fireauth.authStorage.validatePersistenceArgument =
109110
throw unsupportedTypeError;
110111
}
111112
break;
113+
case fireauth.util.Env.WORKER:
114+
// In a worker environment, either LOCAL or NONE are supported.
115+
// If indexedDB not supported and LOCAL provided, throw an error.
116+
if (arg === fireauth.authStorage.Persistence.SESSION ||
117+
(!fireauth.storage.IndexedDB.isAvailable() &&
118+
arg !== fireauth.authStorage.Persistence.NONE)) {
119+
throw unsupportedTypeError;
120+
}
121+
break;
112122
case fireauth.util.Env.BROWSER:
113123
default:
114124
// This is restricted by what the browser supports.

packages/auth/test/authstorage_test.js

+48-1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
/**
22
* @license
3-
* Copyright 2017 Google Inc.
3+
* Copyright 2017 Google LLC
44
*
55
* Licensed under the Apache License, Version 2.0 (the "License");
66
* you may not use this file except in compliance with the License.
@@ -135,6 +135,53 @@ function testValidatePersistenceArgument_node() {
135135
});
136136
}
137137

138+
function testValidatePersistenceArgument_worker() {
139+
// Simulate worker environment.
140+
stubs.replace(
141+
fireauth.util,
142+
'getEnvironment',
143+
function() {
144+
return fireauth.util.Env.WORKER;
145+
});
146+
// Simulate indexedDB supported.
147+
stubs.replace(
148+
fireauth.storage.IndexedDB,
149+
'isAvailable',
150+
function() {
151+
return true;
152+
});
153+
var unsupportedTypeError = new fireauth.AuthError(
154+
fireauth.authenum.Error.UNSUPPORTED_PERSISTENCE);
155+
// Session should throw an error.
156+
fireauth.common.testHelper.assertErrorEquals(
157+
unsupportedTypeError,
158+
assertThrows(function() {
159+
fireauth.authStorage.validatePersistenceArgument('session');
160+
}));
161+
// Local should not throw an error when indexedDB is supported.
162+
assertNotThrows(function() {
163+
fireauth.authStorage.validatePersistenceArgument('local');
164+
});
165+
// None should be supported.
166+
assertNotThrows(function() {
167+
fireauth.authStorage.validatePersistenceArgument('none');
168+
});
169+
170+
// Simulate indexedDB not supported.
171+
stubs.replace(
172+
fireauth.storage.IndexedDB,
173+
'isAvailable',
174+
function() {
175+
return false;
176+
});
177+
// Local should throw an error when indexedDB not supported.
178+
fireauth.common.testHelper.assertErrorEquals(
179+
unsupportedTypeError,
180+
assertThrows(function() {
181+
fireauth.authStorage.validatePersistenceArgument('local');
182+
}));
183+
}
184+
138185

139186
function testValidatePersistenceArgument_reactNative() {
140187
// Simulate React-Native.

0 commit comments

Comments
 (0)