I'm using Firebase to authenticate a user. I have setup Firebase like so:
firebase.js
const firebase = require("firebase-admin")
const serviceAccount = require("./serviceAccountKey.json")
firebase.initializeApp({
credential: firebase.credential.cert(serviceAccount),
databaseURL: "https://logbook-96180.firebaseio.com/"
})
module.exports = firebase
I then import firebase in another file to authenticate a user.
auth.js
const firebase = require("./firebase")
...
const credential = firebase.auth.GoogleAuthProvider.credential(
null,
accessToken
)
firebase.auth().signInWithCredential(credential)
When I execute the authentication, I receive an error Cannot read property 'credential' of undefined, showing that firebase.auth.GoogleAuthProvider is undefined.
Are there any reasons as to why this could be the case? Thanks.
Related
I am trying to integrate firebase with ReactJs like this.
Here is my code
import firebase from "firebase";
var firebaseConfig = {
apiKey: "", // Add API Key
databaseURL: "" // Add databaseURL
};
// Initialize Firebase
firebase.initializeApp(firebaseConfig);
export const db = firebase;
var firepadRef = firebase.database().ref();
export const userName = localStorage.getItem('auth_name');
const urlparams = new URLSearchParams(window.location.search);
const roomId = urlparams.get("id");
if (roomId) {
firepadRef = firepadRef.child(roomId);
} else {
firepadRef = firepadRef.push();
}
export default firepadRef;
And now I get this warning:
It looks like you're using the development build of the Firebase JS
SDK. When deploying Firebase apps to production, it is advisable to
only import the individual SDK components you intend to use.
For the module builds, these are available in the following manner
(replace with the name of a component - i.e. auth, database,
etc):
CommonJS Modules: const firebase = require('firebase/app');
require('firebase/');
ES Modules: import firebase from 'firebase/app'; import
'firebase/';
Typescript: import firebase from 'firebase
After a new firebase.initializeApp, (assuming you are using v9 of firebase), you need to instantiate the db differently. Check your docs. I'd write your code like this instead:
// firebase file which you export the initialization
import firebase from "firebase";
var firebaseConfig = {
apiKey: "", // Add API Key
databaseURL: "" // Add databaseURL
};
// Initialize Firebase
var fire = firebase.initializeApp(firebaseConfig);
export fire.
//second js file
import firebase from ./firebase.js
import {getDatabase, ref, set} from "firebase/database";
var db = getDatabase(Firebase);
export const userName = localStorage.getItem('auth_name');
const urlparams = new URLSearchParams(window.location.search);
const roomId = urlparams.get("id");
//if you want to push to the db or something, you call the method
set(ref(db, 'users/' + userId), {
username: name,
email: email,
profile_picture : imageUrl
});
//more code here
All in all check the docs one more time. and use version 9, its implementation is the best.
https://firebase.google.com/docs/database/web/read-and-write#web-version-9_1
Hello i have big problem, the firebase realtime database dont save data
for example
const createUserProfile = (email,user,username) => {
console.log(email);
console.log(user)
const db = getDatabase();
console.log(db)
const dbRef = ref(db, 'users/' + user.uid);
set(dbRef, {
username,
email,
createDate: getDate()
});
};
In realtime conf i have write true and read true
what i can do? its next js
all data is okay,
console.log(db) response is
and realtime database dont update
This is probably because you don't pass the Firebase app instance to the getDatabase() method.
See here in the doc that you need to do
import { initializeApp } from 'firebase/app';
import { getDatabase } from "firebase/database";
// TODO: Replace with your app's Firebase project configuration
const firebaseConfig = {
apiKey: "API_KEY",
authDomain: "PROJECT_ID.firebaseapp.com",
// The value of `databaseURL` depends on the location of the database
databaseURL: "https://DATABASE_NAME.firebaseio.com",
// ...
};
const app = initializeApp(firebaseConfig);
// Get a reference to the database service
const db = getDatabase(app); // <= See how we pass the app object
I am trying to set authentication with Google in my web application. I did this type of thing a couple of times on Android apps, but now when i load my application it gives error "this.ta is not a function". Maybe its something silly but i can't detect the problem. Here is what i have:
api.js
import { db, storage, auth, google_provider } from './firebase'
sign_in() {
auth.signInWithPopup(google_provider).then(result => {
var token = result.credential.accessToken;
var user = result.user;
}).catch(error => {
var errorCode = error.code;
var errorMessage = error.message;
var email = error.email;
var credential = error.credential;
})
}
firebase.js
import * as firebase from 'firebase';
const app = firebase.initializeApp({
apiKey: "my-key",
authDomain: "domain",
databaseURL: "https://domain-url",
projectId: "name",
storageBucket: "bucket",
messagingSenderId: "id"
})
export const db = app.firestore()
export const storage = app.storage().ref()
export const auth = app.auth()
export const google_provider = firebase.auth.GoogleAuthProvider()
This is the error GoogleAuthProvider Error
Maybe i am wrong in the way i initialize the GoogleAuthProvider or the way i call the API. If somebody knows something i will appreciate anything that could tell me.
Regards.
firebase.auth.GoogleAuthProvideris a constructor.
To me it looks like you forgot to initialize it with new.
export const google_provider = new firebase.auth.GoogleAuthProvider()
In a Firebase Cloud Function running Express, I am attempting to set custom user claims when a client posts a token to a setCustomClaims route. When I call admin.auth().setCustomUserClaims(uid, {admin: true}) within that route, I get an error saying this is "not a function."
My authentication provider is the email/password provider via Firebase authentication (i.e. I am not creating custom tokens).
Do I have to be creating custom tokens to set custom user claims?
Here is my cloud function code:
const functions = require('firebase-functions');
const admin = require("firebase-admin");
import express from "express"
admin.initializeApp(functions.config().firebase);
const app = express()
app.post('/setCustomClaims', (req, res) => {
uid = "some-uid"
admin.auth().setCustomUserClaims(uid, {admin:true}).then(()=> {
res.end(JSON.stringify( { status: 'success' } ) );
})
});
export let api = functions.https.onRequest((request, response) => {
if (!request.path) {
request.url = `/${request.url}` // prepend '/' to keep query params if any
}
return app(request, response)
})
npm install firebase-admin#latest --save
firebase-admin#5.4.3 work, good luck for fun app.
Note: client needs this code
// Force token refresh. The token claims will contain the additional claims.
firebase.auth().currentUser.getIdToken(true);
In the new SDKs, you no longer instantiate a database references via new Firebase. Instead, you will initialize the SDK via firebase.initializeApp():
BEFORE
var ref = new Firebase("https://databaseName.firebaseio.com");
AFTER
// See https://firebase.google.com/docs/web/setup#project_setup for how to
// auto-generate this config
var config = {
apiKey: "apiKey",
authDomain: "projectId.firebaseapp.com",
databaseURL: "https://databaseName.firebaseio.com"
};
firebase.initializeApp(config);
var rootRef = firebase.database().ref();>
I have found same issue on the stackoverflow, check this: firebase.database is not a function
Iam using firebase admin but when I use exports like
var admin = require('firebase-admin');
var serviceAccount = require('./firebaseconfig.json');
admin.initializeApp({
credential: admin.credential.cert(serviceAccount),
databaseURL: "https://database.firebaseio.com"
});
module.exports = { admin : admin }
and used in another file by require like
var admin = require('../Firebaseconfig/firebase.js');
console.log(admin.database())
then gives error while starting the server
but if iam using admin.database() in the same file then Iam not getting any error.
error snippet:
console.log(admin.database())
TypeError: admin.database is not a function
at Object.
You exported an object containing admin. So you have to use admin.admin.database() :D
Or just export admin. module.exports = admin