I was implementing contacts in flutter. In implementing that I was succesfull and also uploaded contacts succesfully to firestore. than I was trying to get uid of user in firebase. for that I tried the code below but when I run that it gives the following error on credentials,
null check cannot be on null value
var credential = await Credentials.applicationDefault();
var projectId = "com.example.myapp";
await FirebaseAdmin.instance.initializeApp(
AppOptions(
credential: credential!,
projectId: projectId,
storageBucket: '$projectId.appspot.com')
).auth().getUserByPhoneNumber(chPhones?[chPhones!.length].phones?.first.value.toString() ?? "")
.then((userRecord) => print("userUID=" + userRecord.uid))
.catchError((error) => print(error));
Related
Using the Firebase CLI, I'm executing arbitrary JavaScript that's stored in a file. In that script, I'm trying to make calls to admin.auth().something(), but it doesn't work against the Firebase Emulator. This is in contrast to making calls to Firestore, which works perfectly fine with the Emulator.
Firestore (everything works)
GCP
This makes calls to Firestore on GCP and it succeeds:
const admin = require('firebase-admin');
admin.initializeApp({ projectId: 'my-project' });
const db = admin.firestore();
(async () => {
const widget = await db.doc('/widgets/123456789').get();
console.log(widget.data().name);
})();
Emulator
This also succeeds:
const admin = require('#firebase/testing');
const db = admin
.initializeAdminApp({ projectId: 'my-project' })
.firestore();
(async () => {
const widget = await db.doc('/widgets/123456789').get();
console.log(widget.data().name);
})();
Firebase Auth (GCP works but Emulator does not)
GCP
This makes calls to Firebase Auth on GCP and it succeeds:
const admin = require('firebase-admin');
admin.initializeApp({ projectId: 'my-project' });
(async () => {
const user = await admin
.auth()
.getUser('user123456789');
console.log(user.email);
})();
Emulator
This fails:
const admin = require('#firebase/testing');
const auth = admin
.initializeAdminApp({ projectId: 'my-project' })
.auth();
(async () => {
const user = await auth.getUser('user123456789');
console.log(user.email);
})();
The error message is:
C:\Users\...\node_modules\#firebase\testing\node_modules\#firebase\component\dist\index.cjs.js:134
throw e;
^
[t [Error]: Your API key is invalid, please check you have copied it correctly.] {
code: 'auth/invalid-api-key',
a: null
}
I'm not sure what API key they're referring to, as the request is against the Emulator. How can I execute Firebase Auth requests against the Emulator using Firebase CLI?
To send requests against Firebase Emulator Auth, set the following environment variables and use the standard Firebase Admin SDK (firebase-admin) instead of using #firebase/testing:
process.env.FIREBASE_AUTH_EMULATOR_HOST = 'localhost:9099';
process.env.FIRESTORE_EMULATOR_HOST = 'localhost:8080';
const admin = require('firebase-admin');
admin.initializeApp({ projectId: 'emulator projectId' });
Now this works:
(async () => {
const user = await auth.getUser('user123456789');
console.log(user.email);
})();
I was trying out flutter cloud firestore in my web project.
I'm on master channel and i'm using the following version in pubspec.yaml file.
cloud_firestore: ^0.13.5
Index.html file :
<script src="https://www.gstatic.com/firebasejs/7.14.2/firebase-app.js"></script>
<script src="https://www.gstatic.com/firebasejs/7.14.2/firebase-auth.js"></script>
<script src="https://www.gstatic.com/firebasejs/7.14.2/firebase-firestore.js"></script>
<script>
// TODO: Replace the following with your app's Firebase project configuration.
// See: https://support.google.com/firebase/answer/7015592
var firebaseConfig = {
apiKey: "--apiKey--",
authDomain: "--authDomain--",
databaseURL: "--databaseURL--",
projectId: "--testapp--",
storageBucket: "--test.appspot.com--",
messagingSenderId: "--senderId--",
appId: "--appId--"
};
// Initialize Firebase
firebase.initializeApp(firebaseConfig);
In My List Screen, I'm fetching data like :
var reference = widget.firestore.collection('work');
print("reference : ${reference}");
try {
QuerySnapshot snapshot = await reference.getDocuments();
print("work snapshot : ${snapshot}");
} catch (error) {
print("work error : ${error}");
}
But, I'm getting an error while trying to fetch data from cloud firestore in my flutter web project.
Error :
Expected a value of type 'JSObject<qc>', but got one of type 'NativeJavaScriptObject'
Can anyone please suggest a workaround solution.
Thanks.
The reason why the error thrown is because you're trying to assign QuerySnapshot when reference.getDocuments() returns a different value type. To fetch the documents inside the Collection, the get() method should be used.
CollectionReference reference = FirebaseFirestore.instance.collection('work');
QuerySnapshot snapshot = await reference.get();
I am trying to use react-native-firebase to do Phone Auth. Firebase is correctly set-up. I can get SMS code back for some phone numbers. I am using verifyPhoneNumber method. My code looks like this:
firebase
.auth()
.verifyPhoneNumber(phoneNumber)
.on('state_changed', phoneAuthSnapshot => {
console.log(phoneAuthSnapshot);
switch (phoneAuthSnapshot.state) {
phoneAuthSnapshot contains below:
{verificationId: null, code: null, error: null, state: "verified"}
Now, since both verificationId and code are NULL, I cannot build credentials:
const credential = firebase.auth.PhoneAuthProvider.credential(
verificationId,
codeInput
);
Since, I cannot build credentials, I cannot get the user details:
firebase
.auth()
.signInWithCredential(credential)
.then(user => {
console.log('PHONE AUTH USER ->>>>>', user.toJSON());
this.setState({ user: user.toJSON() });
})
How can I resolve this?
I'm using firebase facebook for my app. I just want user's public profile so that I can create user in my database. I do not want firebase full authentication just the profile detail but I am getting the following error.
Here is some code
const firebaseConfig = {
apiKey: FIREBASE_KEY,
authDomain: FIREBASE_AUTH_DOMAIN,
databaseURL: FIREBASE_DATABASE_URL,
projectId: FIREBASE_PROJECT_ID,
storageBucket: FIREBASE_STORAGE_BUCKET
}
export const Firebase = firebase.initializeApp(firebaseConfig);
async loginWithFacebook(navigate){
const { type, token} = await Expo.Facebook.logInWithReadPermissionsAsync(FACEBOOK_APP_ID, {
permissions: ['public_profile'],
});
if (type == 'success') {
const provider = new Firebase.auth.FacebookAuthProvider();
const credential = provider.credential(token);
Firebase.auth().signInWithCredential(credential)
.then((user) => {})
.catch((error) => {
});
}
}
I'm very new in react native please let me know what I'm doing wrong here.
Change your code to the following:
const credential = Firebase.auth.FacebookAuthProvider.credential(token);
Firebase.auth().signInWithCredential(credential)
credential is a static method on firebase.auth.FacebookAuthProvider.
I am having trouble to register a user in Firebase with his Facebook credentials in RN. The Facebook and the Firebase apps are setup correctly, since everything is working as expected in Swift. The OAuth redirect URI is also setup correctly.
However when I try to do the same process from RN it fails. My setup is the following, when the user taps my login button I call FB's LoginManager.logInWithReadPermissions(['public_profile', 'email']) and on its success I call FirebaseManager's (which is my custom class for managing Firebase) signUpWithFacebook(). I get a FB access token correctly and I can see that a credential object is created.
However, Firebase always returns the error:
{"error":{"errors":[{"domain":"global","reason":"invalid","message":"A system error has occurred"}],"code":400,"message":"A system error has occurred"}}
The FirebaseManager looks like this:
import { AsyncStorage, NativeModules } from "react-native";
import * as firebase from 'firebase';
const firebaseConfig = {
apiKey: "key",
authDomain: "domain",
databaseURL: "db_url",
projectId: "project_id",
storageBucket: "storage_bucket"
};
const firebaseApp = firebase.initializeApp(firebaseConfig);
const FBSDK = require('react-native-fbsdk');
const {
AccessToken
} = FBSDK;
export default class FirebaseManager {
constructor() {
// Some logic...
}
signUpWithFacebook() {
AccessToken.getCurrentAccessToken().then((data) => {
let accessToken = data.accessToken
console.log('FB accessToken: ' + accessToken)
const provider = new firebase.auth.FacebookAuthProvider();
const credential = provider.credential(accessToken);
console.log('FB credential: ' + credential)
firebase.auth().signInWithCredential(credential)
.then()
.catch((error) => {
console.log('Failed to sign in Firebase with Facebook. ' + error)
})
})
}
}
I would also like to note that Firebase's anonymous & email/password authentication are working with no problem.
My workaround for the moment is to do the Facebook login from Swift and return the user object in RN with a bridge and RN's NativeModules.
Some more info about my project:
react-native: 0.44.2
firebase: 4.0.0
react-native-fbsdk: 0.6.0
Any help would be appreciated!
Might be a long shoot but this looks VERY similar to a an issue in firebase JS SDK. The solution was to not make an instance of FacebookAuthProvider, by changing these lines:
const provider = new firebase.auth.FacebookAuthProvider();
const credential = provider.credential(accessToken);
to this:
var credential = firebase.auth.FacebookAuthProvider.credential(accessToken);