昨天我们已经将注册帐号、帐号登入实作完成了,今天我们要来把剩下的帐号登出以及密码重设功能来实作完成
@IBAction func accountSignInOrSignOut(_ sender: UIButton) {
if (Auth.auth().currentUser == nil || !isSignIn) {
// 无用户登入
if (accountTF.text == "" || passwordTF.text == "") {
CustomFunc.customAlert(title: "请重新输入帐号密码!", message: "", vc: self, actionHandler: nil)
} else {
Auth.auth().signIn(withEmail:accountTF.text!, password: passwordTF.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.accountTF.text = ""
self.passwordTF.text = ""
self.isSignIn = true
self.signInOrSignOutBtn.setTitle("帐号登出", for: .normal)
}
}
} else {
// 有用户登入
// 登出功能,下一篇才会教喔!
}
}
今天我们要来继续把帐号登出也就是 else 的部分完成
在 else 里面加入下面这段,透过 Firebase 提供的 signOut() API,我们可以很轻松的实作帐号登出功能
// 有用户登入
do {
try Auth.auth().signOut()
CustomFunc.customAlert(title: "登出成功!", message: "", vc: self, actionHandler: nil)
self.accountTF.text = ""
self.passwordTF.text = ""
self.isSignIn = false
self.signInOrSignOutBtn.setTitle("帐号登入", for: .normal)
} catch let error as NSError {
CustomFunc.customAlert(title: "", message: "\(String(describing: error.localizedDescription))", vc: self, actionHandler: nil)
}
接着是密码重设功能,在密码重设按钮的 IBAction 加入下面这段
透过 Firebase 提供的 sendPasswordReset() API,我们可以很轻松的实作密码重设功能
@IBAction func resetPassword(_ sender: UIButton) {
if (accountTF.text == "") {
CustomFunc.customAlert(title: "", message: "请输入要重设密码的 Email!", vc: self, actionHandler: nil)
} else {
Auth.auth().sendPasswordReset(withEmail: accountTF.text!, completion: { (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: nil)
})
}
}
密码重设的信件如下图
密码重设的画面如下图
再来是我们要如何知道,目前是否有使用者登入,我们可以透过加入帐号状态监听的方式来达成
先宣告一个变数 handle,型别为「AuthStateDidChangeListenerHandle?」,因为在没有使用者的状态下会为 nil,所以在型别的後面要加个「?」来告知说,此变数有可能会有空值的状况
var handle: AuthStateDidChangeListenerHandle?
然後在 viewWillAppear 里面加入帐号状态监听的 Function,当画面将要出现的时候,就开始监听
// MARK: - 加入 Firebase 帐号状态监听
override func viewWillAppear(_ animated: Bool) {
super.viewWillAppear(animated)
handle = Auth.auth().addStateDidChangeListener { auth, user in
if (user != nil) {
if (self.isSignIn) {
print("目前已有使用者登入!")
self.signInOrSignOutBtn.setTitle("帐号登出", for: .normal)
self.passwordTF.isEnabled = false
} else {
self.signInOrSignOutBtn.setTitle("帐号登入", for: .normal)
self.passwordTF.isEnabled = true
}
} else {
// 目前尚无用户登入
print("目前尚无用户登入!")
}
}
}
接着在 viewWillDisappear 里面加入帐号状态监听移除的 Function,当画面将要消失的时候,就移除监听
// MARK: - 移除 Firebase 帐号状态监听
override func viewWillDisappear(_ animated: Bool) {
super.viewWillDisappear(animated)
Auth.auth().removeStateDidChangeListener(handle!)
}
後面两篇要来教大家,如何透过 Firebase 来实作第三方帐号登入功能
本篇的参考范例程序码:Github
<<: Day 16【web3.js】I KNOW BINARY AND HEXADECIMAL
如果你是写 C/C++ 的开发者,应该对记忆体管理并不陌生,如果你是後端开发者,应该会常常注意服务...
excel 是很多人常用来处理报表与数据的工具,同样地在 python 也有一个套件常常在处理这件事...
昨天写的小网站後续看文章时觉得有一部分JS有硬干的嫌疑(?) 所以把一些部分做了改动 首先使用了HT...
这次的问题是上一篇文的延伸 上一篇是使用者输入参数後,再将指定栏位的值改为1 那这次是输入参数後检查...