forked from firebase/firebaseui-web
-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathstorage.js
More file actions
360 lines (302 loc) · 10.7 KB
/
Copy pathstorage.js
File metadata and controls
360 lines (302 loc) · 10.7 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
/*
* Copyright 2016 Google Inc. All Rights Reserved.
*
* Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except
* in compliance with the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software distributed under the
* License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either
* express or implied. See the License for the specific language governing permissions and
* limitations under the License.
*/
/**
* @fileoverview Utils for storing FirebaseUI data.
*/
goog.provide('firebaseui.auth.storage');
goog.require('firebaseui.auth.Account');
goog.require('firebaseui.auth.PendingEmailCredential');
goog.require('goog.array');
goog.require('goog.storage.Storage');
goog.require('goog.storage.mechanism.HTML5LocalStorage');
goog.require('goog.storage.mechanism.HTML5SessionStorage');
goog.require('goog.storage.mechanism.mechanismfactory');
goog.scope(function() {
var mechanismfactory = goog.storage.mechanism.mechanismfactory;
var storage = firebaseui.auth.storage;
/**
* The namespace for FirebaseUI storage.
* @const {string}
* @private
*/
storage.NAMESPACE_ = 'firebaseui';
/**
* The separator for FirebaseUI storage with app ID key.
* @const {string}
* @private
*/
storage.SEPARATOR_ = ':';
/**
* The underlying storage instance for persistent data.
* @type {!goog.storage.Storage}
* @private
*/
storage.persistentStorage_ = new goog.storage.Storage(
/** @type {!goog.storage.mechanism.Mechanism} */
(mechanismfactory.createHTML5LocalStorage(storage.NAMESPACE_)));
/**
* The underlying storage instance for temporary data.
* @type {!goog.storage.Storage}
* @private
*/
storage.temporaryStorage_ = new goog.storage.Storage(
/** @type {!goog.storage.mechanism.Mechanism} */
(mechanismfactory.createHTML5SessionStorage(storage.NAMESPACE_)));
/**
* @return {boolean} True if web storage is supported, false otherwise.
*/
storage.isAvailable = function() {
return new goog.storage.mechanism.HTML5LocalStorage().isAvailable() &&
new goog.storage.mechanism.HTML5SessionStorage().isAvailable();
};
/**
* Valid keys for FirebaseUI data.
* @enum {{name: string, persistent: boolean}}
*/
storage.Key = {
// Temporary storage.
PENDING_EMAIL_CREDENTIAL: {name: 'pendingEmailCredential', persistent: false},
REDIRECT_URL: {name: 'redirectUrl', persistent: false},
REMEMBER_ACCOUNT: {name: 'rememberAccount', persistent: false},
// Persistent storage.
REMEMBERED_ACCOUNTS: {name: 'rememberedAccounts', persistent: true}
};
/**
* @param {boolean} persistent Whether to use the persistent storage.
* @return {!goog.storage.Storage} The corresponding storage instance.
* @private
*/
storage.getStorage_ = function(persistent) {
return persistent ? storage.persistentStorage_ : storage.temporaryStorage_;
};
/**
* Constructs the corresponding storage key name.
*
* @param {firebaseui.auth.storage.Key} key The key under which the value is
* stored.
* @param {string=} opt_id When operating in multiple app mode, this ID
* associates storage values with specific apps.
* @return {string} The corresponding key name.
* @private
*/
storage.getKeyName_ = function(key, opt_id) {
return opt_id ? key.name + storage.SEPARATOR_ + opt_id : key.name;
};
/**
* Gets the stored value from the corresponding storage.
*
* @param {firebaseui.auth.storage.Key} key The key under which the value is
* stored.
* @param {string=} opt_id When operating in multiple app mode, this ID
* associates storage values with specific apps.
* @return {*} The stored value.
* @private
*/
storage.get_ = function(key, opt_id) {
return storage.getStorage_(key.persistent).get(
storage.getKeyName_(key, opt_id));
};
/**
* Removes the stored value from the corresponding storage.
*
* @param {firebaseui.auth.storage.Key} key The key under which the value is
* stored.
* @param {string=} opt_id When operating in multiple app mode, this ID
* associates storage values with specific apps.
* @private
*/
storage.remove_ = function(key, opt_id) {
storage.getStorage_(key.persistent).remove(
storage.getKeyName_(key, opt_id));
};
/**
* Stores the value in the corresponding storage.
*
* @param {firebaseui.auth.storage.Key} key The key under which the value is
* stored.
* @param {*} value The value to be stored.
* @param {string=} opt_id When operating in multiple app mode, this ID
* associates storage values with specific apps.
* @private
*/
storage.set_ = function(key, value, opt_id) {
storage.getStorage_(key.persistent).set(
storage.getKeyName_(key, opt_id), value);
};
/**
* @param {string=} opt_id When operating in multiple app mode, this ID
* associates storage values with specific apps.
* @return {boolean} Whether there is a redirect URL for a successful sign-in.
*/
storage.hasRedirectUrl = function(opt_id) {
return !!storage.getRedirectUrl(opt_id);
};
/**
* @param {string=} opt_id When operating in multiple app mode, this ID
* associates storage values with specific apps.
* @return {?string} The sign-in redirect URL.
*/
storage.getRedirectUrl = function(opt_id) {
return /** @type {?string} */ (
storage.get_(storage.Key.REDIRECT_URL, opt_id) || null);
};
/**
* Removes the sign-in redirect URL if it exists.
*
* @param {string=} opt_id When operating in multiple app mode, this ID
* associates storage values with specific apps.
*/
storage.removeRedirectUrl = function(opt_id) {
storage.remove_(storage.Key.REDIRECT_URL, opt_id);
};
/**
* Stores the sign-in redirect URL.
*
* @param {string} redirectUrl The sign-in redirect URL.
* @param {string=} opt_id When operating in multiple app mode, this ID
* associates storage values with specific apps.
*/
storage.setRedirectUrl = function(redirectUrl, opt_id) {
storage.set_(storage.Key.REDIRECT_URL, redirectUrl, opt_id);
};
/**
* @param {string=} opt_id When operating in multiple app mode, this ID
* associates storage values with specific apps.
* @return {boolean} Whether there is a remember account setting.
*/
storage.hasRememberAccount = function(opt_id) {
return storage.get_(storage.Key.REMEMBER_ACCOUNT, opt_id) != null;
};
/**
* @param {string=} opt_id When operating in multiple app mode, this ID
* associates storage values with specific apps.
* @return {boolean} Whether or not to remember the account. {@code false} is
* returned if there is no such setting.
*/
storage.isRememberAccount = function(opt_id) {
return !!storage.get_(storage.Key.REMEMBER_ACCOUNT, opt_id);
};
/**
* Stores the remember account setting.
*
* @param {boolean} remember Wheter or not to remember the account.
* @param {string=} opt_id When operating in multiple app mode, this ID
* associates storage values with specific apps.
*/
storage.setRememberAccount = function(remember, opt_id) {
storage.set_(storage.Key.REMEMBER_ACCOUNT, remember, opt_id);
};
/**
* Removes the remember account setting.
*
* @param {string=} opt_id When operating in multiple app mode, this ID
* associates storage values with specific apps.
*/
storage.removeRememberAccount = function(opt_id) {
storage.remove_(storage.Key.REMEMBER_ACCOUNT, opt_id);
};
/**
* @param {string=} opt_id When operating in multiple app mode, this ID
* associates storage values with specific apps.
* @return {Array<firebaseui.auth.Account>} The remembered accounts.
*/
storage.getRememberedAccounts = function(opt_id) {
var rawAccounts = /** @type {Array<!Object>} */ (
storage.get_(storage.Key.REMEMBERED_ACCOUNTS, opt_id) || []);
var accounts = goog.array.map(rawAccounts, function(element) {
return firebaseui.auth.Account.fromPlainObject(element);
});
return goog.array.filter(accounts, goog.isDefAndNotNull);
};
/**
* Remembers an account.
*
* @param {!firebaseui.auth.Account} account The account to remember.
* @param {string=} opt_id When operating in multiple app mode, this ID
* associates storage values with specific apps.
*/
storage.rememberAccount = function(account, opt_id) {
var accounts = storage.getRememberedAccounts(opt_id);
// Find the account if it's already remembered.
var index = goog.array.findIndex(accounts, function(element) {
return element.getEmail() == account.getEmail() &&
element.getProviderId() == account.getProviderId();
});
if (index > -1) {
goog.array.removeAt(accounts, index);
}
// Put the last added account to the begining of the array so it appears as
// the first one.
accounts.unshift(account);
storage.set_(
storage.Key.REMEMBERED_ACCOUNTS,
goog.array.map(accounts, function(element) {
return element.toPlainObject();
}),
opt_id);
};
/**
* Removes all remembered accounts.
*
* @param {string=} opt_id When operating in multiple app mode, this ID
* associates storage values with specific apps.
*/
storage.removeRememberedAccounts = function(opt_id) {
storage.remove_(storage.Key.REMEMBERED_ACCOUNTS, opt_id);
};
/**
* @param {string=} opt_id When operating in multiple app mode, this ID
* associates storage values with specific apps.
* @return {boolean} Whether there is a pending email credential for the
* account.
*/
storage.hasPendingEmailCredential = function(opt_id) {
return !!storage.getPendingEmailCredential(opt_id);
};
/**
* @param {string=} opt_id When operating in multiple app mode, this ID
* associates storage values with specific apps.
* @return {?firebaseui.auth.PendingEmailCredential} The stored pending email
* credential if it exists.
*/
storage.getPendingEmailCredential = function(opt_id) {
var credentialObject = /** @type {?Object} */ (
storage.get_(storage.Key.PENDING_EMAIL_CREDENTIAL, opt_id) || null);
return firebaseui.auth.PendingEmailCredential.fromPlainObject(
credentialObject);
};
/**
* Removes the stored pending email credential if it exists.
*
* @param {string=} opt_id When operating in multiple app mode, this ID
* associates storage values with specific apps.
*/
storage.removePendingEmailCredential = function(opt_id) {
storage.remove_(storage.Key.PENDING_EMAIL_CREDENTIAL, opt_id);
};
/**
* Stores the pending email credential.
*
* @param {!firebaseui.auth.PendingEmailCredential} pendingEmailCredential The
* pending email credential to store.
* @param {string=} opt_id When operating in multiple app mode, this ID
* associates storage values with specific apps.
*/
storage.setPendingEmailCredential = function(pendingEmailCredential, opt_id) {
storage.set_(
storage.Key.PENDING_EMAIL_CREDENTIAL,
pendingEmailCredential.toPlainObject(),
opt_id);
};
});