Firebase用户已通过身份验证,为什么拒绝上传到存储?

问题描述 投票:0回答:1

在本机项目中,我正在使用[[both react-native-firebasefirebase sdk。react-native-firebase不允许使用Firebase存储上传图像Blob,这就是为什么我使用香草Firebase javascript SDK进行此操作的原因。为了区别起见,在我的代码和本文中,我将firebase javascript sdk标识为“ FIREBASE”,将react-native-firebase标识为“ firebase”。

我必须初始化我的firebase应用程序(尽管react-native-firebase不需要此功能,firebase则需要,App.js构造函数和导入:

import * as React from 'react'; import AppNavigation from './src/navigation'; import { Provider } from 'react-redux'; import { store, persistor } from './src/store/index.js'; import firebase from 'firebase/app'; import { PersistGate } from 'redux-persist/integration/react'; export default class App extends React.Component { constructor (props) { super(props); const firebaseConfig = { apiKey: '{apiKey}', authDomain: 'project-ID.firebaseapp.com', databaseURL: 'https://project-ID.firebaseio.com', projectId: 'project-ID', storageBucket: 'project-ID.appspot.com', messagingSenderId: '9999999999' }; if (!firebase.apps.length) { firebase.initializeApp(firebaseConfig); } }

我在一个操作中实现了Firebase 

FIREBASE(用于auth / firestore的firebase,用于存储的FIREBASE:]import * as types from '../actions/types'; import RNFetchBlob from 'rn-fetch-blob'; import firebase from 'react-native-firebase'; import * as FIREBASE from 'firebase/app'; import 'firebase/storage'; import { Platform } from 'react-native'; const Blob = RNFetchBlob.polyfill.Blob; const fs = RNFetchBlob.fs; export const registerUser = (registration) => { const { email, pass, username, img } = registration; return (dispatch) => { dispatch({ type: types.REGISTER_USER }); console.log('starting registration process...'); // check username is unique firebase .firestore() .collection('users') .where('username', '==', username) .get() .then((querySnapshot) => { if (querySnapshot.empty !== true) { // back to registration form registrationFail(dispatch, 'Username already taken. Try again.'); console.log("Registrant's username already exists"); } else { console.log('Registrants username is unique'); // continue with registration firebase .auth() .createUserWithEmailAndPassword(email, pass) .then((userCredential) => { // successful user creation, now authenticated // write to img storage uploadImg(dispatch, img, userCredential.user.uid) .then((imgUrl) => { // on success, write to firestore uploadImgSuccess(dispatch, 'Profile image upload successful...'); // write rest of data to firestore firebase .firestore() .collection('users') .add({ createdAt: firebase.firestore.FieldValue.serverTimestamp(), username: email, uid: userCredential.user.uid, profileImg: imgUrl, email: email, }) .catch((err) => { console.log('Registration failed. Error: ' + err.message); registrationFail(dispatch, err.message); }); } }) .catch((err) => { // Image Profile NOT Uploaded uploadImgFail(dispatch, err); }); }) .catch((err) => { // unsuccessful user creeation registrationFail(dispatch, err.message); }); } }) .catch((err) => registrationFail(dispatch, err.message)); }; }; const uploadImg = async (dispatch, uri, uid, mime = 'image/png') => { console.log('Starting image upload...'); dispatch({ type: types.UPLOAD_IMG, info: 'Uploading profile image...' }); const uploadUri = Platform.OS === 'ios' ? uri.replace('file://', '') : uri; let uploadBlob = null; // let downloadPath = ''; const imageRef = FIREBASE.storage().ref(uid).child('profileImg'); fs .readFile(uploadUri, 'base64') .then((data) => { return Blob.build(data, { type: `${mime};BASE64` }); }) .then((blob) => { uploadBlob = blob; return imageRef.put(blob, { contentType: mime }); }) .then(() => { uploadBlob.close(); return imageRef.getDownloadURL(); }) .then((url) => { console.log('Returning Download URL: ' + url); uploadImgSuccess(dispatch, 'Image upload successful...'); }) .catch((err) => { uploadImgFail(dispatch, 'Image upload failed: ' + JSON.stringify(err)); }); };
但是当我通过uploadImg()时,出现错误:

{ "code_": "storage/unauthorized", "message":"Firebase Storage: User does not have permission to access 'someReference/someChild', "serverResponse":{"Code":403, "message": "permission denied."} }

这里是Firestore规则:

rules_version = '2'; service cloud.firestore { match /databases/{database}/documents { match /{document=**} { allow read, write: if request.auth != null; } } }

这里是存储规则:

rules_version = '2'; service firebase.storage { match /b/{bucket}/o { match /{allPaths=**} { allow read, write: if request.auth != null; } } }

我不知道发生了什么或为什么。在react-native-firebase的createUserWithEmailAndPassword()中对用户进行身份验证,甚至可以将数据上传到Firestore。我唯一的猜测是,这可能与同时使用Firebase和FIREBASE或与我使用FIREBASE设置Firebase的方式有关。我在先前的测试项目中一起使用了这两个工具,并且在分支项目rn-fetch-blobreact-native-fetch-blob的维护版本)的帮助下成功地工作了,但是我没有用于测试的安全规则,因此。 

解决这个问题有什么想法吗?

javascript firebase firebase-storage react-native-firebase
1个回答
0
投票
[是,您猜对了,因为FIREBASE处理本机方面,并且FIREBASE只是JS,所以firebase实例不知道firebase正在执行身份验证。因此,这两个实例都有自己的生活和具有特定属性的潜在客户来标识用户并提供授权。

要解决此问题,请尝试通过Vanilla JS SDK授权用户,或使用rn-firebase来完成整个任务。我建议使用react-native-firebase,它对整个Firebase堆栈都具有良好的支持。https://rnfirebase.io/docs/v5.x.x/storage/reference/storage

希望这会有所帮助!干杯!

© www.soinside.com 2019 - 2024. All rights reserved.