How do I add Firebase Admin Auth into React app .env?

喜欢而已 提交于 2020-04-17 21:17:06

问题


Good question,

to add the google auth from firebase for admin access, first you need the .json file from firebase, you can get it from your admin panel where you have auth setup

go to the service account generate and download the JSON

use it in your file how you wish... example my admin route

//Use dotenv to read .env vars into Node
require('dotenv').config();

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

// Schema import
const postModel = require('../models/postModel');
const userModel = require('../models/userModel');

// parse json to use in all requests got or sent by router .. which is provided by express
router.use(express.json());

// Initialize the default app

var admin = require('firebase-admin');

admin.initializeApp({
    credential: admin.credential.cert({
  "type": process.env.FIREBASE_TYPE,
  "project_id": process.env.ID,
  "private_key_id": process.env.FIREBASE_PRIVATE,
  "private_key": process.env.FIREBASE_PRIVATE_KEY.replace(/\\n/g, '\n'),
  "client_email": process.env.FIREBASE_CLIENT_EMAIL,
  "client_id": process.env.FIREBASE_CLIENTID,
  "auth_uri": process.env.FIREBASE_AUTHURI,
  "token_uri": process.env.FIREBASE_TOKEN,
  "auth_provider_x509_cert_url": process.env.FIREBASE_AUTH_PROVIDER,
  "client_x509_cert_url": process.env.FIREBASE_FIREBASE_CLIENT
}),
databaseURL: process.env.FIREBASE_DATABASE
});





console.log(process.env.ID)


router.post('/', (req, res, next) => {
    const admin_uid = req.body.uid;

    if (admin_uid === process.env.ADMIN_URI) {
        // // users email

        const email = req.body.email;

        admin
            .auth()
            .getUserByEmail(email)
            .then(function(userRecord) {
                // See the UserRecord reference doc for the contents of userRecord.
                console.log('Successfully fetched user data:', userRecord.toJSON());
                // res.json(userRecord.toJSON())

                admin.auth().updateUser(userRecord.uid, { disabled: true }).catch((error) => res.json(error));
            })
            .catch(function(error) {
                console.log('Error fetching user data:', error);
                // res.json(error)
            });

        // delete the posts of the user and the user

        postModel.deleteMany({ author: email }, (error, returnedDocuments) => {
            if (error) return next(error);

            userModel.findOneAndDelete({ email: email }, (error, returnedDocuments) => {
                if (error) return next(error);
                res.json(`User  ${email} deleted ...Posts from ${email} deleted`);
            });
        });
    } else {
        res.json('failure, admin ops');
    }
});

module.exports = router;

in your .env , it has to be in the frontend or else react cannot get the keys so you will need 2 .env, one for client one for the server, in the react .env do this

good luck, add comment for improve if you feel inclined.

来源:https://stackoverflow.com/questions/60517933/how-do-i-add-firebase-admin-auth-into-react-app-env

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!