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);
        });
    }
@A2Asiimwe
Copy link

Good to learn!!

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