0
votes

I trying to delete an account in firebase using REST API, axios and React Native?

Look at my code:

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.: Maybe, I'm puting the wrong FIREBASE_ID_TOKEN

Where can I get the right FIREBASE_ID_TOKEN?

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

Thanks!

1
Hi @Gustavo, I think you should just create a chain to that axios request using catch, and spout out the error. And you'll find the problem. If you still do need help then I would gladly help! P.S. Oh I just noticed you're using axios the wrong way. Check out the docs for example. - Irfandy Jip

1 Answers

0
votes

The ID token you need to use comes from User#getIdToken(). This function returns a Promise containing the user's ID token that you then send off to the Identity Toolkit API.

Make sure to replace [API_KEY] with the Web API Key that you'll find on the project settings page.

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
    });
}