Sign up using Firebase cloud functions and Typescript

Viewed 133

Im trying to write a Sign up cloud function in typescript. When i try to call .auth() on my firebase app, it gives me this compilation error Property 'auth' does not exist on type 'FirebaseNamespace'.ts(2339).

Heres the import: import { firebase } from "@firebase/app";

Heres the function:

app.post("/signup", async (req, res) => {
  try{
    const userJson =  {
      email: req.body.email,
      displayName: req.body.displayName,
      password: req.body.password,
      confirmPassword: req.body.confirmPassword,
    };
    const user = await firebase
      .auth()
      .createUserWithEmailAndPassword(userJson.email, userJson.password)
    const displayName: string = user.data()?.displayName;
    res.json({ message: `user ${displayName} succesfuly created` });
  } catch (error) {
    console.log(error);
    res.status(500).send(error);
  }
});

1 Answers

Looks like you're using the syntax from the client library. The cloud functions have a different format.

import * as admin from "firebase-admin";
admin
  .auth()
  .createUser({
    email: 'user@example.com',
    emailVerified: false,
    phoneNumber: '+11234567890',
    password: 'secretPassword',
    displayName: 'John Doe',
    photoURL: 'http://www.example.com/12345678/photo.png',
    disabled: false,
  })

See: https://firebase.google.com/docs/auth/admin/manage-users#create_a_user

Related