Tuesday, 11 April 2017

Make Firebase Initialization available everywhere in Express/Node.js

I want to use Firebase for an Express App with multiple files. I got e.g. a routes folder where I want to handle e.g. sign in requests with Firebase auth. Now I have Firebase initialized in my app.js file but I cannot access Firebase Auth within my other files in. If I try to initialize it anywhere else I get the error that the reference to Firebase already exists, but I have no idea how to access it. My code looks like this:

app.js:

(...)

// FIREBASE SETUP

const firebase = require('firebase');

const config = {
    apiKey: "",
    authDomain: "",
    databaseURL: "",
    projectId: "",
    storageBucket: ""
    messagingSenderId: ""
  };

firebase.initializeApp(config);

const auth = firebase.auth(); 

(...)

routes/signup.js:

const express = require('express');
const firebase = require('firebase');
const router = express.Router();

// SIGNUP ROUTES

router.get('/', function(req, res, next) {

    res.render('signup');

});

router.post('/', function(req, res, next) {

    const promise = auth.createUserWithEmailAndPassword(req.body.email, req.body.pass).then(function () {

        (...)

    });

    promise.catch(e => console.log(e.message));

});

module.exports = router;

Thanks! :)



via Julian C

No comments:

Post a Comment