Skip to content

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 3 commits into from
May 4, 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
6 changes: 4 additions & 2 deletions packages-exp/auth-compat-exp/index.rn.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,8 @@
* just use index.ts
*/

import { testFxn } from './src';
import { AsyncStorage } from 'react-native';

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

Copy link
Contributor Author

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.

Copy link
Contributor Author

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?

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.

Copy link
Contributor Author

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]

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@scottcrossen - sent!

import { ReactNativePersistence } from '@firebase/auth-exp/src/core/persistence/react_native';

testFxn();
// eslint-disable-next-line @typescript-eslint/no-unused-vars
const reactNativeLocalPersistence = new ReactNativePersistence(AsyncStorage);
4 changes: 3 additions & 1 deletion packages-exp/auth-compat-exp/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,9 @@
},
"peerDependencies": {
"@firebase/app-exp": "0.x",
"@firebase/app-types-exp": "0.x"
"@firebase/app-types-exp": "0.x",
"@firebase/auth-exp": "0.x",
"@firebase/auth-types-exp": "0.x"
},
"dependencies": {
"tslib": "1.11.1"
Expand Down
35 changes: 35 additions & 0 deletions packages-exp/auth-compat-exp/react-native.d.ts
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;
}
2 changes: 1 addition & 1 deletion packages-exp/auth-compat-exp/rollup.config.js
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@ const es5Builds = [
external: id => deps.some(dep => id === dep || id.startsWith(`${dep}/`))
},
/**
* App React Native Builds
* React Native Builds
*/
{
input: 'index.rn.ts',
Expand Down
3 changes: 2 additions & 1 deletion packages-exp/auth-exp/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,8 @@
},
"peerDependencies": {
"@firebase/app-exp": "0.x",
"@firebase/app-types-exp": "0.x"
"@firebase/app-types-exp": "0.x",
"@firebase/auth-types-exp": "0.x"
},
"dependencies": {
"@firebase/logger": "^0.2.2",
Expand Down
78 changes: 78 additions & 0 deletions packages-exp/auth-exp/src/core/persistence/react_native.test.ts
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 packages-exp/auth-exp/src/core/persistence/react_native.ts
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);
}
}
6 changes: 4 additions & 2 deletions packages-exp/auth-types-exp/index.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,8 @@
* limitations under the License.
*/

export interface TestType {
prop?: string;
export interface ReactNativeAsyncStorage {
setItem(key: string, value: string): Promise<void>;
getItem(key: string): Promise<string | null>;
removeItem(key: string): Promise<void>;
}
18 changes: 18 additions & 0 deletions yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand All @@ -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"
Expand Down