Firebase v8 - how to access EmailAuthProvider when `firebase.default.auth` is undefined - javascript

I am using Firebase authentication with AngularFire2 on an Ionic-Angular project. I need to be able to reauthenticate my users when needed, and it seems like the only way to do so is by obtaining the user's credentials, which can only be obtained through firebase.auth.EmailAuthProvider, an object which isn't part of AngularFire, but only of firebase itself.
I am using firebase v8 for this, on which the EmailAuthProvider object should be found in firebase.default.auth.EmailAuthProvider. However, I get that firebase.default.auth is undefined, and am therefore unable to access EmailAuthProvider. The only solution I've found is to revert back to firebase v7, in which the latter can be successfully accessed through firebase.auth.EmailAuthProvider. I cannot upgrade yet to firebase v9 for other compatibility issues, nor want to keep v7 as it gives me loads of high and medium vulnerabilities.
EDIT
I obtain the credentials in this manner:
import * as firebase from "firebase/app";
...
firebase.default.auth.EmailAuthProvider.credential(email, password)
And I get the following error:
Uncaught TypeError: Cannot read properties of undefined (reading 'EmailAuthProvider')
My package.json file has "#angular/fire": "^6.1.5" and "firebase": "^8.10.0"
TLDR
In Firebase v8, how to access EmailAuthProvider, or more generally, how to reauthenticate a user?

Try changing your imports as shown below:
import firebase from "firebase/app";
import "firebase/auth" // <-- make sure you have auth imported
Then access EmailAuthProvider like this:
firebase.auth.EmailAuthProvider.credential(email, password)

Related

I cannot deploy firebase cloud functions: Failed to load function definition from source, No "exports" main defined in D:\

I'm trying to implement Firebase Storage into my Cloud Functions app script. I've been running into a problem where my code won't deploy at this point in the script:
const app = express();
app.engine('hbs',engines.handlebars);
app.set('views','./views');
app.set('view engine','hbs');
admin.initializeApp(functions.config().firebase);
I run into this error:
Error: Failed to load function definition from source: Failed to generate manifest from function source: Error [ERR_PACKAGE_PATH_NOT_EXPORTED]: No "exports" main defined in D:\Documents\ShowtapeLibraryAdmin\node_modules\firebase\package.json
I am using Firebase 9.14, Functions 4.1.0, and Google-Cloud/Storage 6.70 as well as Firebase/app 0.8.4.
I've tried updating firebase, checking the code from the tutorial I used for cloud functions as well as updating any other packages and NPM
Here is the full index.js: https://pastebin.com/Q8J0LzCh
I do apologise if I have not properly formatted this question correctly, this is my first Stack Exchange post!
Firebase uses a modular, functional syntax starting from version V9.0.0. Try refactoring the code as shown below:
const { initializeApp } = require('firebase/app');
It seems you are trying to use Firebase SDK in a Cloud Function so you should ideally use the Firebase Admin SDK itself and not the client SDK as well. Try updating to latest version of Firebase Admin SDK and refactoring the code as shown below:
const { initializeApp } = require('firebase-admin/app');
const { getStorage } = require('firebase-admin/storage');
const storage = getStorage();
Checkout the documentation for Firebase Admin for Cloud Storage for more information.

Connecting to Firebase Auth emulator using firebase-js-sdk in Node environment is throwing an error

I am using the 'firebase' client npm package to do client side testing (creating users with email and password), in a node.js environment.
I am following the example given in the docs...
https://firebase.google.com/docs/emulator-suite/connect_firestore#web-version-9
Following that example, my code is as follows...
const firebaseAuth = require('firebase/auth');
const auth = firebaseAuth.getAuth();
firebaseAuth.connectAuthEmulator(auth, 'http://localhost:9099');
And the exception being thrown is...
Uncaught Error FirebaseError: Firebase: Need to provide options, when not being deployed to hosting via source. (app/no-options).

How to emulate Firebase authentication without an API key

I'm building an open-source project using Firebase's JS SDK. My goal is to allow contributors to run the project locally using the Firebase emulator so that they don't need any real credentials. The Firebase emulator docs specify that "you can run the emulators without ever creating a Firebase project". That's exactly what I want!
After running firebase init, I wrote up the following code. It triggers a popup that allows users to sign in through GitHub:
import { initializeApp } from "firebase/app";
import { connectAuthEmulator, getAuth, GithubAuthProvider } from "firebase/auth";
const app = initializeApp({
projectId: "demo-project",
});
const auth = getAuth(app);
connectAuthEmulator(auth, "http://localhost:9099");
// When users sign in, we call the following method:
async function signIn() {
const githubAuth = new GithubAuthProvider();
await signInWithPopup(firebaseClientAuth, githubAuth);
}
The code above will trigger the following error:
Uncaught (in promise) FirebaseError: Firebase: Error (auth/invalid-api-key)
In the real world, I would call initializeApp() with an apiKey, but here I just want to emulate authentication. I've also tried not to call initializeApp() at all and call getAuth() without any arguments, but it triggers the same error.
Presumably, an API key requires creating a project, so is it actually possible to run the Firebase auth emulator without creating a Firebase project?
Yes it is.
A demo Firebase project has no real Firebase configuration and no live resources. These projects are usually accessed via codelabs or other tutorials.
Project IDs for demo projects have the demo- prefix.Firebase Docs
Run firebase emulators:start --project demo-[ANYTHING_YOU_WANT]
For example firebase emulators:start --project demo-test-project
is it actually possible to run the Firebase auth emulator without creating a Firebase project?
No, you need to have the emulator connected to a project. You'll notice in the setup instructions you are required to run firebase init to choose your project and the products you want to use in that project.

typeError: _ firebase_index_WEBPACK_IMPORTED_MODULE_17__.DEFUALT.collection is not a function

I have been receiving this error typeerror: _ firebase_index_WEBPACK_IMPORTED_MODULE_17__.DEFUALT.collection is not a function, and cannot figure out where it is coming from... I have firebase installed and am calling it in my file, but still receive this error... any ideas?
From the firebase docs:
Firebase App (the core Firebase SDK) is always required and must be listed first.
So, try and replace:
import * as firebase from 'firebase'
with
import firebase from "firebase/app";

Firebase Cloud Messaging - getToken() error in JS SDK

I am developing an app for KaiOS, a fork of the old Firefox OS, for which apps are based on HTML5 + JS. When compiling and running on device, Firebase Cloud Messaging stops working. I receive an error as follows.
TypeError: 'applicationServerKey' member of
PushSubscriptionOptionsInit could not be converted to any of:
ArrayBufferView, ArrayBuffer.
I've followed the instructions in the Firebase docs here as well as this tutorial on setting up FCM for Progressive Web Apps here. I can confirm I am setting my key before calling getToken, as shown below:
import firebase from "firebase";
const messaging = firebase.messaging();
messaging.usePublicVapidKey("<my-key-from-firebase-settings>");
messaging.getToken().then((currentToken) => {
...
}).catch((err) => {
...
});
I have searched for the above error, but to no avail. Does anyone know what this error means or how to fix it? Running the application in the browser on my development machine, everything works perfectly.
Note: the app is written in Typescript
Firebase SDK Version: 7.12.0
It might not be of help to you but I recently experienced a similar error message in a different environment:
Error: Failed to execute 'subscribe' on 'PushManager': The provided
value is not of type '(ArrayBuffer or ArrayBufferView)'
It was fixed in version 7.13.2 of Firebase:
https://github.com/firebase/firebase-js-sdk/issues/2712
https://firebase.google.com/support/release-notes/js#version_7132_-_april_2_2020

Categories