0.0.14 • Published 6 years ago

react-native-firebase-upload v0.0.14

Weekly downloads
39
License
UNLICENSED
Repository
github
Last release
6 years ago

react-native-firebase-upload

npm version

This package allows you to upload assets from React Native to Firebase Cloud Storage through a Firebase Cloud Function.

Note: Blob support has landed in React Native, you can now directly upload to Firebase Cloud Storage from the client.

Installation

yarn add react-native-firebase-upload express busboy raw-body in your functions dir.

yarn add react-native-firebase-upload in your React Native project.

Usage

Firebase Cloud Function

const admin = require('firebase-admin');
const functions = require('firebase-functions');
const upload = require('react-native-firebase-upload');

admin.initializeApp();

exports.upload = functions.https.onRequest(upload.handler);

// with authentication
exports.upload = functions.https.onRequest(upload.authHandler);

React Native

import upload from 'react-native-firebase-upload';
import { auth as fbauth, initializeApp, storage as fbstorage } from 'firebase';

initializeApp(/* .. */);

const auth = fbauth();
const storage = fbstorage();

auth.signInWithEmailAndPassword(/* .. */);
// or auth.signInAnonymously();
// or any other sign in method

const pick = async () => {
  try {
    // we are using Expo here
    const { cancelled, uri } = await ImagePicker.launchImageLibraryAsync();

    if (!cancelled) {
      let name = uri.split('/');
      name = name[name.length - 1];

      const token = await auth.currentUser.getIdToken(true); // true = forceRefresh

      const metadata = await upload({
        uri,
        name, // file name
        endpoint: 'FUNCTION_ENDPOINT',
        storage, // firebase.storage() ref.

        path: 'img/', // will store in `img` folder, defaults to root directory `/`
        token // pass the token if your endpoint requires authentication
      });

      console.log({
        downloadURL: metadata.downloadURLs[0],
        storageLocation: `gs://${metadata.bucket}/${metadata.fullPath}`
      });
    }
  } catch (err) {
    console.log(err.message);
  }
};
0.0.14

6 years ago

0.0.13

6 years ago

0.0.12

6 years ago

0.0.11

6 years ago

0.0.10

6 years ago

0.0.9

6 years ago

0.0.8

6 years ago

0.0.7

6 years ago

0.0.6

6 years ago

0.0.5

6 years ago

0.0.4

6 years ago

0.0.3

6 years ago

0.0.2

6 years ago

0.0.1

6 years ago