How can I call for a segue in the AppDelegate? - ios

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()
}

Related

Firebase Email Verification Swift

I tried to use the Firebase Authentication, but in the login lets me in even when I haven't used the email to confirm the verification.
I have two Viewcontroller , one for Login and the other for signUP.
I can log in and I get the email for verification, but I can also log in in without verification.
public func sendVerificationMail() {
if self.authUser != nil && !self.authUser!.isEmailVerified {
self.authUser!.sendEmailVerification(completion: { (error) in
// Notify the user that the mail has sent or couldn't because of an error.
})
} else {
// Either the user is not available, or the user is already verified.
}
}
#IBAction func signupButtonTapped(_ sender: Any) {
print("Sign up button tapped")
Auth.auth().createUser(withEmail: self.userEmailTextField.text!, password: self.userPasswordTextField.text!) { (user, error) in
if user != nil {
print("User has Signed Up")
self.sendVerificationMail()
}
if error != nil {
print("User cant Sign Up")
}
}
}
#IBAction func signinButtonTapped(_ sender: Any) {
Auth.auth().signIn(withEmail: self.userEmailTextField.text!, password: self.userPasswordTextField.text!) { (user, error) in
if user != nil {
print("User has Signed In")
}
if error != nil {
print("Cant Sign in user")
} else {
self.performSegue(withIdentifier: "toHome", sender: nil)
}
}
}
Firebase Auth doesn't prevent people from signing in when their email isn't verified. If you want to prevent users from advancing when they aren't verified, you will need to code this on the client using the isEmailVerified boolean.
Auth.auth().signIn(withEmail: self.userEmailTextField.text!, password: self.userPasswordTextField.text!) { (authResult, error) in
if let authResult = authResult {
let user = authResult.user
print("User has Signed In")
if user.isEmailVerified {
self.performSegue(withIdentifier: "toHome", sender: nil)
} else {
// do whatever you want to do when user isn't verified
}
}
if let error = error {
print("Cant Sign in user")
}
}
A more comprehensive solution than that provided by #jen-person (and based on together with another SO answer) is this:
final class MySignInView: UIView { // or possibly MySignInViewController: UIViewController
// ... your properties etc...
#IBAction func signInButtonPressed(sender _: AnyObject) {
trySigningIn()
}
}
// MARK: Private
private extension MySignInView {
func trySigningIn() {
guard
let email = userEmailTextField.text,
let password = userPasswordTextField.text
else {
print("Cannot sign in, email or password is 'nil'")
return
}
do {
try signIn(email: email, password: password) { [unowned self] authResult in
self.userDidSignIn(authResult.user)
}
} catch {
// Display error
}
}
func userDidSignIn(_ user: FIRUser) {
// Creds to Jen: https://stackoverflow.com/a/51389154/1311272
guard user.isEmailVerified else {
// TODO display message about non verified email user?
return
}
performSegue(withIdentifier: "toHome", sender: nil)
}
}
// MARK: Error
private extension MySignInView {
enum Error: Strng, Equatable {
case invalidEmail
case userDisabled
case wrongPassword
case userNotFound
case networkError
case unknownError
}
}
// MARK: Firebae specific
private extension MySignInView {
func signIn(
email: String,
password: String,
onSuccessful: (AuthDataResult) -> Void
) throws {
Auth.auth().signIn(
withEmail: email,
password: password
) { (authResult, anyError) in
if let anyError = anyError {
if let error = Error(anyError: anyError) {
throw error
} else {
fatalError("Unsupported error: \(anyError)")
}
}
onSuccessful(authResult)
}
}
}
private extension MySignInView.Error {
init?(anyError: Swift.Error) {
guard let authErrorCode = FIRAuthErrorCode(rawValue: anyError.code) else {
return nil
}
self.init(fireBaseAuthErrorCode: authErrorCode)
}
// Creds goes to to: https://stackoverflow.com/a/39936083/1311272
init(fireBaseAuthErrorCode: FIRAuthErrorCode) {
switch errCode {
case .ErrorCodeInvalidEmail:
self = .invalidEmail
case .ErrorCodeUserDisabled:
self = .userDisabled
case .ErrorCodeWrongPassword:
self = .wrongPassword
case .ErrorCodeUserNotFound:
self = .userNotFound
case .ErrorCodeNetworkError:
self = .networkError
default:
self = .unknownError
}
}
}
You need to inform the user appropriately about the different errors/incorrect states of course, not only print.
This code should probably me moved to a ViewModel, and if your are not using MVVM, I highly recommend it :).
if let email = emailTextfield.text, let password = passwordTextfield.text {
Auth.auth().signIn(withEmail: email, password: password) { authResult, error in
if let e = error{
print(e.localizedDescription)
}
else {
//Do whatever you want to do after successful login
}
}
}
You can try this also:
if( !Auth.auth().currentUser!.isEmailVerified) {
// do something
}

