Re-Send Firebase email verification - ios

I have a question which looks silly but I can't find answer anywhere.
I have a simple signup iOS procedure which relies on Firebase Authentication SDK.
At a certain point after the user is created with:
FIRAuth.auth()?.createUser(withEmail: userName!, password: password!)
right after that I sent my user a verification email:
FIRAuth.auth()?.currentUser?.sendEmailVerification(completion:
{(error) in
if error == nil
{self.showSuccessPopUp()}
else
{self.showErrorPopUp()}
})
Everything works more than fine, no problem at all.
My question is: my user receives the email and - for any reason - didn't click on the autogenerated confirmation link.
Later on he open the app again and - forgetting that he already register once - tries to signup with the same email address.
Firebase just says that there's already an user created with that email address - as per documentation the user is created even if not 'active' -, therefore I'd like to give my users the option to have a "Resend verification email".
I've been digging into Firebase API documentation without a solution.
Does anyone have ever had the same 'issue' ?
Thanks for any help

Though late I would answer this in two scenarios:
1: You successfully called createUser, but when the user opens the app again, firebase.auth() says they are not signed in
In this case, the account exists with a password, so you will need to send a 'reset password' email, not an authentication email
2: You successfully called createUser, but when the user opens the app again, firebase.auth() says they ARE signed in
In this case, they are logged in, but not verified. Use
firebase.auth().currentUser.reload() // reloads user fields, like emailVerified:
if (!firebase.auth().currentUser.emailVerified) {
//resend verification email
} else {
//login
}
pardon my use of javascript but should be easy enough to translate

This question already has a short answer but I'll add my answer as I was facing the same problem. So there's already an user created with that email address. Later on the user opens the app again and wants to resend the email verification, but this time you don't create the user with email as there's already an user created in firebase with same email address, instead you can signing the user through firebase first as firebase already has user's details, if successful then get the current user from firebase. Now you can give your users the option to have a "Resend verification email" if they haven't verified yet. See the code below to get clear idea.
The following code assumes that user receives the email and - for any reason - didn't click on the autogenerated confirmation link. Later on he open the app again and - forgetting that he already register once - tries to signup with the same email address. Therefore you want give users the option to have a "Resend verification email".
firebaseAuth.signInWithEmailAndPassword(username, password)
.addOnCompleteListener(new OnCompleteListener<AuthResult>() {
#Override
public void onComplete(#NonNull Task<AuthResult> task) {
if (task.isSuccessful()) {
firebaseUser = firebaseAuth.getCurrentUser();
firebaseUser.reload(); // Here you finally get the user, now you can send verification mail again.
if(firebaseUser.isEmailVerified()) {
// TODO
}else {
// TODO
Toast.makeText(LoginActivity.this, "Please verify your email first!", Toast.LENGTH_LONG).show();
}
}
}
});
Now set a button Resend or something
Here's the code for that
resend.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
if(firebaseUser!=null){
firebaseUser.reload();
if(!firebaseUser.isEmailVerified()) {
firebaseUser.sendEmailVerification();
Toast.makeText(LoginActivity.this, "Email Sent!", Toast.LENGTH_LONG).show();
}else {
Toast.makeText(LoginActivity.this, "Your email has been verified! You can login now.", Toast.LENGTH_LONG).show();
}
}
}
});

This can be done using a firebase function with an email sending module (Nodemailer or Firebase Trigger Email extension for example)
The solution is in the firebase documentation
See section Generate email verification link

Related

When the user updates the e-mail address with the link

I'm developing an app where people can meet and chat. When a user wants to update their e-mail address, I get help from the following firebase function.
if let user = Auth.auth().currentUser {
user.sendEmailVerification(beforeUpdatingEmail: mail, completion: {error in
if let error = error {
print(error)
self.presenter?.onStateChange(
state: .CHANGE_MAIL_FAIL(message: "An error occurred. Try again later.".localized())
)
return
}
self.presenter?.onStateChange(state: .SUCCESS_MAIL_CHANGE) // sended verification link
})
}
This function sends a link to the user's new email address.If the user confirms with this link, the e-mail address is updated.
When the user updates their e-mail address, I need to make changes to the firestore database.
How can I update the database at the same time the user's email address is updated?

