Martin Dichtler
Martin Dichtler

Reputation: 164

Firebase not storing logged in user

Was working with Firebase for almost half year now and never had this issue. Using code just like before to create new user, he shows up in console, when i close app i can sign in but using same code as always it keeps prompting me for password. In first VC i have:

    if let user = Auth.auth().currentUser {
        performSegue(withIdentifier: "authorized", sender: self)
    }

Which will check if there is auth token stored and perform segue to loading screen. However this never happens. The code is ran but doesn't do anything, no errors in compiler and no errors in console.

EDIT 1: also tried to create new project where i rewrote all the code getting same result, but my other project works without any issues, also i'm able to do anything else besides this using firebase such as access Firestore just not storing the user, went over all the documentation but couldn't find any solution.

EDIT 2: sign in code

if(email.text != "" && password.text != ""){
        Auth.auth().signIn(withEmail: email.text!, password: password.text!) { user, error in
            if(error == nil && user != nil) {
                userID = (Auth.auth().currentUser?.uid)!
                print("Signed in as: \(userID)")

                self.performSegue(withIdentifier: "load", sender: self)
            } else {
                print("Error found: \(error?.localizedDescription)")
            }
        }
    }

Solution:

DispatchQueue.main.async(){
        if let user = Auth.auth().currentUser {
            userID = (Auth.auth().currentUser?.uid)!

            self.performSegue(withIdentifier: "authorized", sender: self)
        }
    }

Loaded with async so firebase will configure before checking for auth

Upvotes: 0

Views: 161

Answers (1)

KingCoder11
KingCoder11

Reputation: 416

The issue is likely that you are calling your code before firebase has properly initialized. I recommend using the entry view controller's viewDidAppear for things such as these. If you were using viewDidLoad, it is likely that firebase did not load yet.

Upvotes: 1

Related Questions