Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import UIKit
- import Firebase
- import FacebookCore
- import FacebookLogin
- class loginVC: UIViewController {
- @IBOutlet weak var emailField: UITextField!
- @IBOutlet weak var passwordField: UITextField!
- @IBOutlet weak var facebookBtn: UIButton!
- @IBOutlet weak var googleBtn: UIButton!
- override func viewDidLoad() {
- super.viewDidLoad()
- // prepare the fields to be used - requires the exention at the bottom UITextFieldDelegate
- emailField.delegate = self
- passwordField.delegate = self
- }
- override func viewWillAppear(_ animated: Bool) {
- super.viewWillAppear(animated)
- if Auth.auth().currentUser != nil {
- // if already logged in - send to feedVC
- self.view.window!.rootViewController?.dismiss(animated: false, completion: nil)
- }
- }
- @IBAction func facebookBtnWasPressed(_ sender: Any) {
- FacebookLoginButtonClicked()
- }
- @IBAction func googleBtnWasPressed(_ sender: Any) {
- }
- @IBAction func loginBtnWasPressed(_ sender: Any) {
- if emailField.text != nil && passwordField.text != nil {
- AuthService.instance.loginUser(withEmail: emailField.text!, andPassword: passwordField.text!, loginComplete: { (success, loginError) in
- if success {
- // if login is successful - send user to feedVC
- self.view.window!.rootViewController?.dismiss(animated: false, completion: nil)
- //
- print("Successfully logged in user")
- //self.dismiss(animated: true, completion: nil)
- } else {
- print(String(describing: loginError?.localizedDescription))
- }
- })
- }
- }
- @IBAction func iDontHaveAnAccountBtnWasPressed(_ sender: Any) {
- // Show signup view controller instead
- let signupVC = storyboard?.instantiateViewController(withIdentifier: "signupVC")
- present(signupVC!, animated: false, completion: nil)
- }
- @IBAction func forgottenMyPasswordBtnWasPressed(_ sender: Any) {
- Auth.auth().sendPasswordReset(withEmail: emailField.text!) { (error) in
- // ...
- }
- }
- @objc func FacebookLoginButtonClicked() {
- let loginManager = LoginManager()
- loginManager.logIn(readPermissions: [.publicProfile, .email, .userFriends], viewController: self) { loginResult in // request access to user's facebook details
- switch loginResult {
- case .failed(let error):
- print(error)
- case .cancelled:
- print("User cancelled login.")
- case .success(let grantedPermissions, let declinedPermissions, let accessToken):
- print(grantedPermissions)
- print(declinedPermissions)
- // Get Facebook credentials
- let credential = FacebookAuthProvider.credential(withAccessToken: (AccessToken.current?.authenticationToken)!)
- let FacebookAccessToken = accessToken
- // Check if existing user are already logged into Firebase
- let currentUser = Auth.auth().currentUser
- if currentUser != nil {
- // Check if user already exists in Firebase, if true link accounts
- Auth.auth().currentUser!.link(with: credential) {(user, error) in
- if user != nil && error == nil {
- // Linking accounts was a success
- print("Linking of Facebook account with existing email and password successfull")
- }
- if let error = error {
- print(error)
- return
- }
- }
- }
- else {
- Auth.auth().signIn(with: credential) { (user, error) in
- if let error = error {
- print(error)
- return
- }
- // User is signed in
- // ...
- print("User signed in with Firebase")
- }
- }
- }
- }
- }
- }
- extension loginVC: UITextFieldDelegate {
- }
Add Comment
Please, Sign In to add comment