Skip to content

Instantly share code, notes, and snippets.

@keith-kurak
Last active July 3, 2022 07:30
Show Gist options
  • Save keith-kurak/7503344811fb508357749b7b102860d3 to your computer and use it in GitHub Desktop.
Save keith-kurak/7503344811fb508357749b7b102860d3 to your computer and use it in GitHub Desktop.
Stupid Simple iOS Firebase Authentication Setup
// AppDelegate.swift --------------------------------------------------------------------------------------------
//Add this stuff to AppDelegate (in addition to whatever is already in there) to enable Firebase and the auth-specific URL handlers
class AppDelegate: UIResponder, UIApplicationDelegate {
var window: UIWindow?
func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplicationLaunchOptionsKey: Any]?) -> Bool {
//setup firebase
FIRApp.configure()
return true
}
// MARK: URL handling for OAuth
@available(iOS 9.0, *)
func application(_ app: UIApplication, open url: URL, options: [UIApplicationOpenURLOptionsKey : Any]) -> Bool {
let sourceApplication = options[UIApplicationOpenURLOptionsKey.sourceApplication] as! String?
return self.handleOpenUrl(url, sourceApplication: sourceApplication)
}
func handleOpenUrl(_ url: URL, sourceApplication: String?) -> Bool {
if FUIAuth.defaultAuthUI()?.handleOpen(url, sourceApplication: sourceApplication) ?? false {
return true
}
// other URL handling goes here.
return false
}
}
// SignInSignoutViewController.swift -----------------------------------------------------------------------------
// This is the actual VC with the sign in button that will open Firebase's Auth UI.
import Foundation
import UIKit
import Firebase
import FirebaseAuthUI
import FirebaseGoogleAuthUI
import FirebaseFacebookAuthUI
class SignInSignOutViewController: UITableViewController {
fileprivate var signInButtonText = "Sign In"
override func viewWillAppear(_ animated: Bool) {
super.viewWillAppear(animated)
self.authStateDidChangeHandle = self.auth?.addStateDidChangeListener(self.handleAuthStateChanged(auth:user:))
updateUI()
}
override func viewWillDisappear(_ animated: Bool) {
super.viewWillDisappear(animated)
if let handle = self.authStateDidChangeHandle {
self.auth?.removeStateDidChangeListener(handle)
}
}
//MARK: table
override func numberOfSections(in tableView: UITableView) -> Int {
return 1
}
override func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return 1
}
override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCell(withIdentifier: "signInCell", for: indexPath)
cell.textLabel?.text = signInButtonText
return cell
}
override func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
showLoginView()
}
//MARK: Firebase auth UI stuff
fileprivate var authStateDidChangeHandle: FIRAuthStateDidChangeListenerHandle?
fileprivate(set) var auth: FIRAuth? = FIRAuth.auth()
fileprivate(set) var authUI: FUIAuth? = FUIAuth.defaultAuthUI()
func showLoginView() {
if (self.auth?.currentUser) != nil {
do {
try self.authUI?.signOut()
} catch let error {
// Again, fatalError is not a graceful way to handle errors.
// This error is most likely a network error, so retrying here
// makes sense.
fatalError("Could not sign out: \(error)")
}
} else {
self.authUI?.isSignInWithEmailHidden = true
// If you haven't set up your authentications correctly these buttons
// will still appear in the UI, but they'll crash the app when tapped.
self.authUI?.providers = [FUIGoogleAuth(), FUIFacebookAuth()]
let controller = self.authUI!.authViewController()
self.present(controller, animated: true, completion: nil)
}
}
func handleAuthStateChanged(auth: FIRAuth, user: FIRUser?) {
updateUI()
}
func updateUI() {
if (self.auth?.currentUser) != nil {
self.signInButtonText = "Sign Out";
} else {
self.signInButtonText = "Sign In";
}
self.tableView.reloadData()
}
}
@colin-m-davis
Copy link

Sick

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment