firebase.auth().createUserWithEmailAndPassword(email, password).then((user) => {
user.sendEmailVerification().then(()=>{console.log("Email Sent")}).catch((err)=>{console.log(err)});
}).catch((error) => {
var errorCode = error.code;
var errorMessage = error.message;
});
Getting error - user.sendEmailVerification() is not function.
Can someone help how to send Verification Email in Firebase Authentication?
I faced the same issue and resolved now.You followed something that is from older versions.The latest library doesn't have the same code u using,please see the latest documentation:
https://firebase.google.com/docs/auth/web/manage-users
Related
I'm new to React Native and I have an issue with Cloud Functions.
getAuthor(uid){
var getUser = firebase.functions().httpsCallable('getUser');
console.log('success');
getUser({uid: uid}).then(function(result) {
console.log('getUser called')
var user = result.data.uid;
return(
result
)
})
.catch(function(error) {
var code = error.code;
var message = error.message;
var details = error.details;
});
}
When I run this code, 'success' is printed but 'getUser called' is never printed. I take this to mean getUser is never called. But I have followed the Firebase guide 'Call functions from your app' and it seems to be the same. Is it a problem with Cloud Functions never being initialised or something? In the Firebase guide it says to initialise an instance of Cloud Functions by adding
var functions = firebase.functions();
but when I added it to config.js I got an error so I skipped this step. Sorry if this seems obvious, I have never used React Native or Firebase before. Any help would be greatly appreciated!
Edit: the function has been deployed, as can be seen in this screenshot
This is the function, by the way:
exports.getUser = functions.https.onCall((data, context) => {
const uid = data.uid;
auth.getUser(uid)
.then(function(UserRecord) {
console.log('USER RECEIVED:' + UserRecord.email.toJSON());
return {email : UserRecord.email.toJSON()};
})
.catch(function(error){
console.log(error);
});
Turns out I can't access user information with their UID! The solution is to add it into Firebase Database instead.
I have implemented google authentication on a web page using firebase sdk.
It works fine on chrome, firefox and edge, but on ie11 I get a google authentication page but the authenticated user information is always null.
I think it's supported in ie 11.
firebase.google.com/support/guides/environments_js-sdk
I added the polyfill by cdn and executed it but it doesn't work. User information is still null.
unpkg.com/core-js-bundle#3.0.1/minified.js
Why doesn't it only work with ie11?
*function signInGoogle() {
if (!firebase.auth().currentUser) {
var provider = new firebase.auth.GoogleAuthProvider();
provider.addScope('https://www.googleapis.com/auth/plus.login');
firebase.auth().signInWithRedirect(provider);
} else {
firebase.auth().signOut();
}
}
function initApp() {
firebase.auth().getRedirectResult().then(function (result) {
var user = result.user;
console.log(user); // always null
if (user) {
document.getElementById('createForm').submit();
}
}).catch(function (error) {
var errorCode = error.code;
var errorMessage = error.message;
var email = error.email;
var credential = error.credential;
// [START_EXCLUDE]
if (errorCode === 'auth/account-exists-with-different-credential') {
alert('You have already signed up with a different auth provider for that email.');
} else {
console.error(error);
}
});
}*
In order to reduce the SDK size, Firebase no longer ship the Polyfils that are required for some older browsers. This was one of the changes in the 6.x release.
Please take a look at https://firebase.google.com/support/guides/environments_js-sdk how to manually enable Polyfills as required.
Hello!
EDIT~
I've eventually managed to do this and wanted to share it just in case anyone else needs it. Most tutorials I've found were outdated and none of them seemed to work for me. But I've finally got everything to work so Here it is!
Sign Up -
(I've created a sign up form with input fields for the username, extra info, password and email)
Make sure you import all firebase scripts you want to use and ABOVE all of them, the firebase app main script. In my case I only needed
Auth & Database - And BELOW all of this you put your Firebase App config and import either an
external .js file where you'll be using firebase functions or write it
all down there. This was a very silly mistake I did myself and I kept getting errors on the console. This is because I've been
trying to call my external .js file BEFORE importing the firebase main
scripts, which makes no sense right?
So here's my .js file for the
sign up function:
//On a different .js file where I make use of most of my functions I've added this part
//(just because I've defined more const for all my functions and I wanted to have them all
//in one place):
//getting all elements -- I've only put in this example the ones that I've used for Sign Up
const signupBtn = document.getElementById("btnsignUp");
const txtEmail = document.getElementById('txtEmail');
const txtPassword = document.getElementById('txtPassword');
const userId = document.getElementById('txtName');
const discord = document.getElementById('txtDiscord');
const bday = document.getElementById('txtBday');
const gender = document.getElementById('txtGender');
const imgURL = document.getElementById('txtimgURL');
//getting references to the apps
const auth = firebase.auth();
const database = firebase.database();
const rootRef = database.ref('users');
//------------------------------------------------//
//firebase SIGN UP.js
signupBtn.addEventListener("click", function(){
var email = txtEmail.value;
var pass = txtPassword.value;
//Signing up
auth.createUserWithEmailAndPassword(email, pass)
.then(() => {
//send verification email
sendVerificationEmail();
})
.catch(function(error) {
// Handle Errors here.
//var errorCode = error.code;
var errorMessage = error.message;
alert("Error :" + errorMessage);
});
});
//verification email function
var sendVerificationEmail = () => {
auth.currentUser.sendEmailVerification()
.then(() => {
alert("Verification Email Sent! Check your mailbox.")
})
.catch(error => {
alert("Error :" + errorMessage);
})
}
//DATABASE
//'set' adds new data to he db
signupBtn.addEventListener('click', (e) => {
e.preventDefault();
rootRef.child(userId.value).set({
Email: txtEmail.value,
Discord: discord.value,
Gender: gender.value,
Birthday: bday.value,
ImgURL: imgURL.value,
CC: 0,//Here I've added some more info that's be stored too along with
RS: 0,//the data that the user has provided
Rupreets: 0,
Bag: 1,//1: small, 2: medium, 3: big
})
});
//And that's all!
In my case, what I did with the database part is something like this:
-App name-
|
+--Users:
|
+--username1
|
+-info1
|
+-info2
|
+-info2
|
+--username2
|
+-info1
|
+-info2
|
+-info2
Well, I hope this will help somebody else too n.n
welcome to Stack Overflow!
A couple of things:
You never actually call writeUserData in your code snippet; you just define it. If you don't call it, nothing will be written.
userId is never defined, so even if you called writeUserData, your database path would be be undefined. You'd need to get the userId from firebase.auth().currentUser.uid. For more on that, see this Firebase doc: https://firebase.google.com/docs/auth/unity/manage-users#get_a_users_profile .
-- Edit --
Here's a code sample. I haven't put in absolutely everything, just the relevant omissions:
//Signing up
firebase.auth().createUserWithEmailAndPassword(email, pass)
.then((data) => {
// createUserWithEmailAndPassWord returns a promise, which, when resolved will contain various user-related properties, so
let id = data.User.uid;
function writeUserData(userId, name, email, imageURL) {
firebase.database().ref('Users/' + userId).set({
Username: name,
Email: email,
Profile_pic: imageURL,
});
}
// call your function, referencing the id above
writeUserData(id, name, email, imageURL)
If the idea of promises and calling functions isn't comfortable, you might look at a Javascript learning resource like javascript.info
Good luck!
When I am logging in using web page the user is not signing in.I am actually working on firebase using javascript.If I am running same code through other project then it is working fine.
var google = document.getElementById("google_sign")
var facebook = document.getElementById("facebook_sign")
var linked = document.getElementById("linked_sign")
var login = document.getElementById("login_btn");
var email1 = document.getElementById("email")
var password1 = document.getElementById("password")
login.onclick=function login1(){
email = email1.value;
password = password1.value;
firebase.auth().signInWithEmailAndPassword(email,password)
.then(() => window.location.href = 'part-after-login.html')
.catch(function(error) {
// Handle Errors here.
var errorCode = error.code;
console.log(error.Message);
});
}
I'm currently using Firebase such that users sign in with their Google accounts to access the app. However, closing the page/opening the page in a new tab requires the user to sign in again. Is there a way to prevent users from having to re-login?
Here is the login code I am using (the example from the firebase docs)
function firebaseLogin() {
firebase.auth().signInWithPopup(provider).then(function(result) {
var token = result.credential.accessToken;
var user = result.user;
document.getElementById('user-name').innerHTML = user.displayName;
document.getElementById('propic').src = user.photoURL;
addClass(login, 'hidden');
removeClass(logout, 'hidden');
var snackbarData = {
message: 'Login Successful',
timeout: 2000
};
snackbarContainer.MaterialSnackbar.showSnackbar(snackbarData);
console.log(user);
reloadList();
}).catch(function(error) {
var errorCode = error.code;
var errorMessage = error.message;
var email = error.email;
var credential = error.credential;
var snackbarData = {
message: 'Login Unsuccessful',
timeout: 2000
};
snackbarContainer.MaterialSnackbar.showSnackbar(snackbarData);
console.error(error);
});
}
I'm not sure if this is the official way to do it, but I ended up saving the user returned from firebase.auth().currentUser in localStorage, and then checking if a user could be found in localStorage on document load. If a user is found, I just set firebase.auth().currentUser to equal the user from localStorage, and everything seemed to work as intended. (For example, if there wasn't an authenticated user, the client wouldn't be able to make calls to the database, but setting currentUser in this fashion allowed accessing the database.)