我目前正在实现用户使用Google登录的可能性。
如何检测到该确切的Google用户以前是否已登录以从数据库中获取数据。到目前为止,当用户已经登录时,使用Google登录会覆盖该条目在数据库中。
是否可以检查以前是否已设置身份验证ID?
class AppDelegate: UIResponder, UIApplicationDelegate, GIDSignInDelegate {
var window: UIWindow?
func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey: Any]?) -> Bool {
FirebaseApp.configure()
GIDSignIn.sharedInstance().clientID = FirebaseApp.app()?.options.clientID
GIDSignIn.sharedInstance().delegate = self
Database.database().isPersistenceEnabled = true
...
return true
}
// MARK: - Google SignIn
func sign(_ signIn: GIDSignIn!, didSignInFor user: GIDGoogleUser!, withError error: Error?) {
if let error = error {
print("Failed logging into Google: ", error)
return
}
print("Successfully logged into Google.")
guard let authentication = user.authentication else { return }
let credential = GoogleAuthProvider.credential(withIDToken: authentication.idToken,
accessToken: authentication.accessToken)
Auth.auth().signIn(with: credential) { (acc, error) in
if let error = error {
print("Failed to create User with Google: ", error)
return
}
self.upDataToDatabase(from: user)
print("Successfully created user with Google.")
}
}
func upDataToDatabase(from user: GIDGoogleUser) {
guard let uid = Auth.auth().currentUser?.uid else { return }
let databaseRefUser = Database.database().reference().child("users/\(uid)")
let userObject = [
"name": String(user.profile.name),
"email": String(user.profile.email),
] as [String: Any]
databaseRefUser.setValue(userObject) { (error, ref) in
if error != nil {
print("Error creating Database connection: ", error as Any)
return
} else {
let storyboard = UIStoryboard(name: "Main", bundle: nil)
let controller = storyboard.instantiateViewController(withIdentifier: "homeView") as! HomeViewController
self.window?.rootViewController = controller
self.window?.makeKeyAndVisible()
}
}
}
@available(iOS 9.0, *)
func application(_ application: UIApplication, open url: URL, options: [UIApplication.OpenURLOptionsKey : Any])
-> Bool {
return GIDSignIn.sharedInstance().handle(url,
sourceApplication:options[UIApplication.OpenURLOptionsKey.sourceApplication] as? String,
annotation: [:])
}
func application(_ application: UIApplication, open url: URL, sourceApplication: String?, annotation: Any) -> Bool {
return GIDSignIn.sharedInstance().handle(url,
sourceApplication: sourceApplication,
annotation: annotation)
}
答案 0 :(得分:0)
用户的Firebase用户ID在删除之前始终保持不变。
[
'name' => '...'
'email' => '...'
'phone' => '...'
'Address' => [
'street' => '...'
'number' => '...'
'city' => '...'
'state' => '...'
]
]
检查databaseRefUser是否已存在,否则不要更新数据库的setValue。