0
votes

I wish to allow an anonymous user login to a Firebase Function.
Like so :

firebase.auth().signInAnonymously().catch(function(error) {
  // Handle Errors here.
  var errorCode = error.code;
  var errorMessage = error.message;
  // ...
});

From : https://firebase.google.com/docs/auth/web/anonymous-auth

The thing is, I don't know what the firebase object is.

What is the import statement I should use to get this firebase object ?


I've tried the following :

const firebase = require('firebase-app');

const firebase = require('firebase');

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

Nothing seems to have the auth().signInAnonymously() property.


I get errors like TypeError: firebase.auth is not a function or admin.auth(...).onAuthStateChanged is not a function in the Function log.


This is the function I want to call : https://firebase.google.cn/docs/reference/js/firebase.auth.Auth#signinanonymously

And this is the firebase object I want to import : https://firebase.google.cn/docs/reference/js/firebase

1
Or I get messages like Error: Cannot find the firebase namespace; be sure to include firebase-app.js before this library. when trying to do the firebase deploy - kris
Can you show more code ? auth function should exist on const firebase = require('firebase-admin'); - Nenroz
Where are you running this code? If you're using Node.js, please edit your question and add the appropriate tag node.js. If you're running in the browser, please indicate that by telling us what browser you're running when getting the error. - Heretic Monkey

1 Answers

1
votes

Backend services, such as Cloud Functions, are not used for signing in users. Sign-in always happens on the frontend, and the ID token for the user is managed there. If you need to make the backend aware of the user for some HTTP request, you're supposed to pass the ID token from the client to the server, then verify the ID token with the Firebase Admin SDK.

Cloud Functions server instances are transient and will not hold correctly and persistently hold the sign-in tokens, which is why you need to pass them from the client on each request.

I suggest reviewing the Firebase Authentication documentation for the frontend platforms you're working with in order to start getting sign-in implemented there.