我想限制对 Firebase 数据库的访问,以便只有我授权的用户才能对其进行写入。但几乎所有地方提出的解决方案似乎都不适合我。
我的控制台中总是出现 401(未经授权)错误。
我尝试了两种方法来检查正确的用户是否登录,但没有一种对我有用:
1. uid硬编码在规则中:
{
"rules": {
".read": true,
".write": "auth.uid === 'UID'")",
}
}
Run Code Online (Sandbox Code Playgroud)
2. 数据库中的uid
{
"rules": {
".read": true,
".write": "root.child('users').hasChild(auth.uid)",
}
}
Run Code Online (Sandbox Code Playgroud)
在这两种方式中,我都使用了 Firebase-Authentication 概述中提供的 uid。我使用 Google 作为登录提供商。
google-authentication firebase firebase-authentication firebase-realtime-database
我正在尝试将子组件的状态传递给App.js文件。为此,我想使用新的上下文API。
但是我读到这是行不通的,因为如果值动态更改,则提供者必须是使用者的父母。
就我而言,我不需要动态更改它,我只想将状态传递给使用者。子组件的状态不会更改,因此如果以后不更改也不会有问题。
是否可以将状态从传递BlogPost.js到App.js?如果有使用上下文API的替代方法,那也可以。
我的代码:
App.js:
import BlogPost from './containers/BlogPost/BlogPost';
import { MediaContext } from './containers/BlogPost/BlogPost.js'
class App extends Component {
render() {
return (
<div>
<BlogPost />
<MediaContext.Consumer>
{value =>
console.log(value)}
</MediaContext.Consumer>
</div>
);
}
}
export default App
Run Code Online (Sandbox Code Playgroud)
BlogPost.js:
应通过此组件的状态。如果我在React.createContext('hello')其中定义上下文,则可以。但是我不能在那里定义状态。
export const MediaContext = React.createContext();
class BlogPost extends Component {
state = {
title: "title",
image: {
src: "link",
alt: "alt,
credits: "Unsplash",
},
text: "Text Text Text",
media: { …Run Code Online (Sandbox Code Playgroud)