#50 User login with Firebase

Krauser Huang
彼得潘的 Swift iOS App 開發教室
8 min readApr 1, 2021

--

練習功能:
Firebase Authentication
使用者登入、登出
Firebase Firestore
使用者帳號註冊、呼叫

實作部分

Firebase是google設計的後端資料庫平台,教學跟使用上都很齊全

Installation

使用第三方程式,利用Cocoapods來安裝

  1. 先開啟Terminal並進入專案目錄(cd 專案目錄)
    P.S. 可以直接拖拉資料夾到Terminal就可以直接貼上路徑
  2. 輸入pod init來產生Podfile
  3. 輸入open -a Xcode Podfile來開啟Podfile檔案
  4. 將註解拿掉,並輸入pod ‘Firebase/Auth’跟‘Firebase/Firestore’(下圖)
  5. command + S來儲存Podfile
  6. 回Terminal,輸入pod install即可開始安裝Realm
  7. 之後請用.wcworkspace檔案來做任何編輯

Firebase建立專案

這裡完全參照彼得潘的文章操作,很簡單完全不會有失誤的時候XD…

P.S.登入方法很多樣,由於這裡是用Email/Password,所以要將資料庫端的功能開啟

很簡易的登入格式

Initialize the Firebase SDK

需要在AppDelegate.swift上初始化Firebase

記得要先初始化才可以使用Firebase的各種功能喔!

Firebase Authentication

Authentication是針對各種使用者身份的安全檢測機制,除了密碼認證之外,也有各種平台的認證(Facebook/Google/Apple…等),這裡先練習使用最普遍信箱密碼註冊、登入、登出

  • 註冊新使用者-Create a password-based account
  1. 判斷所有textField是否有值
  2. 判斷Re-confirm password是否與Password欄位一致
  3. 註冊使用者
檢查好所有textField就可以註冊使用者的資料(email/password)
  • 由於這邊會出現各種error可能性,要一直使用到UIAlertController,所以就自定一個function來囊括UIAlertController與UIAlertAction功能
func popAlert(title: String, message: String, alertTitle: String, action: ((UIAlertAction) -> Void)?) {let controller = UIAlertController(title: title, message: message, preferredStyle: .alert)let alert = UIAlertAction(title: alertTitle, style: .cancel, handler: action)controller.addAction(alert)present(controller, animated: true, completion: nil)}
  • textField判斷式也有兩種情況
/*Check all textFields and validate that the data is correct,if everything is correct, this method returns nil,otherwise, it will return error message1. 確保所有textField都有輸入值2. 確保passwordTextField/repasswordTextField值是相同的*/func validateTextField() -> String? {// Check all textFields are filled in, 檢查所有的textField是否都有輸入if emailTextField.text?.trimmingCharacters(in: .whitespacesAndNewlines) == "" ||passwordTextField.text?.trimmingCharacters(in: .whitespacesAndNewlines) == "" ||repasswordTextField.text?.trimmingCharacters(in: .whitespacesAndNewlines) == "" ||userNameTextField.text?.trimmingCharacters(in: .whitespacesAndNewlines) == "" {return "Please fill in all fields."}// Check wether repassword is same as password, 檢查重新輸入密碼是否與密碼一致if repasswordTextField.text != passwordTextField.text {return "Password do not match, please enter password again."}return nil}
註冊成功就可以在Firebase上看到基本資訊囉!(也可以自己對資料庫手動新增-Add user)
  • 登入使用者-Sign in a user with an email address and password

判斷方式跟註冊差不多

  1. 判斷所有textField是否有值
  2. 登入使用者
  • 登出使用者-Sign out

由於要將畫面切到最初始的第一頁,所以設定了presentingViewController在最一開始的controller,然後用dismiss來將畫面跳回第一頁

整段code全摳,完全不用改(開心

Firebase Firestore

Firestore也需要在AppDelegate.swift上做初始化

Firestore負責儲存帳號以外的資訊,這邊所儲存的是user name(使用者名稱)

Firestore有分成collection/document/data,就像資料夾(collection)裡有文件(document)上面有各種資料(data)
  • 新增資料

collection跟後面data的命名都會直接傳到firestore的資料庫,當要讀取的時候必須相同才能抓取資料

  • 讀取資料

在登入與註冊完帳號後移動到主要畫面並顯示使用者名稱

只抓使用者名稱
利用DocumentSnapshot可以抓取data
更新檔案的時候就會重新抓取

APP Demo

後記

功能理解中…不過還是先感謝Leo跟彼得潘的幫助,讓簡易的登入系統一點都不簡單QQ

Github

Reference

--

--