Advertisement
Guest User

Untitled

a guest
May 20th, 2016
90
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.61 KB | None | 0 0
  1. import UIKit
  2. import FirebaseAuth
  3.  
  4. @IBOutlet weak var emailField: UITextField!
  5.  
  6. @IBOutlet weak var passwordField: UITextField!
  7.  
  8. override func viewDidAppear(animated: Bool) {
  9. if let user = FIRAuth.auth()?.currentUser { //error here
  10. self.signedIn(user)
  11. }
  12. }
  13.  
  14. @IBAction func didTapSignIn(sender: AnyObject) {
  15. // Sign In with credentials.
  16. let email = emailField.text
  17. let password = passwordField.text
  18. FIRAuth.auth()?.signInWithEmail(email!, password: password!) { //error here (user, error) in
  19. if let error = error {
  20. print(error.localizedDescription)
  21. return
  22. }
  23. self.signedIn(user!)
  24. }
  25. }
  26. @IBAction func didTapSignUp(sender: AnyObject) {
  27. let email = emailField.text
  28. let password = passwordField.text
  29. FIRAuth.auth()?.createUserWithEmail(email!, password: password!) { // error here(user, error) in
  30. if let error = error {
  31. print(error.localizedDescription)
  32. return
  33. }
  34. self.setDisplayName(user!)
  35. }
  36. }
  37.  
  38. func setDisplayName(user: FIRUser) {
  39. let changeRequest = user.profileChangeRequest()
  40. changeRequest.displayName = user.email!.componentsSeparatedByString("@")[0]
  41. changeRequest.commitChangesWithCompletion(){ (error) in
  42. if let error = error {
  43. print(error.localizedDescription)
  44. return
  45. }
  46. self.signedIn(FIRAuth.auth()?.currentUser) //error here
  47. }
  48. }
  49.  
  50. @IBAction func didRequestPasswordReset(sender: AnyObject) {
  51. let prompt = UIAlertController.init(title: nil, message: "Email:", preferredStyle: UIAlertControllerStyle.Alert)
  52. let okAction = UIAlertAction.init(title: "OK", style: UIAlertActionStyle.Default) { (action) in
  53. let userInput = prompt.textFields![0].text
  54. if (userInput!.isEmpty) {
  55. return
  56. }
  57. FIRAuth.auth()?.sendPasswordResetWithEmail(userInput!) { //error here (error) in
  58. if let error = error {
  59. print(error.localizedDescription)
  60. return
  61. }
  62. }
  63. }
  64. prompt.addTextFieldWithConfigurationHandler(nil)
  65. prompt.addAction(okAction)
  66. presentViewController(prompt, animated: true, completion: nil);
  67. }
  68.  
  69. func signedIn(user: FIRUser?) {
  70. MeasurementHelper.sendLoginEvent()
  71.  
  72. AppState.sharedInstance.displayName = user?.displayName ?? user?.email
  73. AppState.sharedInstance.photoUrl = user?.photoURL
  74. AppState.sharedInstance.signedIn = true
  75. NSNotificationCenter.defaultCenter().postNotificationName(Constants.NotificationKeys.SignedIn, object: nil, userInfo: nil)
  76. // performSegueWithIdentifier(Constants.Segues.SignInToFp, sender: nil)
  77. }
  78.  
  79. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement