I currently have login authentication working, as well as registration with firebase. I now wanted to keep my user logged in, which is working. The problem I am facing is when I log out, I try to log back in and I receive this error:
// Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: 'Receiver () has no segue with identifier 'chatRoom''\
Which does not make sense, because when I first log in, it brings me to the ChatRoom Viewcontroller using the chatRoom segue.
This is my code below:
func handleLogin() {
guard let email = emailTextField.text, let password = passwordTextField.text
else {
print("Form is not valid")
return
}
Auth.auth().signIn(withEmail: email, password: password, completion: { (user, error) in
if error != nil {
print(error!.localizedDescription)
self.loginErrorAlert("Error!", message: "Username or password incorrect, please try again.")
return
}
// successfully logged in our user and keep user logged in until they logout
if Auth.auth().currentUser != nil {
UserDefaults.standard.set(Auth.auth().currentUser!.uid, forKey: "loggedIn")
UserDefaults.standard.synchronize()
self.performSegue(withIdentifier: "chatRoom", sender: self)// this is the error I am facing
}
})
}
func handleRegister() {
guard let email = emailTextField.text, let password = passwordTextField.text, let name = nameTextField.text else {
print("Form is not valid")
signupErrorAlert("Error!", message: "Could not be Registered at this time, please try again.")
return
}
Auth.auth().createUser(withEmail: email, password: password, completion: { (user: User?, error) in
if error != nil {
print(error!.localizedDescription)
self.loginErrorAlert("Error!", message: "Could not be Registered at this time, please try again later.")
return
}
guard let uid = user?.uid else {
return
}
// successfully authenticated user and keep logged in until they logout
let ref = Database.database().reference(fromURL: "https://boccighub.firebaseio.com/")
let usersReference = ref.child("users").child(uid)
let values = ["name": name, "email": email]
usersReference.updateChildValues(values, withCompletionBlock: { (err, ref) in
if err != nil {
print(err!.localizedDescription)
return
}
if Auth.auth().currentUser != nil {
UserDefaults.standard.set(Auth.auth().currentUser!.uid, forKey: "loggedIn")
UserDefaults.standard.synchronize()
self.performSegue(withIdentifier: "chatRoom", sender: self)
}
})
})
}
// User logged out
#IBAction func handleLogout(_ sender: Any) {
do {
try Auth.auth().signOut()
print("user signedout")
if Auth.auth().currentUser == nil {
print("No user, key removed")
UserDefaults.standard.removeObject(forKey: "loggedIn")
UserDefaults.standard.synchronize()
print("User logged out")
let loginController = LoginViewController()
present(loginController, animated: true, completion: nil)
}
} catch let logoutError {
print(logoutError)
}
}
Fixed. I Removed the let loginController = LoginViewController(),
I created a new segue and now do self.performSegue(withIdentifier: "logOut", sender: self), works perfectly
Related
I try to do firebase authenticate. I've installed all necessary pods and made all needed methods. When I click "Login" even when I entered bad email and password, I get error message (e.g. bad password) and immediately my view is changed to my target view (next ViewController). I don't know why it is happening, because I made if statement. I want to have my login page unless I enter correct email and password.
#IBAction func loginTapped(_ sender: UIButton) {
if let email = emailTextField.text?.trimmingCharacters(in: .whitespacesAndNewlines), let password =
passwordTextField.text?.trimmingCharacters(in:
.whitespacesAndNewlines) {
Auth.auth().signIn(withEmail: email, password: password) { (authResult, error) in
if let e = error {
// Can't sign in
self.errorLabel.text = error!.localizedDescription
self.errorLabel.alpha = 1
} else {
self.performSegue(withIdentifier: "loginToHome", sender: self)
}
}
}
}
You may try to check if an error exists first. If yes, the error message will be shown. If there's no error, the login success segue will be performed.
#IBAction func loginTapped(_ sender: UIButton) {
if let email = emailTextField.text?.trimmingCharacters(in: .whitespacesAndNewlines), let password = passwordTextField.text?.trimmingCharacters(in: .whitespacesAndNewlines) {
Auth.auth().signIn(withEmail: email, password: password) { (authResult, error) in
if error != nil { // there's an error - show error label
self.errorLabel.text = error!.localizedDescription
self.errorLabel.alpha = 1
} else { //no error - perform segue
// no error - perform segue
self.performSegue(withIdentifier: "loginToHome", sender: self) } } } }
Edit: You may also add return after checking that an error exists - the codes that follow after it will not be performed:
#IBAction func loginTapped(_ sender: UIButton) {
if let email = emailTextField.text?.trimmingCharacters(in: .whitespacesAndNewlines), let password = passwordTextField.text?.trimmingCharacters(in: .whitespacesAndNewlines) {
Auth.auth().signIn(withEmail: email, password: password) { (authResult, error) in
if error != nil { // there's an error - show error label
self.errorLabel.text = error!.localizedDescription
self.errorLabel.alpha = 1
return // activities that follow will not be performed
} else { //no error - perform segue
self.performSegue(withIdentifier: "loginToHome", sender: self) } } } }
The segue needs to take place only after the user has been authenticated with their Google account. This authentication process takes place in the AppDelegate. I have a segue between two view controllers rather than a button so when the button is tapped, the segue will only be called for once.
The following code takes place within the sign function in the AppDelegate for anyone who is familiar with Firebase:
Auth.auth().signIn(with: credential) { (authResult, error) in
if let error = error {
print("Firebase sign In error")
print(error)
return
} else {
let db = Firestore.firestore()
db.collection("users").getDocuments() { (querySnapshot, err) in
if let err = err {
print("Error getting documents: \(err)")
} else {
print("Accessing email")
for document in querySnapshot!.documents {
if document.get("uid") as? String != Auth.auth().currentUser?.uid {
db.collection("users").addDocument(data: ["firstName": firstName!, "lastName": lastName!, "email": email!, "uid": authResult!.user.uid]) { (error) in
if error != nil {
print("Error: User data not saved")
return
}
}
}
}
}
}
self.window?.rootViewController!.performSegue(withIdentifier: "googleSegue", sender: nil)
print("User is signed in with Firebase")
}
}
}
However the self.window?.rootViewController!.performSegue(withIdentifier: "googleSegue", sender: nil) does nothing when it should be occurring and taking the user to the connected view controller after they sign in. The print statement does occur so there is nothing wrong in that regard.
For reference, this is what happens in the viewController file:
#IBAction func googleSignInTapped(_ sender: Any) {
print("User has chosen to sign with Google.")
GIDSignIn.sharedInstance().signIn()
}
I'm following along Rob Percival's iOS developer course and I am currently stuck at the "Uber clone" project. Following is an extract of the problematic code. The line of code in which the error occurs is:
if user?.displayName == "Lehrer" {
which is found after the comment //LOG IN
#IBAction func topTapped(_ sender: Any) {
if emailTextField.text == "" || passwordTextField.text == "" {
displayAlert(title: "Achtung", message: "Du musst sowohl eine Email-Adresse als auch Passwort eingeben!")
} else {
if let email = emailTextField.text {
if let password = passwordTextField.text {
if signUpMode {
// SIGN UP
Auth.auth().createUser(withEmail: email, password: password, completion: { (user, error) in
if error != nil {
self.displayAlert(title: "Error", message: error!.localizedDescription)
} else {
if self.benutzerLehrerSwitch.isOn {
//Lehrer
let req = Auth.auth().currentUser?.createProfileChangeRequest()
req?.displayName = "Lehrer"
req?.commitChanges(completion: nil)
self.performSegue(withIdentifier: "lehrerSegue", sender: nil)
} else {
// RIDER
let req = Auth.auth().currentUser?.createProfileChangeRequest()
req?.displayName = "Benutzer"
req?.commitChanges(completion: nil)
self.performSegue(withIdentifier: "benutzerSegue", sender: nil)
}
}
})
} else {
// LOG IN
Auth.auth().signIn(withEmail: email, password: password, completion: { (user, error) in
if error != nil {
self.displayAlert(title: "Error", message: error!.localizedDescription)
} else {
if user?.displayName == "Lehrer" {
// BENUTZER
self.performSegue(withIdentifier: "benutzerSegue", sender: nil)
} else {
// LEHRER
self.performSegue(withIdentifier: "lehrerSegue", sender: nil)
}
}
})
}
}
}
}
}
Basically, I allow users to sign up as one of two different types of roles ("Lehrer" or "Benutzer") and depending on which of those they choose, they should see different screens after logging in. I use Firebase for the backend of this code and signing up User's accounts works fine if I delete the faulty code. Does anyone have any ideas?
You can see the exact look in this screenshot:
Google has updated the methods after Firebase 5+ update
try this
self.name = user.user.displayName
put user?.user.displayName instead of user?.displayName
I am trying to have my login function for firebase run after updating it, but the function is nil and I am getting the following messages:
Cannot create a URL to fetch a configuration with empty app instance ID or empty platform (app instance, platform): (nil), ios
[Firebase/Analytics][I-ACS023125] Unable to get configuration. Invalid server URL.
My code is as follows:
#IBAction func Login(sender: AnyObject) {
let email = self._Email.text!
let password = self._Password.text!
print("below is the email")
print(email)
print("below is the password")
print(password)
Auth.auth().createUser(withEmail: email, password: password) { (user, error) in
if error == nil {
print("Successful login")
if user!.isEmailVerified{
let vc = self.storyboard!.instantiateViewController(withIdentifier: "ProfileView") as! ProfileView
self.present(vc, animated: true, completion: nil)
} else {
print("nil is hitting")
}
} else {
print("Some nil error")
}
}
}
Why is it that when I type in an email and password nothing shows up in Firebase? I've done it before with no issues.
No user entry is made when i check my Authentication tab in firebase.
//Sign up user
let email = emailTextField.text
let password = passwordTextField.text
FIRAuth.auth()?.createUser(withEmail:email! , password: password!, completion: { (user, error) in
if error != nil {
self.login()
} else {
print("User created!")
self.login()
}
})
}
#IBAction func signInButton(_ sender: Any) {
}
//Functions
func login(){
FIRAuth.auth()?.signIn(withEmail: emailTextField.text!, password: passwordTextField.text!, completion: { (user, error) in
if error != nil {
print("Password or email has not been entered correctly")
} else {
print("Successful Login")
}
})
}
}
I don't know if it will fix your problem, but try declaring with a guard statement, and remove your bangs (!) in the signup/login. It should at least be a lot less error prone.
Guard let email = emailTextField.text else { return }
Guard let password = passwordTextField.text else { return }
Now remove bangs so it looks like this
FIRAuth.auth()?.createUser(withEmail: email , password: password, completion: { (user, error) in
And do the same for your login function, unwrap your textField texts with a guard statement.
Cheers.
I was using the wrong GoogleService-info.plist file. I kept using the same one because i didn't think i had to keep re-downloading them for each app.