已经快要到三分之二了,标题名称取到山穷水尽
FirebaseAuth.Auth.auth().signIn(withEmail: email, password: password, completion: { [weak self] authResult, error in // weak self prevents from retention cycle
guard let strongSelf = self else {
return
}
guard let result = authResult, error == nil else {
print("failed to login with email: \(email)")
return
}
let user = result.user
print("logged in user: \(user)")
strongSelf.navigationController?.dismiss(animated: true, completion: nil)
})
DatabaseManager.shared.userExists(with: email, completion: { [weak self] exists in
guard let strongSelf = self else { // prevent from memory leak
return
}
guard !exists else {
strongSelf.alertUserLoginError(message: "user for that email address already exists")
return
}
FirebaseAuth.Auth.auth().createUser(withEmail: email, password: password, completion: { authResult, error in
guard authResult != nil, error == nil else {
print("error creating user")
return
}
// data entry
DatabaseManager.shared.insertUser(with: ChatAppUser(firstName: firstname,
lastName: lastname,
emailAddress: email))
// todo: flow check
strongSelf.navigationController?.dismiss(animated: true, completion: nil)
})
})
import Foundation
import FirebaseDatabase
final class DatabaseManager {
static let shared = DatabaseManager() // singleton
//private let database = Database.database().reference()
private let database = Database.database(url: "($DATABASE_URL)").reference()
}
// MARK: - Account Management
extension DatabaseManager {
public func userExists(with email: String,
completion: @escaping ((Bool) -> Void)) {
var safeEmail = email.replacingOccurrences(of: ".", with: "-")
safeEmail = safeEmail.replacingOccurrences(of: "@", with: "=")
// observe data
database.child(safeEmail).observeSingleEvent(of: .value, with: { snapshot in
guard snapshot.value as? String != nil else {
completion(false)
return
}
completion(true)
})
}
/// Insert new user to database
public func insertUser(with user: ChatAppUser){
database.child(user.safeEmail).setValue([
"first_name": user.firstName,
"last_name": user.lastName
])
}
}
struct ChatAppUser {
let firstName: String
let lastName: String
let emailAddress: String
// let profilePictureUrl: String
var safeEmail: String {
var safeEmail = emailAddress.replacingOccurrences(of: ".", with: "-")
safeEmail = safeEmail.replacingOccurrences(of: "@", with: "=")
return safeEmail
}
}
感谢Firebase,不用造轮的日子真的是非常快乐啊!
<<: [Day 21] - Django-REST-Framework APIView 介绍 -- 处理 HTTP Method 的好帮手
天亮了 昨晚是平安夜 关於迷雾森林故事 确认身份别恍神 8号:先评论一下午5号,感觉蛮好的整个人蛮放...
前言 第二次参加铁人赛,在决定参赛时,就又让人想起连续30天不间断发文的痛苦,但是要用什麽主题来做为...
8 割点、桥、双连通元件 现在让我们回到无向图的演算法。给一张图,要判断这张图是否为连通图相当简单:...
创建App-主页界面 在登入页面设计、建设完成後,进入第二个页面建设啦。主页包括了广告,作业、讨论区...
DBABootcamp SQL Server 主要的系统资料库有以下 4 种。 — master 资...