Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Firebase function context.auth and context.authType undefined

I am diving into Firebase functions (server-side), and having trouble grabbing the authentication information from within the function. In it's simplest form, my index.ts that is getting deployed to Firebase functions looks like this :

import * as functions from 'firebase-functions';
import * as admin from 'firebase-admin';

admin.initializeApp();
export const createOrganization = functions.firestore.document("Organizations/{organizationId}").
    onCreate((snap, context) => {
      console.log(context)
});

I can see from Firebase's logging that this function is indeed getting called, and the context that gets logged looks something like this :

{ eventId: 'XXXXX-XXXX-XXXX',
  timestamp: '2018-04-13T21:37:33.349247Z',
  eventType: 'google.firestore.document.write',
  resource: 
  { service: 'firestore.googleapis.com',
     name: 'projects/XXXX/databases/(default)/documents/Organizations/XXXXXXXXXXXX'
  },
  params: { organizationId: 'XXXXXXXXXXXX' } 
}

I would expect from the documentation that the context parameter would also include properties called "authType" and "auth" that contain information about the user who is making the request. However, after trying all sorts of things, context.authType and context.auth are always undefined.

I have tried this both as an authorized user and unauthorized user with no changes.

Any help figuring out why context.authType and context.auth are not populated would be really helpful, thanks!

like image 931
MinneapolChris Avatar asked Apr 13 '18 21:04

MinneapolChris


1 Answers

Auth information is only given for Firebase products that provide auth information to Cloud Functions for each triggered event. Currently, only Realtime Database supports this. Firestore support is being worked on. In theory, Cloud Storage could provide support as well.

It's inconsistent because each event provider in Cloud Functions for Firebase is driven by different teams, each with different implementations and priorities.

like image 193
Doug Stevenson Avatar answered Nov 10 '22 15:11

Doug Stevenson