Check if user already has an account just by email

How do I check if a user exists in my Firebase Database only by email within Swift?
The first screen the user should see allows him to type in his email. After that he gets to the sign up page or the login page depending on wether or not the user exists.
You're looking for fetchSignInMethodsForEmail, which returns the sign-in methods with which the given email is registered. With that information you can build a sign-in screen.
Try with this:
Auth.auth().fetchProviders(forEmail: email) { (providers, error) in
if error != nil {
print(error?.localizedDescription)
} else {
print(providers)
}
}
If the email address has not yet been used, the empty list (providers) will be printed.

Firebase verifying email while being logged in

I have the following logic in my iOS app:
User registers
Firebase sends an email confirmation
Returns to login screen
Now if the user logs in, without verifying the email, then we have a user session and isEmailVerified is false.
I only need to check the isEmailVerified in a certain point in the app.
Also I think signing the user in, checking the field and signing the user out would be bad practise.
I'd need to reauthenticate the user, what is the best way of doing this? How can I, after the user has logged in, switch the status of isEmailVerified?
Thanks
First, you need to have the email and password to create a credential. Your user already provided this on the login page... So the email and password to persistent storage on iOS. In Android, the equivalent would be SharedPreferences.
I do not code in iOS, but this will give you the idea for the logic.
Then, when you get to that point in your app where email verified is called:
if (user.isEmailVerified) == true {
// you do not need to hold the email and password in persistent storage anymore.
// go into your persistent storage and delete the data.
} else {
// get the email and password the user saved in persistent storage.
String email = persistentStorage.getEmail();
String password = persistentStorage.getPassword();
var user = firebase.auth().currentUser;
var credentials = firebase.auth.EmailAuthProvider.credential(email, password);
user.reauthenticate(credentials);
// then, when finished reauthenticating, check whether isEmailVerified() == true;
}

Implementing email or username login using swift and Parse as a backend?

I am adding the feature of using both email and username to log in to an account. The problem is the the document on Parse.com uses a username to log in, but I want the option to also log in with an email address.
This is what I found on Parse.com as a starting point:
PFUser.logInWithUsernameInBackground("myname", password:"mypass") {
(user: PFUser?, error: NSError?) -> Void in
if user != nil {
// Do stuff after successful login.
} else {
// The login failed. Check error to see why.
}
}
I know this is where I should start, but I do not know where to go after this. I am still new to Swift and I have been trying to find ways to implement this feature, but to no success. How can this be achieved? The help is deeply appreciated.
I just used textFields to enter in an email and password, then set the emailTextField.text as both the username and email when creating an account. Now your email is your username! Bam.
I also kept the case for the email field, since aliases require case sensitivity, but made all the usernames lowercase, so people wouldn't have trouble logging in.

How to get Twitter account Email address using SLRequest or ACAccount?

I am developing an iOS app where i use Social.framework and Accounts.farmework to get Twitter account information.I am getting all the necessary info but not the Email address of the account.
After googling i am confuse is it really available?
Need help.
To request a user’s email, call the TwitterAuthClient#requestEmail method, passing in a valid TwitterSession and Callback.
TwitterAuthClient authClient = new TwitterAuthClient();
authClient.requestEmail(session, new Callback() {
#Override
public void success(Result result) {
// Do something with the result, which provides
// the email address
}
#Override
public void failure(TwitterException exception) {
// Do something on failure
}
});
Click 'Allow' when asked to 'Share your Email Address'.
If the user grants access and the email address is available, the success method is called with the email address in the result.
If the user denies access, the failure method will be called instead.
Note that even if the user grants access to her email address, it is not guaranteed you will get an email address. For example, if someone signed up for Twitter with a phone number instead of an email address, the email field may be empty. When this happens, the failure method will be called because there is no email address available.
Yes, you can get user the information. Check out this!

Resources