9

So Firebase offers something called "functions", which is essentially a nodejs server that has all of the Firebase stuff preconfigured and has all the scaling automatically handled. I'm wondering, is there a way to call a function inside of the "functions" index.js file from an angular 2 app?

I need to utilize the firebase-admin npm module to check if a user's email exists and then grab the uid for that user, if it does.

According to this link, I can setup my index.js file such as:

const functions = require('firebase-functions');
const admin = require('firebase-admin');
admin.initializeApp(functions.config().firebase);

// I'm actually not sure if this is how you do this part:
exports.getUserByEmail = (email) => {
  return admin.auth().getUserByEmail(email);
}

Is there a way I can call getUserByEmail() inside of a component in my Angular 2 app?

Thanks in advance!

AL.
  • 36,815
  • 10
  • 142
  • 281
Stevie Star
  • 2,331
  • 2
  • 28
  • 54
  • You could call a function - to do whatever you want - via HTTP. You likely want to allow only calls from authenticated users, so have a look at: https://github.com/firebase/functions-samples/tree/master/authorized-https-endpoint – cartant Apr 06 '17 at 01:31
  • I appreciate this, but even from this example, I'm still not entirely sure what URL to make my request to and how to call the function via the http call. Any help there? – Stevie Star Apr 06 '17 at 01:39
  • My understanding is that it's a project-ID-based URL. Have a look at: https://github.com/firebase/functions-samples/tree/master/quickstarts/time-server#try-the-sample – cartant Apr 06 '17 at 01:50
  • Ah to utilize the cloud functions like this, do I have to upgrade my plan to something that isn't spark in order to do this? – Stevie Star Apr 06 '17 at 02:00
  • My understanding is that you only need to upgrade if you want to access non-Google domains from within the function. – cartant Apr 06 '17 at 02:18

2 Answers2

10

There are two primary ways of invoking a Cloud Function directly from client code.

You can use a database trigger which responds when some location in your Firebase project's Realtime Database changes.

You can also use an HTTP trigger which response when you access an HTTP endpoint. For a web app, you use whatever method you want to invoke an XHR transaction.

Whichever one you use is up to the architecture of your app, and to some degree your preference. There are plenty of samples of both, and more, in the provided sample code.

You can definitely use the Firebase admin SDK to access your project from within your function code. Many of the samples do exactly that.

Doug Stevenson
  • 297,357
  • 32
  • 422
  • 441
3

Firebase just released a new SDK on March 20 which allows you to call HTTPS trigger like function from the client side and the best part is it checks the user auth so you can restrict the functions to be only called by authenticated users and easily acces their account details such as email,uid,name etc. See more at: https://firebase.google.com/docs/functions/callable

Can
  • 1,646
  • 9
  • 13