in authentication/LegacyAuthQuickstart/AuthenticationExampleSwift/MainViewController.swift [120:338]
func showAuthPicker(_ providers: [AuthProvider]) {
let picker = UIAlertController(title: "Select Provider",
message: nil,
preferredStyle: .alert)
for provider in providers {
var action: UIAlertAction
switch provider {
case .authEmail:
action = UIAlertAction(title: "Email", style: .default) { UIAlertAction in
self.performSegue(withIdentifier: "email", sender: nil)
}
case .authEmailMFA:
action = UIAlertAction(title: "Email with MFA", style: .default) { UIAlertAction in
isMFAEnabled = true
self.performSegue(withIdentifier: "email", sender: nil)
}
case .authPasswordless:
action = UIAlertAction(title: "Passwordless", style: .default) { UIAlertAction in
self.performSegue(withIdentifier: "passwordless", sender: nil)
}
case .authCustom:
action = UIAlertAction(title: "Custom", style: .default) { UIAlertAction in
self.performSegue(withIdentifier: "customToken", sender: nil)
}
case .authAnonymous:
action = UIAlertAction(title: "Anonymous", style: .default) { UIAlertAction in
self.showSpinner {
// [START firebase_auth_anonymous]
Auth.auth().signInAnonymously { authResult, error in
// [START_EXCLUDE]
self.hideSpinner {
if let error = error {
self.showMessagePrompt(error.localizedDescription)
return
}
}
// [END_EXCLUDE]
}
// [END firebase_auth_anonymous]
}
}
case .authApple:
if #available(iOS 13, *) {
action = UIAlertAction(title: "Apple", style: .default) { UIAlertAction in
self.startSignInWithAppleFlow()
}
} else {
continue
}
case .authFacebook:
action = UIAlertAction(title: "Facebook", style: .default) { UIAlertAction in
let loginManager = LoginManager()
loginManager.logIn(permissions: ["email"], from: self, handler: { result, error in
if let error = error {
self.showMessagePrompt(error.localizedDescription)
} else if result!.isCancelled {
print("FBLogin cancelled")
} else {
// [START headless_facebook_auth]
let credential = FacebookAuthProvider
.credential(withAccessToken: AccessToken.current!.tokenString)
// [END headless_facebook_auth]
self.firebaseLogin(credential)
}
})
}
case .authGoogle:
action = UIAlertAction(title: "Google", style: .default) { UIAlertAction in
self.startSignInWithGoogleFlow()
}
case .authTwitter:
action = UIAlertAction(title: "Twitter", style: .default) { UIAlertAction in
// [START firebase_auth_twitter]
self.twitterProvider?.getCredentialWith(_: nil) { credential, error in
self.showSpinner {
if let error = error {
self.hideSpinner {
self.showMessagePrompt(error.localizedDescription)
return
}
}
if let credential = credential {
Auth.auth().signIn(with: credential) { result, error in
self.hideSpinner {
if let error = error {
self.showMessagePrompt(error.localizedDescription)
return
}
}
}
}
}
}
// [END firebase_auth_twitter]
}
case .authGitHub:
action = UIAlertAction(title: "GitHub", style: .default) { UIAlertAction in
// [START firebase_auth_github]
self.gitHubProvider?.getCredentialWith(_: nil) { credential, error in
self.showSpinner {
if let error = error {
self.hideSpinner {
self.showMessagePrompt(error.localizedDescription)
return
}
}
if let credential = credential {
Auth.auth().signIn(with: credential) { result, error in
self.hideSpinner {
if let error = error {
self.showMessagePrompt(error.localizedDescription)
return
}
}
}
}
}
}
// [END firebase_auth_github]
}
case .authPhone:
action = UIAlertAction(title: "Phone", style: .default) { UIAlertAction in
self.showTextInputPrompt(withMessage: "Phone Number:") { userPressedOK, userInput in
if let phoneNumber = userInput {
self.showSpinner {
// [START phone_auth]
PhoneAuthProvider.provider()
.verifyPhoneNumber(phoneNumber, uiDelegate: nil) { verificationID, error in
// [START_EXCLUDE silent]
self.hideSpinner {
// [END_EXCLUDE]
if let error = error {
self.showMessagePrompt(error.localizedDescription)
return
}
// Sign in using the verificationID and the code sent to the user
// [START_EXCLUDE]
guard let verificationID = verificationID else { return }
self
.showTextInputPrompt(withMessage: "Verification Code:") { userPressedOK, verificationCode in
if let verificationCode = verificationCode {
// [START get_phone_cred]
let credential = PhoneAuthProvider.provider().credential(
withVerificationID: verificationID,
verificationCode: verificationCode
)
// [END get_phone_cred]
self.firebaseLogin(credential)
} else {
self.showMessagePrompt("verification code can't be empty")
}
}
}
// [END_EXCLUDE]
}
// [END phone_auth]
}
} else {
self.showMessagePrompt("phone number can't be empty")
}
}
}
case .authGameCenter:
action = UIAlertAction(title: "Game Center", style: .default) { UIAlertAction in
// [START firebase_auth_gamecenter]
GameCenterAuthProvider.getCredential { credential, error in
self.showSpinner {
if let error = error {
self.hideSpinner {
self.showMessagePrompt(error.localizedDescription)
return
}
}
if let credential = credential {
Auth.auth().signIn(with: credential) { result, error in
self.hideSpinner {
if let error = error {
self.showMessagePrompt(error.localizedDescription)
return
}
}
}
}
}
}
// [END firebase_auth_gamecenter]
}
case .authMicrosoft:
action = UIAlertAction(title: "Microsoft", style: .default) { UIAlertAction in
// [START firebase_auth_microsoft]
self.microsoftProvider?.getCredentialWith(_: nil) { credential, error in
self.showSpinner {
if let error = error {
self.hideSpinner {
self.showMessagePrompt(error.localizedDescription)
return
}
}
if let credential = credential {
Auth.auth().signIn(with: credential) { result, error in
self.hideSpinner {
if let error = error {
self.showMessagePrompt(error.localizedDescription)
return
}
}
}
}
}
}
// [END firebase_auth_microsoft]
}
}
picker.addAction(action)
}
picker.addAction(UIAlertAction(title: "Cancel", style: .cancel, handler: nil))
present(picker, animated: true, completion: nil)
}