Ios Swift Firebase-是否有方法直接设置Auth.Auth().currentUser&&;Auth.Auth().currentUser?.uid

Ios Swift Firebase-是否有方法直接设置Auth.Auth().currentUser&&;Auth.Auth().currentUser?.uid,ios,swift,firebase,firebase-authentication,Ios,Swift,Firebase,Firebase Authentication,主要问题是是否可以从保存的Auth对象设置nilAuth对象: let oldAuth = Auth.auth().currentUser let oldAuthId = Auth.auth().currentUser?.uid let newAuth = Auth.auth().currentUser let newAuthId = Auth.auth().currentUser?.uid newAuth?.delete { (error) in if let error = er

主要问题是是否可以从保存的
Auth
对象设置nil
Auth
对象:

let oldAuth = Auth.auth().currentUser
let oldAuthId = Auth.auth().currentUser?.uid

let newAuth = Auth.auth().currentUser
let newAuthId = Auth.auth().currentUser?.uid

newAuth?.delete { (error) in
    if let error = error { return }

    // *** Auth is now nil ***

    // set the nil Auth to the oldAuth
    Auth.auth().currentUser = self.oldAuth
    Auth.auth().currentUser?.uid = self.oldAuthId
}
上面的代码不起作用,我想让它起作用,但问题是你为什么要这样做。解释如下

我正在使用匿名登录并创建帐户登录,但遇到问题

userA首先以匿名方式登录,他们会得到一个
User?
值(
Auth.Auth().currentUser
)和用户ID值(
Auth.Auth().currentUser?.uid
):

之后,用户创建一个帐户并获得一个全新的
user?
值(
Auth.Auth().currentUser
)和用户ID值(
Auth.Auth().currentUser?.uid
)。在他们创建帐户后,更新ref时出现问题。与其继续,他们决定取消,而宁愿保持匿名用户身份

var realUser: User? // "objectABC"
var realUserId: String // "idXYZ"

Auth.auth().createUser(withEmail: emailTextField.text!, password: passwordTextField.text!, completion: { [weak self] (authDataResult: AuthDataResult?, error) in

        if let error = error { return }

        guard let realUserId = authDataResult?.user.uid else { return }

        self?.realUser = authDataResult?.user // "objectABC"
        self?.realUserId = authDataResult?.user.uid // "idXYZ"

        let someRef = Database.database().reference().child("some").child(realUserId).childByAutoId()
        someRef?.updateChildValues(someDict, withCompletionBlock: { [weak self] (error, ref) in

        if let error = error {

             self?.problemAlert()

            // *** PROBLEM *** present alert giving the user the option to cancel or continue on as an anonymous user
            return 
        }
    })
})
我现在的做法是,如果用户决定取消,我将从FB中删除用户对象:

func problemAlert() {

    let alert = UIAlertController ...

    let tryAgainAction = UIAlertAction...

    let cancelAction = UIAlertAction(title: "Cancel", style: .default) { (action) in

        // *** COMPLETELY DELETE THE USER ***
        let user = self.realUser
        user?.delete { (error) in
            if let error = error { return }

            // *** Auth is now nil ***

            // *** set Auth back to the anonymousUser values but this doesn't work ***
            Auth.auth().currentUser = self.anonymousUser
            Auth.auth().currentUser?.uid = self.anonymousUserId
        }
    }
}
这里的问题是,由于创建了新帐户,匿名用户登录对象现在已替换为真实用户对象。但是因为我删除了
用户
?在警报的取消操作中,userA现在为零

有没有一种方法可以将nil Auth对象设置为执行以下操作:

// this doesn't work
Auth.auth().currentUser = anonymousUser
Auth.auth().currentUser?.uid = anonymousUserId

解决方法可以是不要删除新创建的当前用户,让userA继续作为他们使用,但这不是我想要做的。

通常的方法是允许用户创建他们的识别帐户(使用社交提供商或电子邮件+密码),而无需登录,然后

因此,对于email+password,您不需要调用
createUser(使用email:,password:)
,而是使用

let credential = EmailAuthProvider.credential(withEmail: email, password: password)
然后将它们链接到:

user.link(with: credential) { (authResult, error) in
  // ...
}

哦,我从来都不知道,一直以来我都认为你必须按照我的方式去做。这与您将电话号码链接到已存在的电子邮件注册的方式相同。谢谢快速提问,登录匿名用户怎么样。假设你有一个注册用户,他们注销,他们现在是匿名用户,他们重新登录。您是否应该将匿名用户与登录用户(如室内答案)链接?当您注销Firebase身份验证时,您将从所有提供商注销,也从匿名提供商注销。可能我设置错误。我的应用程序的工作方式是,一旦下载了应用程序,用户就会进入主应用程序(想想yelp)。他们可以滚动浏览评论等,但要查看其他用户的评分,他们需要uid(Firebase规则)。如果没有uid,那么你就看不到评分,所以一旦他们进入,我就让他们以匿名用户的身份浏览。注销的人也是这样。规则如{评级:“读取”:“auth.uid!=null”}。他们还需要一个uid来查看其他用户的个人资料照片等。匿名登录发生在后台,用户不知道。这就是我所能想到的基本情况,无论用户是第一次下载应用程序,还是他们注销,他们都需要一个uid来查看其他用户的帖子,所以我在后台以匿名用户身份登录他们
user.link(with: credential) { (authResult, error) in
  // ...
}