gpt4 book ai didi

ios - Swift Firebase - 有没有办法直接设置 Auth.auth().currentUser && Auth.auth().currentUser?.uid

转载 作者:行者123 更新时间:2023-11-29 05:14:08 31 4
gpt4 key购买 nike

主要问题是可以从已保存的 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 继续作为他们,但这不是我想要做的。

最佳答案

通常的方法是允许用户在不登录的情况下创建其已识别的帐户(使用社交提供商或电子邮件+密码),然后 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
// ...
}

关于ios - Swift Firebase - 有没有办法直接设置 Auth.auth().currentUser && Auth.auth().currentUser?.uid,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59356764/

31 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com