0
1

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.

FirebseAuthでアカウントを作成するコード

Last updated at Posted at 2021-06-02

今回は備忘録的な感じでFirebaseのAuthで登録する際に使うコードを残していきたいと思います。
#コードはこんな感じ

ViewController.swift
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 Account", err)
                return
            }
            guard let uid = auth?.user.uid else {return}
            print("You success & create New Account")
        }
    }

#コード解説
ここでemailTextFieldで打ち込まれたメールアドレスやパスワードが空文字でないかをチェックする
割と大事な書き方なので覚えておくと便利です!

guard let email = emailTextField.text else {return}
guard let password = passwordTextField.text else {return}

ここでFirebseAuthに情報を渡し、もし作成ができたら"You success & create New Account"で返す。
できなかったら"You cannot create New Account"とエラー内容が表示される。

Auth.auth().createUser(withEmail: email, password: password){(auth, err) in
            if let err = err{
                print("You cannot create New Account", err)
                return
            }
            guard let uid = auth?.user.uid else {return}
            print("You success & create New Account")
        }

#これどうやって使うの?
登録ボタンの中にぶち込む。以上

@IBAction func RegistUser(_ sender: Any) {
    createUserToFireAuth()
}

#ちなみに
uidは自動で生成されるアカウント識別番号のようなもので今後FireStorageやFireRealtimeDatabaseなどさまざまなものとFirebaseAuthを連携させるときに多用するので覚えててね!

#次回
FirebaseAuthとFirestoreをuidで繋げる

0
1
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
0
1

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?