-
Notifications
You must be signed in to change notification settings - Fork 927
Add react native persistence class #2955
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
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,35 @@ | ||
/** | ||
* @license | ||
* Copyright 2019 Google LLC | ||
* | ||
* 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. | ||
*/ | ||
|
||
/** | ||
* Basic stub for what is expected from the package 'react-native'. | ||
* | ||
* This should be a subset `@types/react-native` which cannot be installed | ||
* because it has conflicting definitions with typescript/dom. If included in | ||
* deps, yarn will attempt to install this in the root directory of the | ||
* monolithic repository which will then be included in the base `tsconfig.json` | ||
* via `typeroots` and then break every other package in this repo. | ||
*/ | ||
|
||
declare module 'react-native' { | ||
interface ReactNativeAsyncStorage { | ||
setItem(key: string, value: string): Promise<void>; | ||
getItem(key: string): Promise<string | null>; | ||
removeItem(key: string): Promise<void>; | ||
} | ||
export const AsyncStorage: ReactNativeAsyncStorage; | ||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
78 changes: 78 additions & 0 deletions
78
packages-exp/auth-exp/src/core/persistence/react_native.test.ts
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,78 @@ | ||
/** | ||
* @license | ||
* Copyright 2019 Google LLC | ||
* | ||
* 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. | ||
*/ | ||
|
||
import { expect } from 'chai'; | ||
|
||
import { testUser } from '../../../test/mock_auth'; | ||
import { PersistedBlob, PersistenceType } from './'; | ||
import { ReactNativePersistence } from './react_native'; | ||
import { ReactNativeAsyncStorage } from '@firebase/auth-types-exp'; | ||
|
||
/** | ||
* Wraps in-memory storage with the react native AsyncStorage API. | ||
*/ | ||
class FakeAsyncStorage implements ReactNativeAsyncStorage { | ||
storage: { | ||
[key: string]: string; | ||
} = {}; | ||
|
||
async getItem(key: string): Promise<string | null> { | ||
const value = this.storage[key]; | ||
return value ?? null; | ||
} | ||
async removeItem(key: string): Promise<void> { | ||
delete this.storage[key]; | ||
} | ||
async setItem(key: string, value: string): Promise<void> { | ||
this.storage[key] = value; | ||
} | ||
clear(): void { | ||
this.storage = {}; | ||
} | ||
} | ||
|
||
describe('core/persistence/react', () => { | ||
const fakeAsyncStorage = new FakeAsyncStorage(); | ||
const persistence = new ReactNativePersistence(fakeAsyncStorage); | ||
|
||
beforeEach(() => { | ||
fakeAsyncStorage.clear(); | ||
}); | ||
|
||
it('should work with persistence type', async () => { | ||
const key = 'my-super-special-persistence-type'; | ||
const value = PersistenceType.LOCAL; | ||
expect(await persistence.get(key)).to.be.null; | ||
await persistence.set(key, value); | ||
expect(await persistence.get(key)).to.be.eq(value); | ||
expect(await persistence.get('other-key')).to.be.null; | ||
await persistence.remove(key); | ||
expect(await persistence.get(key)).to.be.null; | ||
}); | ||
|
||
it('should return persistedblob from user', async () => { | ||
const key = 'my-super-special-user'; | ||
const value = testUser('some-uid'); | ||
|
||
expect(await persistence.get(key)).to.be.null; | ||
await persistence.set(key, value.toPlainObject()); | ||
const out = await persistence.get<PersistedBlob>(key); | ||
expect(out!['uid']).to.eql(value.uid); | ||
await persistence.remove(key); | ||
expect(await persistence.get(key)).to.be.null; | ||
}); | ||
}); |
61 changes: 61 additions & 0 deletions
61
packages-exp/auth-exp/src/core/persistence/react_native.ts
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,61 @@ | ||
/** | ||
* @license | ||
* Copyright 2019 Google Inc. | ||
* | ||
* 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. | ||
*/ | ||
|
||
import { | ||
Persistence, | ||
PersistenceType, | ||
PersistenceValue, | ||
STORAGE_AVAILABLE_KEY | ||
} from './'; | ||
import {ReactNativeAsyncStorage} from '@firebase/auth-types-exp'; | ||
|
||
/** | ||
* Persistence class that wraps AsyncStorage imported from `react-native` or `@react-native-community/async-storage`. | ||
*/ | ||
export class ReactNativePersistence implements Persistence { | ||
readonly type: PersistenceType = PersistenceType.LOCAL; | ||
|
||
constructor(private readonly storage: ReactNativeAsyncStorage) {} | ||
|
||
async isAvailable(): Promise<boolean> { | ||
try { | ||
if (!this.storage) { | ||
return false; | ||
} | ||
await this.storage.setItem(STORAGE_AVAILABLE_KEY, '1'); | ||
await this.storage.removeItem(STORAGE_AVAILABLE_KEY); | ||
return true; | ||
} catch { | ||
return false; | ||
} | ||
} | ||
|
||
async set(key: string, value: PersistenceValue): Promise<void> { | ||
await this.storage.setItem(key, JSON.stringify(value)); | ||
} | ||
|
||
async get<T extends PersistenceValue>( | ||
key: string | ||
): Promise<T | null> { | ||
const json = await this.storage.getItem(key); | ||
return json ? JSON.parse(json) : null; | ||
} | ||
|
||
async remove(key: string): Promise<void> { | ||
await this.storage.removeItem(key); | ||
} | ||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -12592,6 +12592,17 @@ [email protected]: | |
rollup-pluginutils "2.8.2" | ||
tslib "1.10.0" | ||
|
||
[email protected]: | ||
version "0.26.0" | ||
resolved "https://registry.npmjs.org/rollup-plugin-typescript2/-/rollup-plugin-typescript2-0.26.0.tgz#cee2b44d51d9623686656d76dc30a73c4de91672" | ||
integrity sha512-lUK7XZVG77tu8dmv1L/0LZFlavED/5Yo6e4iMMl6fdox/yKdj4IFRRPPJEXNdmEaT1nDQQeCi7b5IwKHffMNeg== | ||
dependencies: | ||
find-cache-dir "^3.2.0" | ||
fs-extra "8.1.0" | ||
resolve "1.15.1" | ||
rollup-pluginutils "2.8.2" | ||
tslib "1.10.0" | ||
|
||
[email protected]: | ||
version "0.27.0" | ||
resolved "https://registry.npmjs.org/rollup-plugin-typescript2/-/rollup-plugin-typescript2-0.27.0.tgz#95ff96f9e07d5000a9d2df4d76b548f9a1f83511" | ||
|
@@ -12613,6 +12624,13 @@ [email protected]: | |
serialize-javascript "^2.1.2" | ||
uglify-js "^3.4.9" | ||
|
||
[email protected]: | ||
version "2.8.1" | ||
resolved "https://registry.npmjs.org/rollup-pluginutils/-/rollup-pluginutils-2.8.1.tgz#8fa6dd0697344938ef26c2c09d2488ce9e33ce97" | ||
integrity sha512-J5oAoysWar6GuZo0s+3bZ6sVZAC0pfqKz68De7ZgDi5z63jOVZn1uJL/+z1jeKHNbGII8kAyHF5q8LnxSX5lQg== | ||
dependencies: | ||
estree-walker "^0.6.1" | ||
|
||
[email protected], rollup-pluginutils@^2.5.0, rollup-pluginutils@^2.6.0, rollup-pluginutils@^2.8.1, rollup-pluginutils@^2.8.2: | ||
version "2.8.2" | ||
resolved "https://registry.npmjs.org/rollup-pluginutils/-/rollup-pluginutils-2.8.2.tgz#72f2af0748b592364dbd3389e600e5a9444a351e" | ||
|
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
it'd be great if the storage layer were injectable, so if someone were to want to sub in something for AsyncStorage they would be able to do that provided that it conforms to the same interface. redux-persist provides a storage option used on initialization: https://github.com/rt2zz/redux-persist#v6-upgrade
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
That is the overall idea, but this is a polyfill and maintains the legacy behavior.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I see that you're big into expo contributions! Is there anything else you think would be helpful from the firebase auth SDK?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
hey @scottcrossen! @EvanBacon has been doing a bunch of work on authentication apis in expo recently, he may have some questions for you.
can we invite you to our slack to discuss further? if you let me know your email i'd be happy to invite you and other folks on your team so we can collaborate further.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Sure sounds good! Email me to get me setup [email protected]
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@scottcrossen - sent!