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
Related
I am working on a signup form and I can create a new user using firebase auth, but the part where I register informations like the name, email... in a database is not working.
On the firebase side, I created a firestore database and a realtime database, and I don't see anything on them.
I tried to use the firebase documentation but it's still not working so I am probably missing something.
Thanks for your help!
PS: I hidded my firebase config here but it's filled on my code
import { initializeApp } from 'https://www.gstatic.com/firebasejs/9.15.0/firebase-app.js'
import { getAuth, createUserWithEmailAndPassword } from 'https://www.gstatic.com/firebasejs/9.15.0/firebase-auth.js'
import { getFirestore, doc, getDoc, getDocs, collection } from "https://www.gstatic.com/firebasejs/9.15.0/firebase-firestore.js";
import { getDatabase, ref, set } from "https://www.gstatic.com/firebasejs/9.15.0/firebase-database.js";
const firebaseConfig = {
apiKey: "",
authDomain: "",
databaseURL: "",
projectId: "",
storageBucket: "",
messagingSenderId: "",
appId: ""
};
const app = initializeApp(firebaseConfig);
//const db = getFirestore();
const auth = getAuth();
const db = getDatabase();
submitData.addEventListener('click', (e) => {
e.preventDefault()
var name = document.getElementById('full_name').value
var email = document.getElementById('email').value;
var password = document.getElementById('psw').value;
// do verification
// Move on with Auth
createUserWithEmailAndPassword(auth, email, password)
.then((userCredential) => {
const user = userCredential.user
alert("user created")
//add to firebase database
function writeUserData( name, email) {
set(ref(db, 'users/' + userId), {
username: name,
email: email,
});
}
})
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
i want to use firebase authentication, so i declared a constant named auth in my html body script and i wanted to use it in my auth.js file but it say it's not defined and i want to know how to solve the probleme.
this is the code i wrote:
this is the code in my html file:
<!-- Firebase -->
<script type="module">
// Import the functions you need from the SDKs you need
import { initializeApp } from "https://www.gstatic.com/firebasejs/9.0.2/firebase-app.js";
import { getAnalytics } from "https://www.gstatic.com/firebasejs/9.0.2/firebase-analytics.js";
import { getAuth, createUserWithEmailAndPassword } from "https://www.gstatic.com/firebasejs/9.0.2/firebase-auth.js";
import { getFirestore } from "https://www.gstatic.com/firebasejs/9.0.2/firebase-firestore.js";
// TODO: Add SDKs for Firebase products that you want to use
// Your web app's Firebase configuration
// For Firebase JS SDK v7.20.0 and later, measurementId is optional
const firebaseConfig = {
apiKey: "AIzaSyC3GLIN5TBmCDoTfy0dEOgOdvVvqNw-ric",
authDomain: "auth-project-38aaa.firebaseapp.com",
projectId: "auth-project-38aaa",
storageBucket: "auth-project-38aaa.appspot.com",
messagingSenderId: "431888894254",
appId: "1:431888894254:web:71bb9b250fbb8a21edd2bf",
measurementId: "G-6BBPCJ3814"
};
// Initialize Firebase
const app = initializeApp(firebaseConfig);
const analytics = getAnalytics(app);
const auth = getAuth(app);
const db = getFirestore(app);
</script>
<!-- JS -->
<script src="vendor/jquery/jquery.min.js"></script>
<script src="js/auth.js"></script>
and this is the code in my auth.js:
const signUpForm = document.querySelector('#register-form');
signUpForm.addEventListener('submit', (e) => {
e.preventDefault();
// get user info
const name = signUpForm['name'].value;
const email = signUpForm['email'].value;
const pass = signUpForm['pass'].value;
const rePass = signUpForm['re_pass'].value;
const agreeTerm = signUpForm['agree-term'].value;
console.log(name, email, pass, rePass, agreeTerm);
// sign up the user
auth.createUserWithEmailAndPassword(email, pass).then(cred => {
console.log(cred);
});
});
The auth.createUserWithEmailAndPassword is for the Firebase SDK v8 and earlier, or for compat mode in v9.
The new syntax is according to the documentation on creating a user:
createUserWithEmailAndPassword(auth, email, pass).then(cred => {
...
});
I am signing in user using mobile number and then i want him to enter email and password later. (after logging in)
I am trying to link email,password Auth method to current PhoneAuth method.
But it's giving me an error:
Cannot read property 'link' of undefined
Here is code:
import { firebaseAuth,fire, messaging } from '../../config/constants';
var credential = fire.auth.EmailAuthProvider.credential(email, password);
fire.auth.currentUser.link(credential).then(function(user) {
console.log("Account linking success", user);
}, function(error) {
console.log("Account linking error", error);
});
And in config/constants
import firebase from 'firebase'
require("firebase/firestore");
const config = {
apiKey: "AIzxxxxxxxxxxxxxxxxxxKXI",
authDomain: "payxxxxxxxxxxxxja1.firebaseapp.com",
databaseURL: "https://payxxxxxxxxxxja1.firebaseio.com",
projectId: "payxxxxxxxxxxxxja1",
storageBucket: "payxxxxxxxxxxxja1.appspot.com",
messagingSenderId: "281xxxxx5xxxxx6"
}
firebase.initializeApp(config)
export const fire = firebase
export const ref = firebase.database().ref()
export const firebaseAuth = firebase.auth
export const messaging = firebase.messaging();
I think something is wrong with fire.auth or fire.auth()
Thanks
link has been removed in favor of linkWithCredential starting with Firebase JS version 4.0.0: https://firebase.google.com/support/release-notes/js#4.0.0
Also make sure the current user is ready:
firebase.auth().onAuthStateChange(user => {
if (user) {
// currentUser is not null.
} else {
// currentUser is null.
}
});
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.