本文介绍了使用SwiftUI和Firestore注册电子邮件,名字,姓氏的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想对用户进行身份验证,并使用用户名(first和last)使用Firestore创建用户数据库.我可以创建用户,但无法同时创建数据库.

I want to authenticate the user and create an user database with Firestore using users name ( first and last ). I can create the user but I couldn’t manage to create the database at the same time.

这是我的SessionStore,可用于注册和登录.通过注册,我想获取用户名并创建一个数据库. (下面没有代码,因为没有用)

This is my SessionStore which works for signup and signin. By Signup I want to grab the name of the user and create a database. ( no code down below because nothing worked)

我是一个初学者,所以我希望能给出一个详尽的答案.

I am a beginner so I would appreciate an elaborate answer.

非常感谢!

import Firebase
import Combine

class SessionStore: ObservableObject {
    var didChange = PassthroughSubject<SessionStore, Never>()
    @Published var session: User? {didSet {self.didChange.send(self) }}
    var handle: AuthStateDidChangeListenerHandle?

    func listen() {
        handle = Auth.auth().addStateDidChangeListener({ (auth, user) in
            if let user = user {
                self.session = User(uid: user.uid, email: user.email)
            } else {
                self.session = nil
            }
        })
    }

    func signUp(email: String, password: String, handler: @escaping AuthDataResultCallback) {
        Auth.auth().createUser(withEmail: email, password: password, completion: handler)
    }

    func signIn(email: String, password: String, handler: @escaping AuthDataResultCallback) {
        Auth.auth().signIn(withEmail: email, password: password, completion: handler)
    }

    func signOut() {
        do {
            try Auth.auth().signOut()
            self.session = nil
        } catch {
            print("Error signing out")
        }
    }


    func unbind() {
        if let handle = handle {
            Auth.auth().removeStateDidChangeListener(handle)
        }
    }

    deinit {
        unbind()
    }
}
struct User {
    var uid: String
    var email: String?

