Advertisement
Guest User

Untitled

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