【问题标题】:Deleting account/user in firebase using REST API使用 REST API 在 Firebase 中删除帐户/用户
【发布时间】:2020-03-11 04:02:07
【问题描述】:

我尝试使用 REST APIaxiosReact Native 删除 firebase 中的帐户?

看看我的代码:

import React, {Component} from 'react'
import {View, Text, TouchableOpacity, StyleSheet} from 'react-native'
import axios from 'axios'

class App extends Component {
  deleteAccount = () => {
    axios.post('https://identitytoolkit.googleapis.com/v1/accounts:delete?key=[API_KEY]', {
      "idToken":"[FIREBASE_ID_TOKEN]"
    })
  }

  render() {
    return(
      <View style={styles.container}>
        <TouchableOpacity onPress={this.deleteAccount}>
          <Text>Button</Text>
        </TouchableOpacity>
      </View>
    )
  }
}

const styles = StyleSheet.create({
  container: {
    flex: 1,
    alignItems: 'center',
    justifyContent: 'center'
  }
})

export default App

PS.:也许,我放错了 FIREBASE_ID_TOKEN

我在哪里可以获得正确的 FIREBASE_ID_TOKEN?

参考:https://firebase.google.com/docs/reference/rest/auth?hl=pt-br#section-delete-account

谢谢!

【问题讨论】:

  • 嗨@Gustavo,我认为你应该使用catch 创建一个指向axios 请求的链,然后输出错误。你会发现问题所在。如果您仍然需要帮助,那么我很乐意提供帮助! P.S. 哦,我刚刚注意到您以错误的方式使用 axios。例如查看文档。

标签: firebase react-native firebase-authentication axios ecmascript-next


【解决方案1】:

您需要使用的 ID 令牌来自User#getIdToken()。此函数返回一个包含用户 ID 令牌的 Promise,然后您将其发送到 Identity Toolkit API。

确保将[API_KEY] 替换为您可以在project settings 页面上找到的Web API 密钥

deleteAccount = () => {
  firebase.auth().currentUser.getIdToken(/* forceRefresh */ true)
    .then((userIdToken) => {
      return axios.post('https://identitytoolkit.googleapis.com/v1/accounts:delete?key=[API_KEY]', {
        "idToken": userIdToken
      });
    })
    .then(function (response) {
      console.log(response);
      // TODO: update UI that account was deleted
    })
    .catch(function (error) {
      console.log(error);
      // TODO: update UI that operation failed
    });
}

【讨论】:

    猜你喜欢
    • 2021-05-18
    • 2021-08-18
    • 2020-05-06
    • 1970-01-01
    • 2019-12-28
    • 1970-01-01
    • 1970-01-01
    • 2019-04-10
    • 2021-07-11
    相关资源
    最近更新 更多