AnyamBorogass
AnyamBorogass

Reputation: 451

React native firebase AsyncStorage warning?

I am developing a react native expo project with firebase. I installed firebase with: "yarn add firebase" and when I import firebase/auth with OnAuthChanged I get the following error:

AsyncStorage has been extracted from react-native core and will be removed in a future release. It can now be installed and imported from '@react-native-async-storage/async-storage' instead of 'react-native'. See https://github.com/react-native-async-storage/async-storage

I understand that firebase is storing my credentials locally with async and it is outdated and thats why I get this error. Now when I try to get rid of this error by installing: "yarn add @react-native-async-storage/async-storage" and changing the dependencies in the index.js to the correct async-storage package (https://stackoverflow.com/a/70122024/15845974) then my Warning dissapears.

The problem now is that my credentials are not saved, and onAuthChanges never triggers. I have to login each time I open my app.The correct version of async-storage is not saving my creds.

Any ideas on this one? :S

import { initializeApp } from 'firebase/app';
import { getAuth } from 'firebase/auth';

const firebaseConfig = {
...
  };  

const firebaseApp = initializeApp(firebaseConfig);
const auth = getAuth(firebaseApp);

onAuthStateChanged(auth, user => {userHandler(user)});

Upvotes: 7

Views: 4233

Answers (2)

Pratik Gondaliya
Pratik Gondaliya

Reputation: 430

Warning: @firebase/auth: Auth (10.7.1): You are initializing Firebase Auth for React Native without providing AsyncStorage.

I encountered the same warning and I resolved it using below method:

Before:

import {initializeApp} from 'firebase/app';
import {initializeAuth} from 'firebase/auth';

// Your web app's Firebase configuration
const firebaseConfig = {
  apiKey: '',
  authDomain: '',
  projectId: '',
  storageBucket: '',
  messagingSenderId: '',
  appId: '',
};

const app = initializeApp(firebaseConfig); 
const auth = getAuth(app);

export {auth};

After:

import {initializeApp} from 'firebase/app';
import {initializeAuth, getReactNativePersistence} from 'firebase/auth';
import ReactNativeAsyncStorage from '@react-native-async-storage/async-storage';

const firebaseConfig = {
  apiKey: '',
  authDomain: '',
  projectId: '',
  storageBucket: '',
  messagingSenderId: '',
  appId: '',
};

const app = initializeApp(firebaseConfig);
const auth = initializeAuth(app, {
  persistence: getReactNativePersistence(ReactNativeAsyncStorage),
});

export {auth};

I hope it helps. Thank you.

Upvotes: 1

Mike
Mike

Reputation: 41

This code removed the AsyncStorage warning for me

import AsyncStorage from '@react-native-async-storage/async-storage';
import {initializeApp, getApps, getApp} from 'firebase/app';

import {
  initializeAuth,
  getReactNativePersistence,
} from 'firebase/auth/react-native';

const firebaseConfig = {
  // Configuration
};


const app = getApps().length === 0 ? initializeApp(firebaseConfig) : getApp();


const auth = initializeAuth(app, {
  persistence: getReactNativePersistence(AsyncStorage),
});

export {auth};

Upvotes: 4

Related Questions