Firebase + React Native: Offline Authentication

I use Firebase in a React Native iOS application, mainly for storing user data and user authentication, which works great when the device actually has a working network connection .

When it comes to the standalone capabilities of Firebase, it looks like this:

Problem: users running the application without a network connection cannot do anything because they never logged in

Here are the steps to reproduce this behavior:

Step 1: A user with a disconnected user launches an application with a network connection

  • user clicks the "Login to Facebook" button
  • Firebase register using auth function
  • onAuthStateChanged (user)called with a registered user as parameter Lecture
  • received with the help user.getToken(), is sent to my server, which generates a user token ( generatedToken), which can be used to sign in Firebase auth with signInWithCustomToken (generatedToken)and therefore is stored in local storage
  • the user happily reads and writes to the Firebase database, the changes are instantly synchronized with the Firebase server.

Step 2: The recorded user starts the application with a network connection

  • the application implements there generatedTokenin local storage
  • generatedToken is used for firebase.auth().signInWithCustomToken (..)
  • (similar to step 1.3)
  • (similar to step 1.4)
  • (similar to step 1.5)
  • : (onAuthStateChanged (user) null user, ) Firebase
  • . Firebase.

3:

  • generatedToken
  • generatedToken firebase.auth().signInWithCustomToken (..)
  • firebase.auth().signInWithCustomToken (..) ,
  • onAuthStateChanged (user) null user
  • Firebase , -

  • persistenceEnabled true Objective-C/AppDelegate.m, FIRApp:

    (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
    {
      // ...
      [FIRApp configure];
      [FIRDatabase database].persistenceEnabled = YES;
      // ...
    }
    

    ( , ) , Firebase.

  • --- ---

!

+6
1

-sdk RN . , Firebase JS SDK native, , , :

Firebase Web SDK JS, ( ).

SDK firebase 2-3 , -SDK.

, -SDK Android ios. :

  • AdMob
  • FCM/Messaging
  • /
  • Firebase
  • Google Analytics
  • /SMS

sdk android/ios firebase js- (.. ).

, , :

react-native-firebase, , sdk api js , , sdk android ios firebase. jb firebase, , , , -sdk.

:

import firebase from 'react-native-firebase';

const instance = firebase.initializeApp({
  persistence: true
});

// can also use `keepSynced` / `setPersistence` methods:
// instance.database().ref('/someref').keepSynced();
// instance.database().setPersistence(true);

export default instance;
Hide result

( : --firebase)

+8

Source: https://habr.com/ru/post/1014492/


All Articles