    init(uid: String, email: String?) {
        self.uid = uid
        self.email = email
    }
}
    ```

推荐答案

Firebase身份验证管理用户身份验证-虽然它确实存储了某些联合身份提供者提供的其他信息(例如个人资料图片URL),但这不是故意的成为配置文件管理解决方案.如果您想存储有关用户的其他信息,则可以使用Cloud Firestore或Firebase Realtime Database来实现.

Firebase Authentication manages user authentication - while it does store additional information provided by some of the federated identity providers (such as profile picture URL), it isn't intended to be a profile management solution. If you'd like to store additional information about your users, you can implement this using Cloud Firestore or Firebase Realtime Database.

Firebase用户对象仅具有有限数量的属性,主要是允许身份验证提供程序对用户进行身份验证.对于Swift, UserInfo 定义以下:

A Firebase user object only has a limited number of attributes, mostly to allow an authentication provider to authenticate the user. For Swift, the UserInfo defines the following:

  • providerId-提供者标识符
  • uid-用户的提供商的唯一ID
  • displayName-用户名(即他们的全名)
  • photoURL-用户个人资料照片的URL(如果由身份验证提供程序支持!)
  • email-用户的电子邮件地址
  • phoneNumber-用户的电话号码,仅当用户通过电话号码auth认证时才可用
  • providerId - the provider identifier
  • uid - the provider's unique ID for the user
  • displayName - the name of the user (i.e. their full name)
  • photoURL - the URL of the user's profile photo (if supported by the auth provider!)
  • email - the user's email address
  • phoneNumber - the user's phone number, only available if the user authenticated via phone number auth

因此,您需要做的是:

  1. 构建您的UI,以便用户可以提供其凭据(电子邮件/密码)以及您要捕获的其他任何个人资料信息(例如名字,姓氏,居住地)
  2. 使用Auth.auth().createUser(withEmail: email, password: password)
  3. 创建用户
  4. 如果该呼叫成功返回,请在Cloud Firestore或Firebase Realtime Database中创建用户个人资料
  1. Build your UI so it allows users to provide their credentials (email / password) as well as any additional profile info that you'd like to capture (such as first name, last name, place of residence)
  2. Create the user using Auth.auth().createUser(withEmail: email, password: password)
  3. If that call returns successfully, create a user profile in Cloud Firestore or Firebase Realtime Database

这是一个示例实现.

别忘了:

  1. 为您的项目下载GoogleService-Info.plist
  2. 在您的AppDelegate
  3. 中呼叫FirebaseApp.configure()
  4. 在您的项目中创建Cloud Firestore数据库(使用Firebase控制台)
  1. Download the GoogleService-Info.plist for your project
  2. Call FirebaseApp.configure() in your AppDelegate
  3. Create a Cloud Firestore database in your project (using the Firebase Console)

用户界面

// File: ContentView.swift
import SwiftUI

struct ContentView: View {
  @State var firstName: String = ""
  @State var lastName: String = ""
  @State var city: String = ""
  @State var email: String = ""
  @State var password: String = ""
  @State var confirmPassword: String = ""

  @State var showSignUpForm = true
  @State var showDetails = false

  @ObservedObject var sessionStore = SessionStore()
  @State var profile: UserProfile?

  var body: some View {
    NavigationView {
      VStack {
        if self.showSignUpForm {
          Form {
            Section {
              TextField("First name", text: $firstName)
                .textContentType(.givenName)
              TextField("Last name", text: $lastName)
                .textContentType(.familyName)
              TextField("City", text: $city)
                .textContentType(.addressCity)
            }
            Section {
              TextField("Email", text: $email)
                .textContentType(.emailAddress)
                .autocapitalization(.none)
              SecureField("Password", text: $password)
              SecureField("Confirm password", text: $confirmPassword)
            }
            Button(action: { self.signUp() }) {
              Text("Sign up")
            }
          }
          .navigationBarTitle("Sign up")
        }
        else {
          Form {
            TextField("Email", text: $email)
              .textContentType(.emailAddress)
              .autocapitalization(.none)
            SecureField("Password", text: $password)
            Button(action: { self.signIn() }) {
              Text("Sign in")
            }
          }
          .navigationBarTitle("Sign in")
        }
        Button(action: { self.showSignUpForm.toggle() }) {
          Text(self.showSignUpForm ? "Have an account? Sign in instead." : "No account yet? Click here to sign up instead.")
        }
      }
      .sheet(isPresented: $showDetails) {
        UserProfileView(userProfile: self.profile ??  UserProfile(uid: "", firstName: "", lastName: "", city: ""))
      }
    }
  }

  func signUp() {
    sessionStore.signUp(email: self.email, password: self.password, firstName: self.firstName, lastName: self.lastName, city: self.city) { (profile, error) in
      if let error = error {
        print("Error when signing up: \(error)")
        return
      }
      self.profile = profile
      self.showDetails.toggle()
    }
  }

  func signIn() {
    sessionStore.signIn(email: self.email, password: self.password) { (profile, error) in
      if let error = error {
        print("Error when signing up: \(error)")
        return
      }
      self.profile = profile
      self.showDetails.toggle()
    }
  }
}

struct ContentView_Previews: PreviewProvider {
  static var previews: some View {
    ContentView()
  }
}
// File: UserProfileView.swift
import SwiftUI

struct UserProfileView: View {
  var userProfile: UserProfile
  var body: some View {
    NavigationView {
      Form {
        Text(userProfile.uid)
        Text(userProfile.firstName)
        Text(userProfile.lastName)
        Text(userProfile.city)
      }
      .navigationBarTitle("User \(userProfile.uid)")
    }
  }
}

struct UserProfileView_Previews: PreviewProvider {
  static var previews: some View {
    let userProfile = UserProfile(uid: "TEST1234", firstName: "Peter", lastName: "Friese", city: "Hamburg")
    return UserProfileView(userProfile: userProfile)
  }
}

存储库

// File: SessionStore.swift
import Foundation
import Combine
import Firebase

class SessionStore: ObservableObject {
  @Published var session: User?
  @Published var profile: UserProfile?

  private var profileRepository = UserProfileRepository()

  func signUp(email: String, password: String, firstName: String, lastName: String, city: String, completion: @escaping (_ profile: UserProfile?, _ error: Error?) -> Void) {
    Auth.auth().createUser(withEmail: email, password: password) { (result, error) in
      if let error = error {
        print("Error signing up: \(error)")
        completion(nil, error)
        return
      }

      guard let user = result?.user else { return }
      print("User \(user.uid) signed up.")

      let userProfile = UserProfile(uid: user.uid, firstName: firstName, lastName: lastName, city: city)
      self.profileRepository.createProfile(profile: userProfile) { (profile, error) in
        if let error = error {
          print("Error while fetching the user profile: \(error)")
          completion(nil, error)
          return
        }
        self.profile = profile
        completion(profile, nil)
      }
    }
  }

  func signIn(email: String, password: String, completion: @escaping (_ profile: UserProfile?, _ error: Error?) -> Void) {
    Auth.auth().signIn(withEmail: email, password: password) { (result, error) in
      if let error = error {
        print("Error signing in: \(error)")
        completion(nil, error)
        return
      }

      guard let user = result?.user else { return }
      print("User \(user.uid) signed in.")

      self.profileRepository.fetchProfile(userId: user.uid) { (profile, error) in
        if let error = error {
          print("Error while fetching the user profile: \(error)")
          completion(nil, error)
          return
        }

        self.profile = profile
        completion(profile, nil)
      }
    }
  }

  func signOut() {
    do {
      try Auth.auth().signOut()
      self.session = nil
      self.profile = nil
    }
    catch let signOutError as NSError {
      print("Error signing out: \(signOutError)")
    }
  }
}
// File: UserProfileRepository.swift
import Foundation
import Firebase
import FirebaseFirestoreSwift

struct UserProfile: Codable {
  var uid: String
  var firstName: String
  var lastName: String
  var city: String
}

class UserProfileRepository: ObservableObject {
  private var db = Firestore.firestore()

  func createProfile(profile: UserProfile, completion: @escaping (_ profile: UserProfile?, _ error: Error?) -> Void) {
    do {
      let _ = try db.collection("profiles").document(profile.uid).setData(from: profile)
      completion(profile, nil)
    }
    catch let error {
      print("Error writing city to Firestore: \(error)")
      completion(nil, error)
    }
  }

  func fetchProfile(userId: String, completion: @escaping (_ profile: UserProfile?, _ error: Error?) -> Void) {
    db.collection("profiles").document(userId).getDocument { (snapshot, error) in
      let profile = try? snapshot?.data(as: UserProfile.self)
      completion(profile, error)
    }
  }
}

这篇关于使用SwiftUI和Firestore注册电子邮件,名字,姓氏的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

07-28 04:31