My code shows only undefined. I want it to show user name, profile image, and user email. Thanks for help in advance.
var firebaseConfig = {
//My Configuration
};
firebase.initializeApp(firebaseConfig);
var mainUser = firebase.auth().currentUser;
var userNameId, userEmailId, photoUrl, uid, emailVerified;
if (mainUser != null) {
userNameId = user.displayName;
userEmailId = user.email;
photoUrl = user.photoURL;
emailVerified = user.emailVerified;
uid = user.uid; // The user's ID, unique to the Firebase project. Do NOT use
// this value to authenticate with your backend server, if
// you have one. Use User.getToken() instead.
}
document.write(userNameId);
document.write(userEmailId);
document.write(photoUrl);
document.write(emailVerified);
document.write(uid);
Your user variable is not defined. Change the mainUser to user instead:
var firebaseConfig = {
apiKey: "AIzaSyCfX0ye2p4W8Jq2Za3H0_nRpgtFAlGxFxA",
authDomain: "trendy-cart-79671.firebaseapp.com",
databaseURL: "https://trendy-cart-79671.firebaseio.com",
projectId: "trendy-cart-79671",
storageBucket: "trendy-cart-79671.appspot.com",
messagingSenderId: "563099031291",
appId: "1:563099031291:web:1fe8d3c43e2dfd8429425e"
};
firebase.initializeApp(firebaseConfig);
var user = firebase.auth().currentUser;
var userNameId, userEmailId, photoUrl, uid, emailVerified;
if (user != null) {
userNameId = user.displayName;
userEmailId = user.email;
photoUrl = user.photoURL;
emailVerified = user.emailVerified;
uid = user.uid; // The user's ID, unique to the Firebase project. Do NOT use
// this value to authenticate with your backend server, if
// you have one. Use User.getToken() instead.
}
document.write(userNameId);
document.write(userEmailId);
document.write(photoUrl);
document.write(emailVerified);
document.write(uid);
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 wanted to create a login with facebook button, so i created this code:
import { getAuth, signOut,
onAuthStateChanged, FacebookAuthProvider, signInWithPopup } from "https://www.gstatic.com/firebasejs/9.0.2/firebase-auth.js";
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"
};
const app = initializeApp(firebaseConfig);
const auth = getAuth(app);
// login with facebook
const facebookProvider = new FacebookAuthProvider();
const signInWithFacebook = document.querySelector('#facebook-icon');
signInWithFacebook.addEventListener('click', (e) => {
e.preventDefault();
signInWithPopup(auth, facebookProvider).then((result) => {
const user = result.user;
const credential = FacebookAuthProvider.credentialFromResult(result);
const accessToken = credential.accessToken;
})
.catch((error) => {
const errorCode = error.code;
const errorMessage = error.message;
const email = error.email;
const credential = FacebookAuthProvider.credentialFromError(error);
});
});
when i try to login with facebook it gives me this error message:
Facebook has detected that firebase sign in isn't using a secure connection to transfer information.
can you help to solve this problem, please?
For these tests you must run them with an SSL connection, if you are testing on localhost you are probably not using SSL.
im getting error while connecting my html code to firebase database
const firebaseConfig = {
apiKey: "AIzaSyBSu7dx2w4GY6_HiPYyv0Gj_xDddoPa8hI",
authDomain: "assistant2-139ff.firebaseapp.com",
databaseURL: "https://assistant2-139ff-default-rtdb.firebaseio.com",
projectId: "assistant2-139ff",
storageBucket: "assistant2-139ff.appspot.com",
messagingSenderId: "888617709384",
appId: "1:888617709384:web:a325963680f27cccea4d7d",
measurementId: "G-W9NKQV6N45"
};
// Reference messages collection
var messagesRef = firebase.database().ref('Login Details');
// Listen for form submit
document.getElementById('contactForm').addEventListener('submit', submitForm);
// Submit form
function submitForm(e){
e.preventDefault();
/* // Get values
var name = getInputVal('fullname');
var name = getInputVal('email');
var password = getInputVal('password');
// Save message
saveMessage(fullname, email, password);*/
console.log(123);
}
I'm trying to connect a user to the user collection in firestore. And after then,I'm trying to redirect login page.But not work.
I have implemented this page for reference
How do I link auth users to collection in Firestore?
var express = require('express');
const firebase = require('firebase/app');
require("firebase/auth");
require("firebase/database");
require("firebase/firestore");
var router = express.Router();
var moment = require('moment');
var firebaseConfig = {
apiKey: "hogehoge",
authDomain: "hogehoge.firebaseapp.com",
databaseURL: "https://hogehoge.firebaseio.com",
projectId: "hogehoge",
storageBucket: "hogehoge.appspot.com",
messagingSenderId: "hogehoge",
appId: "hogehoge"
};
// Initialize Firebase
firebase.initializeApp(firebaseConfig);
let db = firebase.firestore();
let auth = firebase.auth();
/* GET home page. */
router.get('/', function(req, res, next) {
res.render('register', { title: 'register page' });
});
router.post('/', function(req, res, next) {
var userName = req.body.user_name;
var email = req.body.email;
var password = req.body.password;
var createdAt = moment().format('YYYY-MM-DD HH:mm:ss');
var promise = auth.createUserWithEmailAndPassword(email, password);
// If there is any error stop the process.
promise.catch(function (error) {
var errorCode = error.code;
console.log(`GOT ERROR: ` + errorCode)
});
// When no errors create the account
promise.then(function () {
var userUid = auth.currentUser.uid;
db.collection('users').doc(userUid).set({
email: email,
name: userName,
password: password,
created_at: createdAt
});
res.redirect('/login');
});
});
module.exports = router;
If I don't write a redirect,
//res.redirect('/login');
registration is successful, but page is timeout.
So I'm writing a redirect process(not comment out), but this time the registration process is not done.
How can I redirect after registration is complete?
I'm not really sure but I would say that you have to fulfill your promise of your db.collection('users').doc(userUid).set(...). So try this:
promise.then(function () {
var userUid = auth.currentUser.uid;
db.collection('users').doc(userUid).set({
email: email,
name: userName,
password: password,
created_at: createdAt
}).then(function () {
res.redirect('/login');
});
});
var config = {
apiKey: "A**********************kBs",
authDomain: "the**********.firebaseapp.com",
databaseURL: "https://***********.firebaseio.com",
projectId: "the-o***********nette",
storageBucket: "the***********.appspot.com",
messagingSenderId: "6***********6"
};
firebase.initializeApp(config);
$("#register").click(
function() {
var email=$("#regEmail").val();
var password=$("#regPass").val();
var fname=$("#regfName").val();
var lname=$("#reglName").val();
firebase.auth().createUserWithEmailAndPassword(email, password)
.catch(function(error) {
// Handle Errors here.
var errorCode = error.code;
var errorMessage = error.message;
alert(errorMessage)
});
alert( "you have successfully registered");
// Get a reference to the database service
var database = firebase.database();
var userId = firebase.auth().currentUser;
firebase.database().ref('users/' + userId).set({
fname: fname,
lname: lname,
email: email,
})
.catch(function(error) {
var errormsg=error.message;
console.log(errormsg);
});
}
);
The createuserwithemailandpassword is working properly. But I am not able to add anything into the database. No error log is reported. The var is mentioned in the HTML file. Let me know if anything else is needed. thanks!
You are passing the full user object instead of the uid. Please try this:
var userId = firebase.auth().currentUser.uid;
firebase.database().ref('users/' + userId).set();