Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- FIRAuth.auth()?.addStateDidChangeListener { auth, user in
- if let _user = user {
- if _user.isAnonymous {
- print("User logged in as anonymous. Saving uid to user defaults storage.")
- UserDefaults.standard.setValue(_user.uid, forKey: "uid")
- } else {
- print("User logged in with email: (_user.email)")
- }
- } else {
- FIRAuth.auth()?.signInAnonymously() { (user, error) in
- if let _error = error {
- print("Anonymous signIn error: (_error)")
- }
- }
- }
- }
- let credential = FIREmailPasswordAuthProvider.credential(withEmail: emailField.text!, password: passwordField.text!)
- FIRAuth.auth()?.currentUser?.link(with: credential, completion: { user, error in
- if error == nil {
- FIRAuth.auth()!.signIn(withEmail: emailField.text!,
- password: passwordField.text!)
- }
- })
- if let prevUserUID = UserDefaults.standard.string(forKey: "uid"), prevUserUID != _user.uid {
- FIRDatabase.database().reference().child("todo-items").queryOrdered(byChild: "user").queryEqual(toValue: prevUserUID).observe(.value, with: { snapshot in
- for item in snapshot.children {
- var todoItem = TodoItem(snapshot: item as! FIRDataSnapshot)
- todoItem.user = _user.uid
- todoItem.ref?.setValue(todoItem.toAnyObject())
- }
- })
- print("Data migrated.")
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement