1
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 5 years have passed since last update.

Swift5 Firebase でチャットアプリをつくろう 新規会員登録・ログイン・ログアウト編

1
Posted at

前提条件
・xcode11.4
・swift5

Firebaseとは

Firebaseとは、Googleが提供している、すばやく高品質のモバイルアプリやWebアプリケーションを開発することができるプラットフォームです。

Firebaseを使うことで、開発者はアプリケーションの開発に専念でき、バックエンドで動くサービスを作成する必要も管理する必要もありません。
ここでいうバックエンドとは、サービスの内、見えないところでデータの処理や保存などを行う要素のことです。

firebase-01_mbaas.png

Firebaseではたくさん役に立つ機能がありますが、今回はログインや新規登録の時に使う認証機能Firebase Authenticationとデータを保管するCloud Firestoreを使っていきます。
ちなみにデータベースにはCloud FirestoreとRealtime Databaseがありますが、Cloud FirestoreはRealtime Databaseをアップグレードさせたものです。

新規会員登録

Firebaseを導入したら進めてください。
導入の仕方は以下の記事が参考になると思います!
https://qiita.com/eKushida/items/54b62d3961a5c74a71af

ちなみにpodfileは 'Firebase/Auth'と'Firebase/Firestore'を入れました。

emailとパスワードをセット

emailとパスワードのtextFieldのtextを現在ログインしているユーザーの妥当するところにいれる。
Auth.auth().createUserが現在ログインしているユーザーのことです。

RegisterViewController.swift

 @IBAction func registerPressed(_ sender: UIButton) {
        if let email = emailTextField.text, let password = passwordTextField.text {
            Auth.auth().createUser(withEmail: email, password: password) { authResult, error in
                if error != nil {
                    print(error)
                    //あとでコード書く
                } else {
                    print("会員登録成功")

Cloud FirestoreのAuthenticationを見てみると、emailとパスワードがセットされました。
スクリーンショット 2020-08-20 12.38.08.png

ログイン

ログインするときは Auth.auth().signInです。
ログインに成功したときはログイン画面にいくようになっています。

RegisterViewController.swift
 @IBAction func loginPassword() {
        if let email = emailTextField.text,let password = passwoedTexxtField.text {
        Auth.auth().signIn(withEmail: email, password: password) {  authResult, error in
            if error != nil {
                
            } else {
                let storyboard = UIStoryboard(name: "Main", bundle: Bundle.main)
                                   let rootViewController = storyboard.instantiateViewController(withIdentifier: "RootTabBarController")
                                   UIApplication.shared.keyWindow?.rootViewController = rootViewController
                                   
                                   let ud = UserDefaults.standard
                                   ud.set(true, forKey: "isLogin")
                                   ud.synchronize()
               }
    }
        }

ログアウト

@IBAction func logout() {
        let firebaseAuth = Auth.auth()
        do {
            try firebaseAuth.signOut()
            let storyboard = UIStoryboard(name: "SignIn", bundle: Bundle.main)
            let rootViewController = storyboard.instantiateViewController(withIdentifier: "RootNavigationController")
            UIApplication.shared.keyWindow?.rootViewController = rootViewController
            // ログイン状態の保持
            let ud = UserDefaults.standard
            ud.set(false, forKey: "isLogin")
            ud.synchronize()
            
        } catch let signOutError as NSError {
            print ("Error signing out: %@", signOutError)
        }  
    }
1
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
1
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?