bmcmahen / julienne-swift

An iOS port of Julienne.app, built with SwiftUI and Firebase
51 stars 14 forks source link

ContentView not switching to not nil session view #2

Open b-mahjour opened 4 years ago

b-mahjour commented 4 years ago

Hi, thanks so much for your code. It's been really helpful. I'm having an issue with the sign in. Basically my contentview.swift is identical to yours:

    func getUser () {
        session.listen()
    }
    var body: some View {
        Group{
            if (session.session != nil){
                Text("signed in")
            } else{
                SignInView()
            }
        }.onAppear(perform: getUser)
    }
}

As well as my signinview:

    func signIn () {
        loading = true
        error = false
        session.signIn(email: email, password: password) { (result, error) in
            self.loading = false
            if error != nil {
                self.error = true
                print("OG GOD WHYYY")
            } else {
                print(result?.user.uid)
//                self.session.listen()
                self.email = ""
                self.password = ""
            }
        }
    }

However, when I test signing it, there is no error and the email and password become blank, but it remains on the sign in view page instead of displaying "signed in" as I believe it should. I've compared everything to the code you have online and it seems to be identical. What am I missing?

Thanks again!

b-mahjour commented 4 years ago

And my listen in sessionstore for reference as well:

    func listen () {
        // monitor authentication changes using firebase
        handle = Auth.auth().addStateDidChangeListener { (auth, user) in
            if let user = user {
                // if we have a user, create a new user model
                print("Got user: \(user)")
                self.isLoggedIn = true
                self.session = User(
                    uid: user.uid,
                    displayName: user.displayName,
                    email: user.email
                )
            } else {
                // if we don't have a user, set our session to nil
                self.session = nil
                self.isLoggedIn = false
            }
        }
    }
dchakarov commented 4 years ago

Hi @b-mahjour, try putting @Published before the var session: User? line in Session.swift here: https://github.com/bmcmahen/julienne-swift/blob/master/julienne/Session.swift#L49

psenechal commented 4 years ago

Adding @Published worked for me. I added it to isLoggedIn as well. Any reason we can't use isLoggedIn instead of session != nil? Seems to work well and I prefer using truthy statements over checking for nil.