TEAM LICENSES: Save money and learn new skills through a Hacking with Swift+ team license >>

CANT IMPORT FIREBASE FIRESTORE WHATS MY ERROR ?

Forums > SwiftUI


import SwiftUI
import FirebaseAuth
import FirebaseFirestore
import Firebase

struct ContentView: View {

    // login e registro
    @State private var email = ""
    @State private var password = ""
    @State private var nomeUsuario = ""

    // controle telas e autenticação
    @State private var usuarioLogado = false
    @State private var botaoRegistrar = false
    @State private var botaoLogout = false

    var body: some View {

        VStack (spacing:15)  {

            // Verifica se usuário está logado
            if !usuarioLogado {

                VStack (spacing:15) {

                    Text("Citie")
                        .font(.largeTitle)

                    TextField("E-mail", text: $email)
                        .autocapitalization()
                        .keyboardType(.emailAddress)
                        .textFieldStyle(.roundedBorder)
                        .multilineTextAlignment(.center)

                    if botaoRegistrar == true {
                        TextField("Usuário", text: $nomeUsuario)
                            .autocapitalization()
                            .textFieldStyle(.roundedBorder)
                            .multilineTextAlignment(.center)
                    }

                    SecureField("Senha", text: $password)
                        .textFieldStyle(.roundedBorder)
                        .multilineTextAlignment(.center)

                    if botaoRegistrar == true {
                        Button("Registrar") {
                            registrar()
                        }
                        .bold()
                        .frame(width:200, height:40)
                        .background(.blue)
                        .foregroundColor(.white)
                        .cornerRadius(10)
                        .shadow(color: .gray, radius: 5)

                        Button("Tenho usuário. Fazer login.") {
                            botaoRegistrar = false
                        }

                    } else {

                        Button("Login") {
                            login()
                        }
                        .bold()
                        .frame(width:200, height:40)
                        .background(.blue)
                        .foregroundColor(.white)
                        .cornerRadius(10)
                        .shadow(color: .gray, radius: 5)

                        Button("Não tenho usuário. Criar Conta.") {
                            botaoRegistrar = true
                        }

                    }
                }
                        .padding()
                        .onAppear{
                            Auth.auth().addStateDidChangeListener {
                                auth, user in
                                if user != nil {
                                    usuarioLogado = true
                                }
                            }
                        }

            }

            else {

                AppPrincipal()

                // acionar esse state no perfil do usuario.
                if botaoLogout {
                    Button("logout")
                    {
                        if logout() == "ok" {
                            usuarioLogado = false
                            email = ""
                            password = ""
                        }
                    }
                }

            }

        }
        .padding()
    }

        func registrar() {
            Auth.auth().createUser(withEmail: email, password: password)  { result, error in
                if error != nil {
                    print(error!.localizedDescription)
                }

                registrarFirestore()

            }
        }

        func registrarFirestore(){
            guard let uid = Auth.auth().currentUser?.uid else { return }
            let userData = ["nomeUsuario":nomeUsuario, "email": email, "uid": uid]
            Firestore.firestore().collection("users")
                .document(uid).setData(userData) { err in
                    if let err = err {
                        print(err)
                        return
                    }
                }
        }

        func login(){
            Auth.auth().signIn(withEmail: email, password: password) { result, error in
                if error != nil {
                    print(error!.localizedDescription)
                }

            }
        }

        func logout() -> String {
            try?
            Auth.auth().signOut()
            return "ok"
        }

    }

#Preview {
    ContentView()
}

2      

Hacking with Swift is sponsored by Blaze.

SPONSORED Still waiting on your CI build? Speed it up ~3x with Blaze - change one line, pay less, keep your existing GitHub workflows. First 25 HWS readers to use code HACKING at checkout get 50% off the first year. Try it now for free!

Reserve your spot now

Sponsor Hacking with Swift and reach the world's largest Swift community!

Reply to this topic…

You need to create an account or log in to reply.

All interactions here are governed by our code of conduct.

 
Unknown user

You are not logged in

Log in or create account
 

Link copied to your pasteboard.