В настоящее время я реализую возможность входа пользователя в Google.
Как я могу определить, ранее ли этот пользователь Google вошел в систему для получения данных из базы данных. Пока, когда пользователь уже вошел в систему в прошлом, вход в систему с Google перезаписывает запись в база данных.
Есть ли способ проверить, был ли ранее установлен идентификатор авторизации?
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)
}