You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Why am I getting a request.auth is undefined in this onCall function?
I am using Firebase Function emulator for this test.
I am trying to call a Firebase function directly from my app like so:
/functions/getStory.js:
import { getFirestore } from 'firebase-admin/firestore'
import { error, log } from 'firebase-functions/logger'
import { onCall, HttpsError } from 'firebase-functions/v2/https'
export const getStory = onCall({ cors: true }, async (request) => {
const db = getFirestore() // in v2, this is how you get Firestore instance
log('request.auth from client:', request.auth)
if (!request.auth) {
throw new HttpsError('unauthenticated', 'Request not authenticated')
}
return {
uid: request.auth.uid,
docId: request.data.docId,
text: 'Hello World from Server!'
}
})
The above function, when invoked, responds with the following:
i functions: Beginning execution of "us-central1-getStory"
> {"verifications":{"app":"MISSING","auth":"MISSING"},"logging.googleapis.com/labels":{"firebase-log-type":"callable-req quest-verification"},"severity":"DEBUG","message":"Callable request verification passed"}
> {"severity":"ERROR","message":"request.auth from client: undefined"}\n at
i functions: Finished "us-central1-getStory" in 3.5084ms
i functions: Beginning execution of "us-central1-getStory"
i functions: Finished "us-central1-getStory" in 1.4233ms
i functions: Beginning execution of "us-central1-getStory"
> {"verifications":{"app":"MISSING","auth":"VALID"},"logging.googleapis.com/labels":{"firebase-log-type":"callable-request-verification"},"severity":"DEBUG","message":"Callable request verification passed"}
> {"severity":"INFO","message":"request.auth from client: kRUx8ZxLoUd0y7kTcxgPfwTiGu13"}
i functions: Finished "us-central1-getStory" in 2.5383ms
Here is the client side code that invokes the cloud function:
Notice that the request.auth is undefined at an initial log point ("message":"request.auth from client: undefined"}, but then later resolves correctly. Why does this happen? It's like auth is not initialized yet until a later time. If that's the case, how do I properly check request.auth so I can do some authentication on the server to determine if the user is eligible to receive confidential data from Firestore when using the onCall method?
reacted with thumbs up emoji reacted with thumbs down emoji reacted with laugh emoji reacted with hooray emoji reacted with confused emoji reacted with heart emoji reacted with rocket emoji reacted with eyes emoji
-
Why am I getting a
request.auth
isundefined
in thisonCall
function?I am using Firebase Function emulator for this test.
I am trying to call a Firebase function directly from my app like so:
/functions/getStory.js
:The above function, when invoked, responds with the following:
Here is the client side code that invokes the cloud function:
/pages/test-oncall.vue
:The browser log for the above client code is:
Notice that the
request.auth
isundefined
at an initiallog
point ("message":"request.auth from client: undefined"}
, but then later resolves correctly. Why does this happen? It's like auth is not initialized yet until a later time. If that's the case, how do I properly checkrequest.auth
so I can do some authentication on the server to determine if the user is eligible to receive confidential data from Firestore when using theonCall
method?Beta Was this translation helpful? Give feedback.
All reactions