Search code examples
iosfirebasefirebase-authenticationfirebaseui

Customize FUIAuthPickerViewController


How can I customize the Firebase UI Auth Picker controller with custom buttons, custom actions, background, loader etc..

I already try to subclass the FUIAuthPickerViewController but we can't access to login buttons


Solution

  • This is how you can create your own class of FUIAuthPickerViewController:

    Create FUICustomLoginController.swift with:

    import UIKit
    import FirebaseUI
    import FirebaseAuth
    
    class FUICustomLoginController: ViewController {
    
        var authUI: FUIAuth! = FUIAuth.defaultAuthUI()
        var auth: Auth = Auth.auth()
    
        private func didSignIn(auth: AuthCredential?, error: Error?, callBack: AuthResultCallback?) {
            let callBack: (AuthDataResult?, Error?) -> Void = { [unowned self] result, error in
                callBack?(result?.user, error)
                self.authUI.delegate?.authUI?(self.authUI, didSignInWith: result, error: error)
            }
            if let auth = auth {
                self.auth.signInAndRetrieveData(with: auth, completion: callBack)
            } else if let error = error {
                callBack(nil, error)
            }
        }
    
        func signIn<T: FUIAuthProvider>(type: T.Type, defaultValue: String? = nil) {
            try? self.authUI.signOut() // logout from google etc..
            self.authUI.providers.first(where: { $0 is T })?.signIn(withDefaultValue: defaultValue, presenting: self, completion: self.didSignIn)
        }
    }
    

    Subclass your controller from FUICustomLoginController:

    class LoginPickerController: FUICustomLoginController {
    
        override func viewDidLoad() {
            super.viewDidLoad()
    
            // Customize authUI if needed
            //self.authUI.providers = ... 
            self.authUI.delegate = self
        }
    
        @IBAction func loginFacebook(_ sender: Any) {
            self.signIn(type: FUIFacebookAuth.self)
        }
    
        @IBAction func loginGoogle(_ sender: Any) {
            self.signIn(type: FUIGoogleAuth.self)
        }
    
        @IBAction func loginPhone(_ sender: Any) {
            self.signIn(type: FUIPhoneAuth.self)
        }
    }
    
    extension LoginPickerController: FUIAuthDelegate {
    
        func authUI(_ authUI: FUIAuth, didSignInWith authDataResult: AuthDataResult?, error: Error?) {
            // perform login actions
        }
    }