I've ran into this issue and have been trying to solve it for a few days now and it seems everything I try it creates more errors. I'm having problems creating a user with this function use FIRAuth and its telling me to add in a ; right before in, but it doesn't fix the problem.
Picture of Errors:
func handleRegistration() {
guard let email = emailTextField.text, let password = passTextField.text
else {
print("Form is not Valid")
return
}
FIRAuth.auth()?.createUser(withEmail: email, password: password, completion: FIRUser?, error) in
if error != nil {
print("Error")
return
}
print("Login Successful")
}
Try this :-
FIRAuth.auth()?.createUser(withEmail: email, password: password, completion: {(user, error) in
if error != nil {
print("Error")
return
}else{
print("Login Successful")
return
}
})
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'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'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.
I want the user to insert the current password and the new one when updating his password.
I've searched Firebase documentation and didn't find a way to verify the user's current password.
Does anyone know if this is possible?
You will be able to achieve it using reauthenticate before changing the password.
let user = FIRAuth.auth()?.currentUser
let credential = FIREmailPasswordAuthProvider.credentialWithEmail(email, password: currentPassword)
user?.reauthenticateWithCredential(credential, completion: { (error) in
if error != nil{
self.displayAlertMessage("Error reauthenticating user")
}else{
//change to new password
}
})
Just to add more information, here you can find how to set the credential object for whatever provider you are using.
For Swift 4:
typealias Completion = (Error?) -> Void
func changePassword(email: String, currentPassword: String, newPassword: String, completion: #escaping Completion) {
let credential = EmailAuthProvider.credential(withEmail: email, password: currentPassword)
Auth.auth().currentUser?.reauthenticate(with: credential, completion: { (error) in
if error == nil {
currentUser.updatePassword(to: newPassword) { (errror) in
completion(errror)
}
} else {
completion(error)
}
})
}
Firebase Documentation can be found here
For Swift 5 to change password in firebase
import Firebase
func changePassword(email: String, currentPassword: String, newPassword: String, completion: #escaping (Error?) -> Void) {
let credential = EmailAuthProvider.credential(withEmail: email, password: currentPassword)
Auth.auth().currentUser?.reauthenticate(with: credential, completion: { (result, error) in
if let error = error {
completion(error)
}
else {
Auth.auth().currentUser?.updatePassword(to: newPassword, completion: { (error) in
completion(error)
})
}
})
}
How to use ?
self.changePassword(email: "abcemail#gmail.com", currentPassword: "123456", newPassword: "1234567") { (error) in
if error != nil {
self.showAlert(title: "Error" , message: error?.localizedDescription)
}
else {
self.showAlert(title: "Success" , message: "Password change successfully.")
}
}
I am new to Firebase and have been trying to implement a "Sign Up" page for my app. Currently, I am only using the email feature, just to test things out. I am able to create the new user and store it in the "Login & Auth" tab in the dashboard, but for what ever reason I cannot retrieve the UID. The only way is if I close out of my app and reload it, then it can access the UID. Any suggestions on why this is?
Swift 4.1 FireBase User Create/Login Steps:-
STEP 1:- Register new firebase authentication Pod in to your Podfile :
pod 'Firebase/Auth'
STEP 2:- Open terminal and install pod with your related project directory path:
pod install
STEP 3:- import firebase authentication library in your UIViewController where ever you want:
import FirebaseAuth
STEP 4:- On your Registration UIButton Action write this snippet :
Auth.auth().createUser(withEmail: (txtEmail.text ?? ""), password: (txtPass.text ?? "")) { (result, error) in
if let _eror = error {
//something bad happning
print(_eror.localizedDescription )
}else{
//user registered successfully
print(result)
}
}
STEP 5:- If you want to Sign In after Registration use this snippet on your Sign in UIButton:
Auth.auth().signIn(withEmail: (txtEmail.text ?? ""), password: (txtPass.text ?? "")) { (result, error) in
if let _eror = error{
print(_eror.localizedDescription)
}else{
if let _res = result{
print(_res)
}
}
}
Happy Codding ;)!!!!
Example of how to create users:
Auth.auth().createUser(withEmail: email, password: password, completion: { (result, error) -> Void in
if (error == nil) {
UserDefaults.standardUserDefaults().setValue(result.uid, forKey: "uid")
print("Account created :)")
let userDict = ["Name": name!, "Major": major!, "Email": email!]
let uid = result!.uid
self.dismissViewControllerAnimated(true, completion: nil)
}
else{
print(error)
}
})
Hope this helps.
Creating user in Firebase is very easy, just add firebase to your project using cocoa pods and then create a signup screen. You need email and password. Now first import Firebase
import Firebase
Then inside on viewDidLoad() method configure firebase
FIRApp.configure()
Now get email and password from textfields and use the following code for registration.
FIRAuth.auth()?.createUserWithEmail(email!, password: password!, completion: { (user: FIRUser?, error) in
if error == nil {
//registration successful
}else{
//registration failure
}
})
Thats it.
Source: Firebase Swift Tutorial
Before you try any code, make sure your password is at least 6 characters long. Print the error if is possible, I would print the error on each function. sign up and sign in just to keep track of what you are doing.
#IBAction func signUp(_ sender: Any) {
Auth.auth().createUser(withEmail: self.usernameTextField.text!, password: self.passwordTextField.text!) {(user, error) in
if user != nil {
print("User Has SignUp")
}
if error != nil {
print(":(",error)
}
}
Auth.auth().signIn(withEmail: self.usernameTextField.text!, password: self.passwordTextField.text!) {(user, error) in
if user != nil {
print("User Has Sign In")
}
if error != nil {
print(":(",error)
}
}