I'm trying to change the type of auth persistence based on the user choice.
I've tried to set the persistence to the type of browserSessionPersistence but it give me this error.
I'm on react native and I'm using firebase v9
TypeError: undefined is not an object (evaluating '_$$_REQUIRE(_dependencyMap[0], "#firebase/util").getModularInstance(auth).setPersistence')
Here is my import and the code where i set persistence
(auth is working fine and the code of setpersistence is run before login that works)
import {setPersistence,browserSessionPersistence} from 'firebase/auth';
setPersistence(browserSessionPersistence)
I tried to console log the value of browserSessionPersistence and I get that its undefined.
Some Firebase JavaScript SDK features are only compatible with browsers only.
browserSessionPersistence is only supported on web.
You should either use inMemoryPersistence or browserLocalPersistence within React Native.
Namespaced Version V8
Modular Version V9
firebase.auth.Auth.Persistence.LOCAL
browserLocalPersistence
firebase.auth.Auth.Persistence.SESSION
browserSessionPersistence
firebase.auth.Auth.Persistence.NONE
inMemoryPersistence
More details at https://firebase.google.com/docs/auth/web/auth-state-persistence#supported_types_of_auth_state_persistence
Related
I have a cloud function that changes a user's custom claim when a certain field changes.
I'm writing unit test for my app using firebase emulators. I need to manually change the user claim (since functions won't run in testing mode). I want to test that the database denies access to a user after his custom claims have been changed by the cloud function
thank you
#firebase/testing is deprecated and was last updated Oct 2020.
Use #firebase/rules-unit-testing instead:
For v8.10.0 of the Web SDK, use npm install #firebase/rules-unit-testing#1.3.15 (from Aug 2021, the last update before Firebase v9).
For v9.0.0 onwards, in both modular and namespaced modes, use npm install #firebase/rules-unit-testing.
Technically they used to be the same package, but one is getting new updates whereas the other isn't. Because they were built from the same code base, their APIs are similar when dealing with the legacy namespaced Firebase SDK. For all new code, you should use #firebase/rules-unit-testing alongside the modular SDK and modular test methods.
Based on the documentation:
import * as firebase from "#firebase/rules-unit-testing"; // note this "firebase" is not the normal firebase namespace!
const testApp = firebase.initializeTestApp({
projectId: "my-test-project",
auth: {
uid: "alice",
email: "alice#example.com",
isAdmin: true, // custom claim
isModerator: true // custom claim
}
});
If using the normal Firebase namespace in your code, import the test library with this instead:
import * as firebase from "firebase"; // for v8 or older.
import * as firebaseTest from "#firebase/rules-unit-testing";
I'm trying to use Firebase in my web project, and I can't understand how it works. I do:
import app from "firebase/app"; //1
import firebase from 'firebase'; //2
console.log(app === firebase); //3
it outputs true.
If I use 2 string my code to get data from firebase works and I get message about not using proper SDK in production
If I use 1 string I get an error from:
app.initializeApp(firebaseConfig);
const db = app.firestore();
Uncaught TypeError:
firebase_app__WEBPACK_IMPORTED_MODULE_0___default.a.firestore is not a
function
Please help me to sort out how to fix this.
I've got it, webpack is in charge. It wraps imported modules, that's why they are equal. Though, don't know how to optimize my module loads using webpack and firebase simultaneously.
I'm working on putting together a server-side Dart application that will run in App Engine. It needs to access a Firestore database, but I'm having trouble doing so.
The Dart packages I've tried are:
firebase_admin_interop
firebase
In both cases, I get errors like this when attempting to execute my code:
file:///root/.pub-cache/hosted/pub.dartlang.org/firebase_admin_interop-1.2.2/lib/src/database.dart:5:8: Error: Not found: 'dart:js'
import 'dart:js';
My vague understanding of this error is that it means the library has a dependency on running in a browser. However, I've not been able to find any way to interact with Firestore in a server-to-server configuration using Dart.
Is my only recourse here to use the Firestore REST API?
There is no Firebase Admin SDK for Dart. So indeed the REST API would be your best best.
Note that much of the Admin functionality is not exposed in documented REST API. So ymmv, and I'd seriously consider switching to a platform where you can use one of the official Admin SDKs.
If the number of search results on google are an indicator for popularity, then
Go, Node.js and C# are the languages most people use with the firestore admin libraries
Google searched input taken from https://cloud.google.com/firestore/docs/quickstart-servers
Go: "cloud.google.com/go/firestore" -> 3890 results
Node.js: "require('#google-cloud/firestore')" -> 2120 results
C#: "FirestoreDb.Create" -> 1110
Python: "from google.cloud import firestore" -> 859 results
Java: "import com.google.cloud.firestore.Firestore" -> 601 results
PHP: "Google\Cloud\Firestore\FirestoreClient" -> 1 result
I am a newbie in Firebase. I have read that it is good to unit test as you go. So this is what I have been trying to do lately. I currently have a problem when trying to test render on a class that uses Firebase. This is the following code I have been trying to fix:
import 'react-native';
import React from 'react';
import MainTab from '../../components/MainTab';
import Enzyme from 'enzyme';
import renderer from 'react-test-renderer';
it('renders correctly', () => {
const tree = renderer.create(
<MainTab/>
).toJSON();
expect(tree).toMatchSnapshot();
});
However, I am getting the following error on the test when trying to obtain the current user's id from firebase:
Has anyone stumbled into this error before? P.S. Don't go hard on me if this is something really vacuous, just trying to learn.
1. signInWithEmailAndPassword
signInWithEmailAndPassword(email, password) returns
firebase.Promise containing non-null firebase.User
Asynchronously signs in using an email and password.
This method will be deprecated and will be updated to resolve with a
firebase.auth.UserCredential as is returned in
firebase.auth.Auth#signInAndRetrieveDataWithEmailAndPassword.
For now, the implementation for this method is in below:
firebase.auth().signInWithEmailAndPassword(email, password).then(function(user) {
// user signed in
// - user.displayName
// - user.email
// - user.uid // <---- the user's unique ID
})
You can see firebase.User properties here:
https://firebase.google.com/docs/reference/js/firebase.User
You can read more details about this question here https://stackoverflow.com/a/39959002/3332734
2. For your case...
If you need to test the email/password authentication, I suggest you to use a firebase "dev" project and testing with user credentials for this dev firebase project.
Be careful, don't store your UID for other tests!
Firebase dont't provides a test for authentication yet.
3. Firebase beta to 1.0 version
Easier unit testing using a new firebase-functions-test npm module that simplifies writing unit tests.
See more about it in:
Launching Cloud Functions for Firebase v1.0
Firebase SDK for Cloud Functions Migration Guide: Beta to version 1.0
I hope I have been useful!
Im trying to implement authentication on my SPA, and it works fine.
The way im doing it is like in the docs:
import firebase from 'firebase/app';
import 'firebase/auth';
const config = {
// ...
};
const firebaseApp = firebase.initializeApp(config);
// and somewhere else in the code(with all the arguments and stuff)
firebase.auth().createUserWithEmailAndPassword();
firebase.auth().signInWithEmailAndPassword();
Now, my problem is that these imports add too much to the app. I honestly don't think that 180KB(minified, but not compressed) is acceptable for an SPA that aims to work on mobile.
Just for comparison, my whole app, Vuejs + Router + Vuex + other 3~ small libraries and the app logic weight 170KB(minified but not compressed).
So I wanted to know if there is another solution, or if im doing it wrong, or if there is an easy workaround. Ideally, I would be able to just make an HTTP request and get back a JWP.
Well, I contacted firebase to add this as a feature request, and they suggested me to use for now the REST API for auth. It requires a custom token, but it is a good solution.
The documentation for the REST API:
https://firebase.google.com/docs/reference/rest/auth/
As far as I know, the actual endpoints you hit for Firebase auth isn't publicly documented nor have I seen them around the Web. With that said, if you are set on reducing your bundle size, your best bet is to pick apart the SDK and figure out how and what you need to construct.
signInWithEmailAndPassword is defined here and as you can see, it just calls out to another private function and so on.