reactjs - Firebase 错误:用户无权访问
问题描述
我正在开发一个 snapchat 克隆,我想使用在网络摄像头上单击的图像存储在 firestore 中并将其用作预览。该代码是完美的,但它显示了与 firebase 相关的错误。我不知道该怎么做。这是我的代码
import "./Preview.css"
import { resetCameraImage, selectCameraImage } from './../features/cameraSlice';
import { useSelector, useDispatch } from 'react-redux';
import { useHistory } from 'react-router';
import CloseIcon from "@material-ui/icons/Close";
import TextFieldsIcon from "@material-ui/icons/TextFields";
import CreateIcon from "@material-ui/icons/Create";
import NoteIcon from "@material-ui/icons/Note";
import MusicNoteIcon from "@material-ui/icons/MusicNote";
import AttachFileIcon from "@material-ui/icons/AttachFile";
import CropIcon from "@material-ui/icons/Crop";
import TimerIcon from "@material-ui/icons/Timer";
import SendIcon from "@material-ui/icons/Send";
import { v4 as uuid } from "uuid";
import { db, storage } from "./firebase";
import firebase from 'firebase';
function Preview() {
const cameraImage = useSelector(selectCameraImage);
const history = useHistory();
const dispatch = useDispatch();
useEffect(() => {
if (!cameraImage) {
history.replace('/');
}
}, [cameraImage, history]);
const closePreview = () => {
dispatch(resetCameraImage());
}
const sendPost = () => {
const id = uuid();
const uploadTask = storage.ref(`posts/${id}`).putString(cameraImage, "data_url");
uploadTask.on('state_changed', null, (error) => {
// error function
console.log(error);
},
() => {
// complete function
storage.ref('posts').child(id).getDownloadURL().then((url) => {
db.collection('posts').add({
imageUrl: url,
username: "PAPA React",
read: false,
//profilePic,
timestamp: firebase.firestore.FieldValue.serverTimestamp(),
});
history.replace('/chats');
});
}
);
};
return (
<div className="preview">
<CloseIcon className="preview__close" onClick={closePreview}/>
<div className="preview__toolbarRight">
<TextFieldsIcon />
<CreateIcon />
<NoteIcon />
<MusicNoteIcon />
<AttachFileIcon />
<CropIcon />
<TimerIcon />
</div>
<img src={cameraImage} alt="" />
<div className="preview__footer" onClick={sendPost} >
<h2>Send Now</h2>
<SendIcon fontSize="small" className="preview__sendIcon" />
</div>
</div>
)
}
export default Preview
抛出错误
我的 Firebase 规则设置
替代方法上的相同错误
替代方法上的相同错误
解决方案
该错误表示用户无权访问您的 Firebase 存储。为 Firebase 存储添加规则以授予用户访问权限。例如:
service firebase.storage {
match /b/{bucket}/o {
match /{allPaths=**} {
allow read, write;
}
}
}
推荐阅读
- amcharts - 如何在amcharts雷达图中使标签弯曲
- c++ - 你有关于 cakewalk .wrk 数据格式的文档吗?
- rust - 不发出简单结构的创建事件
- flutter - 为什么小部件关闭时颤动图像没有释放内存
- processing - 对象释放另一个较小的对象?
- laravel - 如何在 Laravel 登录中检查附加条件?
- python - 如何使用 If Else (Python) 过滤不同的参数
- function - 有没有办法排除在 ElasticSearch 中之前的 function_scores 中匹配的文档?
- javascript - 如何在 JS 的 url 中将多个 Id 作为参数传递?
- python - 生成的熊猫数据框附加到excel表