Firebase Anonymous Auth, Nil User

I'm using Firebase database and offer anonymous login. The first anonymous login made on a single device works as expected. If I sign out and attempt any more anonymous logins, it succeeds, the completion block has no error and returns a user.
However, once it's all done and we're out of the completion block, Auth.auth().currentUser() is nil.
If I run a simple Timer checking Auth.auth().currentUser() every second, throughout the entire login process it is always nil and never changes.
Quick breakdown of code:
Login anonymously.
Check if id exists in db.
Update profile displayName with id for easy referral later.
Fetch client in db.
All go wrong!
Tap a button to sign in.
#IBAction func clientLoginBtnTap(_ sender : UIButton) {
self.clientActivityIndicator?.showActivityIndicator()
Auth.auth().signInAnonymously { (user, error) in
if error == nil {
//check id matches available client
self.checkClient(id: (self.clientIdField?.text)!, completion: { (isValid) in
if isValid == true {
//now signed in, update client id
let profileChangeRequest = user?.createProfileChangeRequest()
profileChangeRequest?.displayName = self.clientIdField?.text
profileChangeRequest?.commitChanges(completion: { (error) in
if error == nil {
//done
UserDefaults.standard.set(true, forKey: kIS_USER_CLIENT_NOT_TRAINER)
self.dismiss(animated: true, completion: {
//self.delegate?.didLoginAsClient()
})
}
else {
self.logout()
self.clientIdField?.shake()
self.clientActivityIndicator?.hideActivityIndicator()
}
})
}
else {
self.logout()
self.clientIdField?.shake()
self.clientActivityIndicator?.hideActivityIndicator()
}
})
}
else {
}
}
}
func checkClient(id : String, completion: #escaping (_ isValid : Bool) -> Void) {
let ref = Database.database().reference().child("v2").child("clients").child(id)
ref.observeSingleEvent(of: .value, with: { (snapshot) in
if snapshot.exists() { completion(true) }
else { completion(false) }
}) { (error) in
completion(false)
}
}
func logout() {
do {
try Auth.auth().signOut()
}
catch let error as NSError {
print (error.localizedDescription)
}
}
Login is successful.
Then this runs after login and the user exists but Auth.auth().currentUser() is nil. When a client login happens, I try to get the client data but permission is denied because we have no user.
handle = Auth.auth().addStateDidChangeListener { (auth, user) in
self.currentUser = user
if user == nil {
self.updateForNoUser()
}
else {
self.updateForUser()
}
}
func updateForUser() {
//Trainer Logged in
if UserDefaults.standard.bool(forKey: kIS_USER_CLIENT_NOT_TRAINER) == false {
self.performSegue(withIdentifier: "master", sender: self)
}
//Client Logged in
else {
if let id = Auth.auth().currentUser?.displayName {
let ref = Database.database().reference().child("v2").child("clients").child(id)
ref.observeSingleEvent(of: .value, with: { (snapshot) in
if snapshot.exists() {
self.client = Client(snapshot: snapshot)
self.performSegue(withIdentifier: "masterClient", sender: self)
}
}) { (error) in }
}
else {
do {
try Auth.auth().signOut()
}
catch let error as NSError {
print (error.localizedDescription)
}
}
}
}

Firebase Login URL Fetch Config Error

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")
}
}
}

Terminating app due to uncaught exception Swift

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

Users won't show up on Authentication tab in Firebase

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.

Resources