今天是實作登入功能:
先建立一個變數用來儲存當前帳號是否登入:
var isLogin: Bool = false
程式碼:
if (Auth.auth().currentUser == nil || !isLogin) {
if (accountTextField.text == "" || passwordTextField.text == "") {
CustomFunc.customAlert(title: "請重新輸入帳號密碼!", message: "", vc: self, actionHandler: nil)
} else {
Auth.auth().signIn(withEmail:accountTextField.text!, password: passwordTextField.text!) { (user, error) in
guard error == nil else {
CustomFunc.customAlert(title: "", message: "\(String(describing: error!.localizedDescription))", vc: self, actionHandler: nil)
return
}
CustomFunc.customAlert(title: "登入成功!", message: "", vc: self, actionHandler: self.getFirebaseUserInfo)
self.accountTextField.text = ""
self.passwordTextField.text = ""
self.isLogin = true
}
}
}
下面這可做可不做:
getFirebaseUserInfo:
func getFirebaseUserInfo() {
let currentUser = Auth.auth().currentUser
guard let user = currentUser else {
CustomFunc.customAlert(title: "使用者資訊", message: "無法取得使用者資料", vc: self, actionHandler: nil)
return
}
let uid = user.uid
let email = user.email
CustomFunc.customAlert(title: "使用者資訊", message: "UID:\(uid)\nEmail:\(email!)", vc: self, actionHandler: nil)
}