I'm trying to Auth users with email but my function always return nil user. it worked perfectly until I added Facebook sign in as well, I made sure that I'm logged out of my facebook user when I try to register a new user to firebase.
Please let me know if any more of my code is needed to answer this.
Auth.auth().createUser(withEmail: email, password: pass, completion: {
(user, error) in
if user != nil {
print ("registered")
self.performSegue(withIdentifier: "goToHome", sender: self)
} else {
print("Error")
}
})
Instead of if user != nil do if (error != nil) and have your success code in the else statement.
Related
Sometimes we get Auth.auth().currentUser as nil (May be when session for that login expires).
My main question is:
What is the reason behind currentUser being nil.
If the curentUser is nil what if I call the reauthenticateWithCredential method on it?
Do reAuthenticate method fills the session again and start providing values in the currentUser?
Below is the code to reAuthenticate user.
func authenticateUser()
{
let credential = EmailAuthProvider.credential(withEmail: myEmail, password: myPassword)
Auth.auth().currentUser?.reauthenticate(with: credential, completion: { (dataResult, error) in
if(error == nil){
print("Email login success")
completionHandler(true)
}else{
print("Email login failed - " + error.debugDescription)
completionHandler(false)
}
})
}
Any HELP will be Appreciated,
Many Thanks!!
Dinal J
I have some simple App, which have auth.
nearest code check if you already entered:
func signingManager(){
Auth.auth().addStateDidChangeListener { [weak self] (auth, user) in
guard let self = self else {return}
if user != nil {
self.showNextVC()
print("You are already entered")
}
}
}
It's works when you first open the app and if you entered func "showNextVC" will open next VC.
In the same time i have login button with code :
#IBAction func logInTapped(_ sender: UIButton) {
guard let email = emailTextField.text, let password = passwordTextField.text, email != "", password != "" else {
displayWarningLabel(withText: "info is incorrect")
return
}
Auth.auth().signIn(withEmail: email, password: password, completion: { [weak self] (user, error) in
if error != nil {
self?.displayWarningLabel(withText: "error occured")
return
}
if user != nil {
self?.showNextVC()
print("Congratulations, you have successfully logged in!")
}
self?.displayWarningLabel(withText: "no such user")
}
)}
Now about the problem: if I click the "login" button, the "signingManager ()" method and it's "showNextVC" are triggered first, and only then the "logInTapped" method itself and again "showNextVC".
As a result, I have 2 VCs and two messages:
"You are already entered" and
"Congratulations, you have successfully logged in!"
What am I doing wrong? Thanks!
Since you're listening for for auth state changes, you don't need to handle the self?.showNextVC() in the completion callback for signIn(withEmail:, password:). That code should only be present in the callback for addStateDidChangeListener.
Alternatively, you can:
Use the addStateDidChangeListener to initially detect whether the user is signed-in already.
Inside the callback for the state change:
Remove the listener by calling removeAuthStateDidChangeListener
Start the explicit sign-in flow, and call signIn(withEmail:, password:) like you're doing now.
I'm currently working on an application with integrated Firebase login.
The last one I developed worked fine but the current App is causing a problem.
I'm taking the input from the Email and Password field and trying to login.
func login(user: String, pass: String){
Auth.auth().signIn(withEmail: user, password: pass) { (user, error) in
if(error != nil){
print("Success")
}
}
Every time I hit the "Login" button it prints "Success", even when I've not even entered Username and Password (blank text fields)
Code Snippet
Thanks for your Help in advance!
if (error != nil) ? Ok try to change if error == nil , because (error != nil) means that there is an error :).
func login(user: String, pass: String){
Auth.auth().signIn(withEmail: user, password: pass) { (user, error) in
if(error == nil){
print("Success")
} else {
print("error")
}
}
I am trying to login to my user after updating firebase, and after tracing the error, I get the following error:
Error Domain=FIRAuthErrorDomain Code=17007 "The email address is
already in use by another account."
UserInfo={NSLocalizedDescription=The email address is already in use
by another account., error_name=ERROR_EMAIL_ALREADY_IN_USE}
After looking it seems to be a because that firebase user is already in use, I am not sure how to fix this. I believe it is because I never signed out the user before closing app, but not am unable to login as any of my users.
Below is my code:
#IBAction func Login(sender: AnyObject) {
let email = self._Email.text!
let password = self._Password.text!
Auth.auth().createUser(withEmail: email, password: password) { (user, error) in
if error == nil {
//successfull login
print("Successful login****************************************")
//performs a segue to the next view controller
if user!.isEmailVerified{
//if the email is verified
let vc = self.storyboard!.instantiateViewController(withIdentifier: "ProfileView") as! ProfileView
self.present(vc, animated: true, completion: nil)
}
else {
print("email is not verified")
}
} else {
print("Some login error")
}
}
}
As ZassX pointed out, you're indeed using the signUp method of the Firebase iOS SDK, which is the createUserWithEmail. Use this method instead to signIn using email and password:
Auth.auth().signIn(withEmail: email, password: password) { (user, error) in
// ...
}
More info: https://firebase.google.com/docs/auth/ios/password-auth
You can check the list of your registered users in your Firebase Authentication Dashboard (https://console.firebase.google.com)
Also, it is good to print out the error description if you're having an error object. Like so:
print(error.localizedDescription).
I tried running the following and it does not work. What am I doing wrong to check the Firebase server if the user has already used the email?
FIRAuth.auth()!.createUser(withEmail: self.userEmailTextField.text!, password: self.userPasswordTextField.text!) {(createUser, error) in
if error != nil {
FIRAuth.auth()!.signIn(withEmail: self.userEmailTextField.text!, password: self.userPasswordTextField.text!)
//Display logged in
let viewController = self.storyboard!.instantiateViewController(withIdentifier: "TabBarController") as UIViewController
self.present(viewController, animated: true, completion: nil)
}else {
//Display an alert message
self.displayMyAlertMessage(userMessage: "Email already in use. Please see the login page.")
return
}
}
Please refer below code.
Code:
FIRAuth.auth()?.createUser(withEmail: email, password: password) {
(user, error) in
if (error) {
// Email is already in use.
} else {
// Create new user successfully
}
}
Handle Firebase iOS Auth Errors
Firebase already does this for you. Erase the sign in method and just run the create user method using an email that already exists and FIRAuthErrorCodeEmailAlreadyInUse will be returned. Check this out for more info