Advertisement
Guest User

Untitled

a guest
Sep 6th, 2017
97
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.52 KB | None | 0 0
  1. Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '*** setObjectForKey: key cannot be nil'
  2.  
  3. @IBAction func createAccount(_ sender: Any) {
  4. if let email = emailField.text, let pwd = pwdField.text{
  5. Auth.auth().createUser(withEmail: email, password: pwd, completion: { (user, error) in
  6. if error != nil {
  7. print("Unable to Authenticate E-mail with Firebase - Error: (String(describing: error))")
  8. }else{
  9. print("Successfully Authenticated E-mail with Firebase")
  10. if let usr = user {
  11. let keychainResult = KeychainWrapper.standard.set(usr.uid, forKey: KEY_UID)
  12. print("Data saved to Keychain: (keychainResult)")
  13. DataService.ds.createFirebaseDBUser(uid: usr.uid, userData: ["provider": "Email"])
  14. }
  15. }
  16. })
  17. }
  18. }
  19.  
  20. @IBAction func facebookLogin(_ sender: Any) {
  21. print("Start Login Process")
  22. let fbLogin = FBSDKLoginManager()
  23. print("Set Read Permissions")
  24. //TODO: add Read Permissions
  25. fbLogin.logIn(withReadPermissions: ["email","user_location"], from: self) { (result, error) in
  26. if error != nil{
  27. print("Unable to provide Authentication with Facebook - (String(describing: error))")
  28. }else if result?.isCancelled == true{
  29. print("User Cancelled FB Authentication")
  30. }else {
  31. print("User Successfully Logged In")
  32. let credential = FacebookAuthProvider.credential(withAccessToken: FBSDKAccessToken.current().tokenString)
  33. self.firebaseAuth(credential)
  34. }
  35. }
  36.  
  37. }
  38.  
  39. @IBAction func emailLogin(_ sender: Any) {
  40. if let email = emailField.text, let password = passwordField.text{
  41. Auth.auth().signIn(withEmail: email, password: password, completion: { (user, error) in
  42. if error != nil{
  43. print("Unable to Authenticate Firebase - (String(describing: error))")
  44. } else {
  45. print("Succesfully authenticated with Firebase")
  46. if let user = user{
  47. let userData = ["provider": "email"]
  48. self.completeSignIn(id: user.uid, userData: userData)
  49. }
  50. }
  51.  
  52. })
  53. }
  54. }
  55.  
  56.  
  57. func firebaseAuth(_ credential: AuthCredential){
  58. Auth.auth().signIn(with: credential, completion: { (user, error) in
  59. if error == nil {
  60. print("Succesfully authenticated with Firebase")
  61. if let user = user{
  62. let userData = ["provider": credential.provider]
  63. self.completeSignIn(id: user.uid, userData: userData)
  64. }
  65. } else{
  66. print("Unable to Authenticate Firebase - (String(describing: error))")
  67. }
  68. })
  69. }
  70.  
  71.  
  72. func completeSignIn(id: String, userData: Dictionary<String, String>){
  73. DataService.ds.createFirebaseDBUser(uid: id, userData: userData)
  74. let keychainResult = KeychainWrapper.standard.set(id, forKey: KEY_UID)
  75. print("Data saved to Keychain: (keychainResult)")
  76. performSegue(withIdentifier: "goToMain", sender: nil)
  77. }
  78.  
  79. import Foundation
  80. import Firebase
  81.  
  82. let DB_BASE = Database().reference()
  83.  
  84. class DataService {
  85.  
  86. static let ds = DataService()
  87.  
  88. private(set) var REF_BASE = DB_BASE
  89. private(set) var REF_PROMOS = DB_BASE.child("promos")
  90. private(set) var REF_NEGOCIOS = DB_BASE.child("negocios")
  91. private(set) var REF_USERS = DB_BASE.child("users")
  92.  
  93. func createFirebaseDBUser(uid: String, userData: Dictionary<String, String>){
  94. REF_USERS.child(uid).updateChildValues(userData)
  95. }
  96.  
  97. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement