Email is not verified firebase - javascript

I am using Angular 9 and firebase. When a user signs up they get a verification email and the when they click it they are taken to a verification screen saying it has been verified. But they are getting an error when they do to login saying email is not verified. The page has to be reloaded for it to work.
onSignup(form: NgForm) {
this.submitted = true;
if (form.valid) {
this.authService.RegisterUser(this.signup.username, this.signup.password)
.then((res) => {
// Do something here
this.authService.SendVerificationMail();
this.router.navigateByUrl('/email-verification');
}).catch((error) => {
window.alert(error.message)
})
}
}
Here is the send verification mail from the auth service.
SendVerificationMail() {
this.ngFireAuth.onAuthStateChanged(function(user) {
user.sendEmailVerification();
});
}
Why is this happening and what should I do?

Related

How can I do e2e testing if I have passwordless sign up that sends OTP to the email?

I have a passwordless sign up flow which uses OTP code that is sent to users email.
Here is the function that handles the sign in/sign up:
// this verifies the OTP code
const handleSignInOTP = async () => {
toast.loading('Verifying OTP...');
const { data, error } = await supabaseClient.auth.verifyOtp({
email,
token: otp,
type: 'magiclink'
});
if (data) {
toast.dismiss();
toast.success('Successfully signed in!');
}
if (error) {
toast.dismiss();
toast.error(error.message);
}
};
At the moment I am completely lolst, how could I possibly use cypress to test something like that, is that even possible?

Firebase Email Verification is not working

In my code user is created but Email Verification is not working.
Here is my code:
const signup = () => {
auth.createUserWithEmailAndPassword(user.email, user.password)
.then((userCredential) => {
// send verification mail.
userCredential.user.sendEmailVerification();
//auth.signOut();
alert("Email sent");
})
.catch(alert);
}
You should wait for the sendEmailVerification promise to complete before showing the "Email sent" alert as it blocks the thread and prevents the sendEmailVerification from doing what it needs to.
const signup = () => {
auth.createUserWithEmailAndPassword(user.email,user.password)
.then((userCredential) => {
// send verification mail.
return userCredential.user.sendEmailVerification();
})
.then(() => alert("Email sent"))
.catch((err) => alert(err));
}
Note: Take care in where you call signup as the browser may be reloading the page before any of the above steps complete because you aren't chaining the Promises. Either return false to the caller (to prevent the form element submitting) or the Promise chain to the caller (to handle success/failure of the actions).

Firebase auth password reset

I'm working on a firebase app that contains an authentication system that allows users to sign up/login and create a profile. The app stores and calls profile info about the users. The system worked just fine until I realized there's no way for a user to reset their password. So I added sendPasswordResetEmail function and it's been downhill since then. I'm a newbie to firebase and to StackOverflow so forgive the mess you're about to see. Am I missing something in that function?
const resetpassword = async () => {
const email = document.getElementById('email').value;
try {
const { user } = auth.sendPasswordResetEmail(email);
alert('Password Reset Email Sent!');
}
catch(error) {
console.log("error ===>", error);
if (error.message === "Firebase: Error (auth/user-not-found).") {
alert("There is no user corresponding to this email address.")}
else (errorCode == 'auth/invalid-email') {
alert(errorMessage)}
}
}
If you are using firebase version 9 then use code snippet below to reset user password
import { getAuth, sendPasswordResetEmail } from "firebase/auth";
const auth = getAuth();
sendPasswordResetEmail(auth, email)
.then(() => {
// Password reset email sent!
// ..
})
.catch((error) => {
const errorCode = error.code;
const errorMessage = error.message;
// ..
});

Firebase Facebook authentication not returning email for some users

I run an Ionic 3 app with Firebase. Everything was fine until one of the users logged in and his email address was blank in the Firebase Authentication section. Does the code below need any additional permissions set for it to retreive the email address?
facebookLogin(): void {
this.facebook.login(['email']).then((response) => {
const facebookCredential = firebase.auth.FacebookAuthProvider
.credential(response.authResponse.accessToken);
firebase.auth().signInWithCredential(facebookCredential)
.then((success) => {
// console.log("Firebase success: " + JSON.stringify(success));
this.user = success;
this.doLoginStuff(success, 'facebook');
})
.catch((error) => {
if (error.code === "auth/account-exists-with-different-credential") {
this.presentToast(error.message)
}
// alert("Firebase failure: " + JSON.stringify(error));
});
}).catch((error) => { console.log(error) });
}
Don't use .signInWithCredential, use .onAuthStateChanged Or, this may be a Facebook-side issue. Browse to developers.facebook.com and check that your app is switched on/public and then check your app review page to make sure you can get emails.

Resent email verification link not changing verified to true

I'm setting up a custom authentication system in my meteor app.
When a user signs up, they are sent an email verification link. If they click on this, it changes the verification boolean to true as expected.
I have a button in the users account setting page that allows them to resend a verification email if needed. Clicking on this works ok and they receive another email with a different link.
When this new link is clicked, it redirects to the home page but it doesn't verify the email address.
My guess is that the users account isn't aware of the new token.
//server code
Meteor.methods({
'sendVerificationEmail' : function(userId, primaryEmail){
var userId = Meteor.user();
Accounts.sendVerificationEmail(userId, primaryEmail, function (error) {
if (! error) {
return alert('Verfication email sent');
} else {
return alert(error);
};
});
}
});
//Client code
Accounts.onEmailVerificationLink(function (token, done) {
Accounts.verifyEmail(token, function (error) {
if (! error) {
console.log('Account verified');
alert('Account verified');
}
done();
// show something if there was an error.
});
});
// Email verification route
Router.route('/verify-email/:token', {
name: 'verifyEmail'
});

Categories

Resources