【问题标题】:How to find if users exist in Firebase?如何查找用户是否存在于 Firebase 中?
【发布时间】:2017-01-28 08:05:07
【问题描述】:

我正在使用 Firebase Web SDK,并且知道检查集合中是否存在子项的方法。但是,我找不到一个简洁的例子来检查用户的存在,而不尝试让他/她登录。

export class Email extends Component {
  constructor (props) {
    super(props)
    this.state = { email: '' }
  }

  checkEmail () {
    const { email } = this.state
    FirebaseApp.auth()
    .createUserWithEmailAndPassword(email, uuid.v4())
    .then((User) => Actions.Password({ User }))
    .catch((error) => {
      // Handle Errors here.
      console.log('firebase', {error})
      this.setState({error})
      // ...
    })
    // Actions.password()
  }

  render () {
    return (
      <View style={{ ...styles.onboarding, backgroundColor: 'purple' }}>
        <View style={{ borderBottomWidth: 3, borderColor: 'black', padding: 10 }}>
          <TextInput ref='email'
            style={styles.input}
            keyboardType='email-address'
            placeholder='Your email'
            value={this.state.email}
            onChangeText={(email) => this.setState({email: email.toLowerCase()})} />
        </View>
        {!this.state.error ? null
          : <Text>
            {this.state.error.message}
          </Text>
        }
        <TouchableOpacity onPress={() => this.checkEmail()} style={{ ...styles.button, margin: 20, borderColor: 'black' }}>
          <Text style={styles.buttonText}>Next</Text>
        </TouchableOpacity>
      </View>
    )
  }
}

你有什么可以重复使用的代码吗?

【问题讨论】:

  • 你有你要查询的用户的用户id吗?
  • 不,我想在创建用户之前检查存在性
  • 您的数据库中的用户 ID 是由 OAuth 自动生成的,还是由用户选择/自定义的?
  • 显示您的 JSON(作为文本,无屏幕截图)以及您已经尝试过的内容。
  • 用户 id 应该是普通的电子邮件,因为我最近加入了 firebase 开发。这就是我尝试过的如此火,更新。

标签: reactjs firebase react-native firebase-realtime-database firebase-authentication


【解决方案1】:

目前,由于它符合我的应用安全策略和设置,我创建了一个按 id 精确索引的电子邮件集合。

firebase.ref(`emails/${user.email}`).set(true)

【讨论】:

    猜你喜欢
    • 2019-01-04
    • 1970-01-01
    • 2017-02-17
    • 1970-01-01
    • 2018-11-27
    • 1970-01-01
    • 2021-02-26
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多