Skip to content

Latest commit

 

History

History
96 lines (83 loc) · 3.43 KB

cloudfunctions.md

File metadata and controls

96 lines (83 loc) · 3.43 KB

Cloud functions

Install dependencies:

To get started and get the firebase functions tools installed please consider the following documentation: https://firebase.google.com/docs/functions/get-started

Creating cloud functons with HTTPS requests using express:

  1. Import the packages we will need:
const functions = require("firebase-functions");
const admin = require("firebase-admin");
const express = require("express");
const cors = require("cors");
  1. Initalize the firebase pipeline:
admin.initializeApp( functions.config().firebase );
  1. We need to now tell the app that we want to use express built in json capabilities:
app.use( express.json() );
  1. If calling the functions for web, we must enable cors:
app.use( cors() );
  1. Creating a cloud function:
app.post( route , async (req, res) => {
}

route is where the request will be made from the client side. req is the incoming request. res is what is to be sent back to the client side.

  1. If req is from web we must declare that we are okay with the origin of the request. So inside our cloud functions we must declare:
res.set('Access-Control-Allow-Origin', '*');
res.set('Access-Control-Allow-Methods', 'POST');
res.set('Access-Control-Allow-Headers', 'Content-Type, Authorization');
  1. Getting parameters from the client side request:
const requestBody = req.body;
const email = requestBody.email;
const pass = requestBody.pass;
  1. Returning the response to the client side:
return res.status( 500 ).send( { 'error': "This is indicating an error being sent back to sender" } );
return res.status( 200 ).send( { 'success': "This is indicating that the cloud function did what it was supposed to with no errors" } );
  1. Declaring the cloud functions name
exports.cloudFunctionName = functions.https.onRequest(app);
  1. Deploying the cloud functions( must be in functions directory )
firebase deploy --only functions
  1. Calling from the client side application( Dart ):
            String displayStatement = "";

           // Hide before push
           String url = "exampleUrl";
           

           final Map<String, dynamic> requestBody = {
               'email': email,
               'password': password,
           };

           final http.Response response = await http.post(
               Uri.parse( url ),
               headers: <String, String>{
               'Content-Type': 'application/json; charset=UTF-8',
               },
               body: jsonEncode( requestBody ),
           );

           if( response.statusCode == 200 ) 
           {
              // Success
              
          } 
           else
           {
               Map<String, dynamic> errorSpecs = json.decode( response.body )['error'];

               String errorMessage = errorSpecs['code'];
               
              //DO SOMETHING WITH THE ERROR
           }

Conclusion

This was an example of post requests cloud functions using https. If you want to do the other kinds of requests, you can use the app.get and others but it might change the structure of the cloud function. The example simply sends a users email and password through the req object where it is then used in the cloud function.