【问题标题】:Swift Firebase -is there a way to directly set Auth.auth().currentUser && Auth.auth().currentUser?.uidSwift Firebase - 有没有办法直接设置 Auth.auth().currentUser && Auth.auth().currentUser?.uid
【发布时间】:2020-04-08 22:11:44
【问题描述】:

主要问题是可以从保存的Auth 对象中设置一个零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)和 userId 值(Auth.auth().currentUser?.uid):

var anonymousUser: User? // "object123"
var anonymousUserId: String? // "id456"

Auth.auth().signInAnonymously(completion: { [weak self](authDataResult: AuthDataResult?, error) in

        if let error = error { return }

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

        self?.anonymousUser = authDataResult?.user // "object123"
        self?.anonymousUserId = authDataResult?.user.uid // "id456"
    })
}

稍后用户创建一个帐户并获得一个全新的User? 值(Auth.auth().currentUser)和 userId 值(Auth.auth().currentUser?.uid)。他们创建帐户后,更新参考时出现问题。他们决定取消并宁愿保持匿名用户,而不是继续。

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
        }
    }
}

这里的问题是匿名用户登录对象现在已替换为真实用户对象,因为创建了新帐户。但是因为我删除了User?在警报的取消操作中,userA 现在为零。

有没有办法可以设置 nil Auth 对象做这样的事情:

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

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

【问题讨论】:

    标签: ios swift firebase firebase-authentication


    【解决方案1】:

    通常的方法是允许用户在不登录的情况下创建他们已识别的帐户(使用社交服务提供商或电子邮件+密码),然后link the new account with their existing anonymous account

    因此,对于电子邮件+密码,您无需调用createUser(withEmail: , password:),而是使用

    创建他们的凭据
    let credential = EmailAuthProvider.credential(withEmail: email, password: password)
    

    然后将它们链接到:

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

    【讨论】:

    • ohhhhhhh 我从来不知道,一直以来我都认为你必须按照我的方式去做。这与将电话号码链接到现有电子邮件注册的方式相同。谢谢!!!
    • 快速提问,如何登录匿名用户。假设您有一个注册用户,他们退出,他们现在是匿名用户,他们重新登录。您是否应该将匿名用户与登录用户联系起来,例如室内答案?
    • 当您注销 Firebase 身份验证时,您会从所有提供商注销,同时也从匿名提供商注销。
    • 也许我设置错了。我的应用程序的工作方式是,一旦下载了应用程序,用户就会被带入主应用程序(想想 yelp)。他们可以滚动浏览评论等。但要查看其他用户的评分,他们需要一个 uid(Firebase 规则)。如果没有 uid,那么你就看不到评分,所以一旦他们进入,我让他们以匿名用户的身份浏览。与已注销的人相同。规则例如 {ratings: “read”: “auth.uid != null” }。他们还需要有一个 uid 才能查看其他用户的个人资料照片等。匿名登录发生在后台,用户不知道。我能想到的就这些
    • 基本上无论用户是第一次下载应用还是注销他们都需要一个 uid 来查看其他用户的帖子,所以我在后台以匿名用户的身份登录跨度>
    猜你喜欢
    • 2019-04-20
    • 1970-01-01
    • 1970-01-01
    • 2021-07-06
    • 2018-08-19
    • 2019-01-24
    • 1970-01-01
    • 2021-02-26
    • 1970-01-01
    相关资源
    最近更新 更多