Advertisement
Guest User

Untitled

a guest
Jan 9th, 2017
154
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.34 KB | None | 0 0
  1. FIRAuth.auth()?.addStateDidChangeListener { auth, user in
  2. if let _user = user {
  3. if _user.isAnonymous {
  4. print("User logged in as anonymous. Saving uid to user defaults storage.")
  5. UserDefaults.standard.setValue(_user.uid, forKey: "uid")
  6. } else {
  7. print("User logged in with email: (_user.email)")
  8. }
  9. } else {
  10. FIRAuth.auth()?.signInAnonymously() { (user, error) in
  11. if let _error = error {
  12. print("Anonymous signIn error: (_error)")
  13. }
  14. }
  15. }
  16. }
  17.  
  18. let credential = FIREmailPasswordAuthProvider.credential(withEmail: emailField.text!, password: passwordField.text!)
  19. FIRAuth.auth()?.currentUser?.link(with: credential, completion: { user, error in
  20. if error == nil {
  21. FIRAuth.auth()!.signIn(withEmail: emailField.text!,
  22. password: passwordField.text!)
  23. }
  24. })
  25.  
  26. if let prevUserUID = UserDefaults.standard.string(forKey: "uid"), prevUserUID != _user.uid {
  27. FIRDatabase.database().reference().child("todo-items").queryOrdered(byChild: "user").queryEqual(toValue: prevUserUID).observe(.value, with: { snapshot in
  28. for item in snapshot.children {
  29. var todoItem = TodoItem(snapshot: item as! FIRDataSnapshot)
  30. todoItem.user = _user.uid
  31. todoItem.ref?.setValue(todoItem.toAnyObject())
  32. }
  33. })
  34. print("Data migrated.")
  35. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement