7 C
London
Thursday, September 12, 2024

ios – Cannot register after signing out utilizing Firebase Auth


If I create a person in Firestore Auth console after which login through app it is nice. Nevertheless, if I create a person through my app, log off, after which try and login – it throws the next error and I am not logged in.

Any assist can be appreciated thanks. I am not so acquainted with Firebase. I’ve been following this tutorial.

Error:

Error Area=FIRAuthErrorDomain Code=17004 “The provided auth credential is malformed or has expired.” UserInfo={NSLocalizedDescription=The provided auth credential is malformed or has expired., FIRAuthErrorUserInfoNameKey=ERROR_INVALID_CREDENTIAL}

RegistrationView

import SwiftUI

struct RegisterView: View {
    @EnvironmentObject var viewModel: AuthViewModel
    @State var e-mail: String = ""
    @State var identify: String = ""
    @State var password: String = ""

    var physique: some View {
        VStack(alignment: .main) {
            Textual content("Register")

            //
            TextField("Title", textual content: $identify)

            //
            TextField("E mail", textual content: $e-mail)
                .textInputAutocapitalization(.by no means)
                .keyboardType(.emailAddress)
                .textCase(.lowercase)
            
            //
            SecureField("Password", textual content: $password)

            //
            Spacer()

            //
            Button("Signal Up") {
                Job {
                    attempt await viewModel.register(identify: identify, e-mail: e-mail, password: e-mail)
                }
            }
        }
    }
}

LoginView

import SwiftUI

struct LoginView: View {
    @EnvironmentObject var viewModel: AuthViewModel
    @State var e-mail: String = ""
    @State var password: String = ""

    var physique: some View {
        NavigationStack {
            VStack(alignment: .main) {
                Textual content("Login")

                //
                TextField("E mail", textual content: $e-mail)
                    .textInputAutocapitalization(.by no means)
                    .keyboardType(.emailAddress)
                    .textCase(.lowercase)

                //
                SecureField("Password", textual content: $password)

                //
                Spacer()

                //
                Button("Login") {
                    Job {
                        attempt await viewModel.login(e-mail: e-mail, password: password)
                    }
                }

                NavigationLink("Enroll") {
                    RegisterView()
                }
            }
        }
    }
}

AuthViewModel

import SwiftUI
import FirebaseAuth
import FirebaseFirestoreSwift
import FirebaseFirestore

remaining class AuthViewModel: ObservableObject{
    @Printed var userSession: FirebaseAuth.Person?
    @Printed var currentUser: Person?

    init() {
        // Learn from Firestore cache on system if logged in
        self.userSession = Auth.auth().currentUser
    }

    func login(e-mail: String, password: String) async throws {
        do {
            let consequence = attempt await Auth.auth().signIn(withEmail: e-mail, password: password)
            self.userSession = consequence.person
            attempt await fetch()
        } catch {
            print(error)
        }
    }

    func register(identify: String, e-mail: String, password: String) async throws {
        do {
            let consequence = attempt await Auth.auth().createUser(withEmail: e-mail, password: password)
            self.userSession = consequence.person

            //
            let person = Person(id: consequence.person.uid, identify: identify, e-mail: e-mail)
            let encodeUser = attempt Firestore.Encoder().encode(person)
            attempt await Firestore.firestore().assortment("customers").doc(person.id).setData(encodeUser)

            //
            attempt await fetch()
        } catch {
            print("Didn't create person", error)
        }
    }

    func signout() {
        do {
            attempt Auth.auth().signOut()
            self.userSession = nil
            self.currentUser = nil
        } catch {
            print(error)
        }
    }

    func deleteAccount() {
    }

    func fetch() async throws {
        guard let uid = Auth.auth().currentUser?.uid else { return }
        let snapshot = attempt await Firestore.firestore().assortment("customers").doc(uid).getDocument()
        self.currentUser = attempt snapshot.knowledge(as: Person.self)
    }
}

Latest news
Related news

LEAVE A REPLY

Please enter your comment!
Please enter your name here