1
4

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 3 years have passed since last update.

FirebaseAuthとFirestoreをuidで繋げる

Posted at

前回の『FirebseAuthでアカウントを作成するコード』からEmailとPasswordを使ってアカウントを作成していきました。この情報をFirestoreに繋げていきたいと思います。
#コードはこんな感じ

ViewController.swift
    //前回の記事を参考にしてください→https://qiita.com/agachan0930/items/08e7f8412dbd32323ef7
    private func createUserToFireAuth(){
        guard let email = emailTextField.text else {return}
        guard let password = passwordTextField.text else {return}
        
        Auth.auth().createUser(withEmail: email, password: password){(auth, err) in
            if let err = err{
                print("You cannot create New Tinder Account", err)
                return
            }
            guard let uid = auth?.user.uid else {return}
            //新しく追加したのはここで下のsetUserDataToFirestoreの関数と接続されています。
            self.setUserDataToFirestore(email:email, uid: uid)
        }
    }

    //MARK:Create&ConnectFromFirebaseAuthToFirestore
    private func setUserDataToFirestore(email:String, uid: String){
        guard let name = nameTextField.text else {return}
        let document = [
            "name": name,
            "email": email,
            "createAt": Timestamp()
        ] as [String: Any]
        
        
        Firestore.firestore().collection("users").document(uid).setData(document){ err in
            if let err = err{
                print("You cannot save your account in FireStore", err)
                return
            }
            
            print("You success & save your account uid in FireStore")
        }
    }

#コード解説
Emailの情報とuidの情報をsetUserDataToFirestoreの関数に引き渡す。ここでの返り値はなし。

createUserToFireAuth内
self.setUserDataToFirestore(email:email, uid: uid)

createUserToFireAuthからEmailとuidをString型で受け渡しをする。

    //MARK:Create&ConnectFromFirebaseAuthToFirestore
    private func setUserDataToFirestore(email:String, uid: String){
    //省略
    }

辞書型[String:Any]で名前とEmail及び登録した時刻を型に準じてdocumentに格納する。

        let document = [
            "name": name,
            "email": email,
            "createAt": Timestamp()
        ] as [String: Any]

FireStore.firestoreでFirebase全体のデータベースを呼び出す。そのデータベース内の名前(users)に接続をする。
.documentで階層のリストを検索。
.setData(document)で先ほどセットしたデータ(document)を中に埋めこむ。
そしてif関数を用いて登録ができたかどうかを判定する。

        Firestore.firestore().collection("users").document(uid).setData(document){ err in
            if let err = err{
                print("You cannot save your account in FireStore", err)
                return
            }
            
            print("You success & save your account uid in FireStore")
        }

#次回
モデルの作成を行う

1
4
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
1
4

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?