Ask Your Question
2

How to use Node.js with Firebase to generate a new user?

asked 2023-06-20 22:41:14 +0000

pufferfish gravatar image

edit retag flag offensive close merge delete

1 Answer

Sort by ยป oldest newest most voted
0

answered 2023-06-20 22:47:01 +0000

scrum gravatar image

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.

edit flag offensive delete link more

Your Answer

Please start posting anonymously - your entry will be published after you log in or create a new account. This space is reserved only for answers. If you would like to engage in a discussion, please instead post a comment under the question or an answer that you would like to discuss

Add Answer


Question Tools

Stats

Asked: 2023-06-20 22:41:14 +0000

Seen: 11 times

Last updated: Jun 20 '23