Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save toraritte/65b6ddf8cb0795dccd01c23bd000438a to your computer and use it in GitHub Desktop.
Save toraritte/65b6ddf8cb0795dccd01c23bd000438a to your computer and use it in GitHub Desktop.
Sending password reset email after user has been created with Firebase Admin SDK (Node.js)

Sending password reset email after user has been created with Firebase Admin SDK (Node.js)

1. Install Firebase's Node modules

Install the Firebase Node modules:

$ npm install firebase firebase-admin --save

2. Firebase Admin setup on Node.js REPL (see also: official guide)

Go to the Firebase console, click Project Settings (the little gear icon next to "Project Overview") and choose the Service Accounts tab. Node.js is selected by default, hit Generate new private key.

Following the instructions above the button (or in the official guide above) on the Node REPL:

    const firebase_admin = require('firebase-admin');
    const serviceAccount = require('./generated-service-account.json');

    firebase_admin.initializeApp({
        credential: firebase_admin.credential.cert(serviceAccount),
        databaseURL: "https://your-app.firebaseio.com"
    });

3. Firebase Client setup on Node.js REPL (see also: official guide)

    const firebase_client = require('firebase');

     var config = {
         apiKey: "your-api-key",
         authDomain: "your-app.firebaseapp.com",
         databaseURL: "https://your-app.firebaseio.com",
         projectId: "your-app",
         storageBucket: "your-app.appspot.com",
         messagingSenderId: "1234567"
     };

    firebase_client.initializeApp(config);

4. Example function to create user and send password reset email on creation

    function add_user(displayName, email) {

        firebase_admin.auth().createUser({
            displayName: displayName,
            email: email
        }).then( function(_userRecord) {
            firebase_client.auth().sendPasswordResetEmail(email);
        });
    }
@BhuvaneshwaranR
Copy link

Using firebase client SDK in the backend is not a good practice.

@shivam-rallyhealth
Copy link

Using firebase client SDK in the backend is not a good practice.

why? Can you elaborate on that?

@hsingh23
Copy link

hsingh23 commented Jan 9, 2023

perfect

@CodeNinja96x
Copy link

hi can you please add how we can send an email when we also have multiple tenant in our app using the same sendPasswordResetEmail() function

@AymaneChabat
Copy link

I don't know if anyone still needs to know but basically using the firebase client SDK in the backend will lead to many issues and one of the main ones is the client going offline, so basically in the staging phase with localhost the server will tend to stop if you don't send requests for a long time because the firebase client will go offline, I am talking from experience because I used to have those issues but once I switched to firebase admin in the backend, everything became much easier and smoother.

@A2Asiimwe
Copy link

Good to learn!!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment