I am facing a problem in sending or retrieving the data from the Firebase web.
Although I can successfully register the user and I can even get the user UID by writing the code shown below-
if(user!=null)
{
var uid = firebase.auth().currentUser.uid;
console.log(`UID ===> ${uid}`)
}
But still I cannot retrieve/send data from/to the Firebase. I am not even getting any error in the console.
My codes for index.js file in the functions folder is below-
const functions = require('firebase-functions');
const express = require('express');
var admin = require('firebase-admin');
var serviceAccount = require('/path/to/my/serviceAccount/key.json');
admin.initializeApp({
credential: admin.credential.cert(serviceAccount),
databaseURL: 'https://urlOfMyDatabaseURL.firebaseio.com'
});
const app = express();
app.get('/', (request, response)=>{
response.send(index.html);
});
exports.app=functions.https.onRequest(app);
Please help, I am stuck here!
Related
What is the best way to use the Firebase admin SDK in jest script to get Firestore information?
I am unable to use the admin SDK in my jest scripts. Running admin.firestore() throws:
Total timeout of API google.firestore.v1.Firestore exceeded 600000 milliseconds before any response was received.
My code:
let issueRefundWix: HttpsCallable<unknown, unknown>,
initApp = async () => {
const serAcc: ServiceAccount = await import(GOOGLE_APPLICATION_CREDENTIALS);
const app = initializeApp(firebaseConfig);
const functions = getFunctions(app, "us-central1");
const db = getFirestore(app);
const auth = getAuth(app);
admin.initializeApp({
credential: admin.credential.cert(serAcc),
storageBucket: "gs://**.appspot.com/",
});
return {
app: app,
functions: functions,
db: db,
auth: auth,
};
};
beforeAll(async () => {
const app = await initApp();
const functions = app.functions;
issueRefundWix = httpsCallable(functions, "issueRefundWix");
db = admin.firestore();
uid = await createWixUser();
exampleSubscription.wixId = uid;
console.log("Finished beforeAll()");
});
This set up works fine when I am running tests on the Firebase Emulator. Can I test functions the same on the emulator as when they are deployed? All tests work fine if I remove any dependencies on admin.firestore().
What is the best way to use the admin SDK to query firestore in jest tests?
Thank you
Is there a way to add admin sdk to an app that's initialized by client firebase already? When I tried to initialize both, it throws an error saying that I can't initialize default app twice. I need client side firebase for login etc, but admin for managing users, both in the same app/website.
What I did so far:
const firebaseConfig = {
...
};
const app = initializeApp(firebaseConfig);
const auth = getAuth(app);
const db = getFirestore(app);
var admin = require("firebase-admin");
admin.initializeApp({
'credentrial': ...
}); //error
var serviceAccount = require("./api_key.json");
var admin_auth = admin.auth();
export { auth, db, admin_auth };
Thanks!
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);
})();
when I try to access firebase storage via admin SDK in firebase function, it fails on admin.storage(), however succeed on admin.database(), any suggestion?
const functions = require('firebase-functions');
const admin = require('firebase-admin');
const serviceAccount = require("./myapp-ea8ed-firebase-adminsdk-140ib-682f0b6fce.json");
admin.initializeApp({
credential: admin.credential.cert(serviceAccount),
databaseURL: "https://myapp-ea8ed.firebaseio.com"
});
in the function I can access the firebase database by
const ref = admin.database().ref("/root");
but fail on access firebase storage by
const bucket = admin.storage().bucket("myapp-ea8ed.appspot.com");
When I deploy storage trigger functions I get the following error msg and the deployment fails:
Deployment error.
Insufficient permissions to (re)configure a trigger (permission denied for bucket Bucket-Name. Please, give owner permissions to the editor role of the bucket and try again.
Firestore trigger functions can be deployed. I already tried to fix the ACLs using the commands suggested here and here.
Any ideas how to fix that?
Edit: I set up a new project, facing the same error.
My /functions/index.js:
const functions = require('firebase-functions');
const admin = require('firebase-admin');
const serviceAccount = require("./serviceAccountKey.json");
admin.initializeApp({
credential: admin.credential.cert(serviceAccount),
databaseURL: "https://<project-name>.firebaseio.com"
});
const storage = admin.storage();
exports.helloWorld = functions.https.onRequest((request, response) => {
response.send("Im deployed successfully");
});
exports.storageTrigger = functions.storage
.object()
.onChange(event => console.log('I fail to deploy'));