Guest User

Untitled

a guest
Jan 23rd, 2018
81
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.26 KB | None | 0 0
  1. import UIKit
  2. import Firebase
  3. import FacebookCore
  4. import FacebookLogin
  5.  
  6.  
  7. class loginVC: UIViewController {
  8.  
  9. @IBOutlet weak var emailField: UITextField!
  10. @IBOutlet weak var passwordField: UITextField!
  11. @IBOutlet weak var facebookBtn: UIButton!
  12. @IBOutlet weak var googleBtn: UIButton!
  13.  
  14. override func viewDidLoad() {
  15. super.viewDidLoad()
  16.  
  17. // prepare the fields to be used - requires the exention at the bottom UITextFieldDelegate
  18. emailField.delegate = self
  19. passwordField.delegate = self
  20. }
  21.  
  22. override func viewWillAppear(_ animated: Bool) {
  23. super.viewWillAppear(animated)
  24.  
  25. if Auth.auth().currentUser != nil {
  26.  
  27. // if already logged in - send to feedVC
  28. self.view.window!.rootViewController?.dismiss(animated: false, completion: nil)
  29.  
  30. }
  31.  
  32.  
  33. }
  34.  
  35. @IBAction func facebookBtnWasPressed(_ sender: Any) {
  36. FacebookLoginButtonClicked()
  37.  
  38. }
  39.  
  40.  
  41. @IBAction func googleBtnWasPressed(_ sender: Any) {
  42.  
  43. }
  44.  
  45.  
  46. @IBAction func loginBtnWasPressed(_ sender: Any) {
  47. if emailField.text != nil && passwordField.text != nil {
  48.  
  49. AuthService.instance.loginUser(withEmail: emailField.text!, andPassword: passwordField.text!, loginComplete: { (success, loginError) in
  50.  
  51.  
  52. if success {
  53. // if login is successful - send user to feedVC
  54. self.view.window!.rootViewController?.dismiss(animated: false, completion: nil)
  55.  
  56. //
  57. print("Successfully logged in user")
  58. //self.dismiss(animated: true, completion: nil)
  59. } else {
  60. print(String(describing: loginError?.localizedDescription))
  61. }
  62.  
  63. })
  64.  
  65. }
  66. }
  67.  
  68. @IBAction func iDontHaveAnAccountBtnWasPressed(_ sender: Any) {
  69. // Show signup view controller instead
  70. let signupVC = storyboard?.instantiateViewController(withIdentifier: "signupVC")
  71. present(signupVC!, animated: false, completion: nil)
  72. }
  73.  
  74. @IBAction func forgottenMyPasswordBtnWasPressed(_ sender: Any) {
  75.  
  76.  
  77. Auth.auth().sendPasswordReset(withEmail: emailField.text!) { (error) in
  78. // ...
  79. }
  80.  
  81. }
  82.  
  83.  
  84.  
  85. @objc func FacebookLoginButtonClicked() {
  86. let loginManager = LoginManager()
  87. loginManager.logIn(readPermissions: [.publicProfile, .email, .userFriends], viewController: self) { loginResult in // request access to user's facebook details
  88. switch loginResult {
  89. case .failed(let error):
  90. print(error)
  91. case .cancelled:
  92. print("User cancelled login.")
  93. case .success(let grantedPermissions, let declinedPermissions, let accessToken):
  94. print(grantedPermissions)
  95. print(declinedPermissions)
  96.  
  97. // Get Facebook credentials
  98. let credential = FacebookAuthProvider.credential(withAccessToken: (AccessToken.current?.authenticationToken)!)
  99. let FacebookAccessToken = accessToken
  100.  
  101. // Check if existing user are already logged into Firebase
  102. let currentUser = Auth.auth().currentUser
  103.  
  104. if currentUser != nil {
  105. // Check if user already exists in Firebase, if true link accounts
  106. Auth.auth().currentUser!.link(with: credential) {(user, error) in
  107. if user != nil && error == nil {
  108. // Linking accounts was a success
  109. print("Linking of Facebook account with existing email and password successfull")
  110. }
  111.  
  112. if let error = error {
  113. print(error)
  114. return
  115. }
  116. }
  117. }
  118.  
  119.  
  120.  
  121. else {
  122.  
  123. Auth.auth().signIn(with: credential) { (user, error) in
  124. if let error = error {
  125. print(error)
  126. return
  127. }
  128. // User is signed in
  129. // ...
  130. print("User signed in with Firebase")
  131. }
  132. }
  133.  
  134. }
  135.  
  136.  
  137. }
  138. }
  139. }
  140.  
  141.  
  142.  
  143.  
  144.  
  145.  
  146. extension loginVC: UITextFieldDelegate {
  147.  
  148. }
Add Comment
Please, Sign In to add comment