Firebase用苹果登录。新用户和回头用户

时间:2019-12-16 04:43:45

标签: ios swift firebase google-cloud-platform sign-in-with-apple

我已实现使用Apple登录。我以这种方式启动首次用户:

let credential = OAuthProvider.credential(withProviderID: "apple.com",
                                          idToken: idTokenString,
                                          rawNonce: nonce)

然后登录用户:

// HERE: Auth.auth().currentUser is always nil for first time users AND returning users
Auth.auth().signIn(with: credential) { (authResult, error) in
     // HERE: Auth.auth().currentUser is never nil for first time users AND returning users
     if error == nil {
         guard let uid = authResult?.user.uid else { return }
         let databaseRef = Database.database().reference().child("Users/\(uid)")
         //update realtime database here
     }
}

对于初次使用的用户来说效果很好。问题是当它是回头用户时。基本上,对于首次使用用户和回头用户,我需要两个不同的操作。区别在于返回用户,我不向数据库添加任何内容

P.S。我遵循了here

中来自firebase的官方文档

1 个答案:

答案 0 :(得分:0)

似乎有以下作品

guard let uid = authResult?.user.uid else { return }
let db = Database.database().reference()
 db.child("Users").observeSingleEvent(of: .value) { (DataSnapshot) in
     if DataSnapshot.hasChild(uid){
        //Existing user. No need to add anything to database
     }else{
        //New user. Add/Update database with some default values
     }

我不知道这是否是最好的方法,但目前对我有效