Ask Your Question

Revision history [back]

click to hide/show revision 1
initial version

To use Node.js with Firebase to generate a new user, follow these steps:

  1. Install firebase-admin and firebase modules:
npm install firebase-admin firebase --save
  1. Initialize Firebase in your Node.js project:
const admin = require('firebase-admin');

const serviceAccount = require('/path/to/serviceAccountKey.json');
const firebaseConfig = require('/path/to/firebaseConfig.json');

admin.initializeApp({
  credential: admin.credential.cert(serviceAccount),
  databaseURL: firebaseConfig.databaseURL
});
  1. Create a function to generate a new user:
async function createNewUser(email, password) {
  try {
    const userRecord = await admin.auth().createUser({
      email: email,
      password: password,
    });
    console.log('Successfully created new user:', userRecord.uid);
    return userRecord;
  } catch (error) {
    console.error('Error creating new user:', error);
    return null;
  }
}
  1. Call the function with the desired email and password:
const email = 'testuser@example.com';
const password = 'examplepassword';

createNewUser(email, password)
  .then((userRecord) => {
    // Handle successful user creation
  })
  .catch((error) => {
    // Handle error on user creation
  });

This will create a new user with the specified email and password in your Firebase project.