2023-05-30

Error while doing a POST call from Angular to Firebase Cloud Function

I've created a Firebase Cloud Function and it's working;

export const deleteUser = functions.https.onRequest(async (request, response) => {
  const userEmail = request.body.userEmail;
  await admin.auth().getUserByEmail(userEmail)
    .then(function (userRecord:any) {
      const uid = userRecord.uid;
      admin.auth().deleteUser(uid)
        .then( () => {
          console.log('User deleted');
          response.status(200).send('User deleted');
        })
        .catch(() => {
          console.log('Error deleting user');
          response.status(500).send('Failed to delete user');
        });
    })
    .catch(() => {
      console.log('Error fetching user');
      response.status(500).send('Failed while fetching user');
    })
})

I've checked with Postman and everythings is ok, it deletes the user from Firebase as expected. Screenshot of the call from Postman

I call my service from my component, and works fine.

removeUser(userUid:any, userEmail:any){
    userEmail = {'userEmail': userEmail};
    this.authService.gdelete(userEmail).subscribe();
    // this.usersService.removeUser(userUid);
  }

The problem is when I call the Cloud Funtcion from my Angular Service.

  gdelete(userEmail:any) {
    //no consigo conectar el post con la cloud function 
    const httpOptions = {
      headers: new HttpHeaders({
          'Content-Type': 'application/json', 
          'Access-Control-Allow-Origin': '*'
      })
    };
    return this.http.post('https://myfirebaseserver.cloudfunctions.net/deleteUser', {body: userEmail}, httpOptions);
  }

It displays the following error: Screenshot of the error in the browser console

I think the problem is when I call the cloud function. I've serched on SO lots of posts related and implemented the solutions but nothing worked.



No comments:

Post a Comment