Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Firebase does not show up sign page for microsoft.com in Swift / iOS

I am implementing the Firebase authentication by using OAuthProvider class to sign in with a personal microsoft account.

I have followed this instructions: https://firebase.google.com/docs/auth/ios/microsoft-oauth?authuser=0

However when I am using OAuthProvider of Firebase SDK it does not show up the sign in page of Microsoft, actually nothing gets invoked by getCredentialWith.

When I am using GoogleAuthProvider everything works fine and Firebase SDK shows up the sign in page of Google.

let provider = OAuthProvider(providerID: "microsoft.com")
provider.scopes = ["files.readwrite.appfolder", "user.read"]

provider.getCredentialWith(nil, completion: { credential, error in
  if let error = error {
    os_log("Firebase Error: %@", type: .fault, error as CVarArg)
    return
  }

  if (credential != nil) {
    Auth.auth().signInAndRetrieveData(with: credential!, completion: { authResult, error in
      if let error = error {
        os_log("Firebase Error: %@", type: .fault, error as CVarArg)
        return
      }
    })
  }
})
like image 890
Andreas Reuter Avatar asked Oct 20 '25 06:10

Andreas Reuter


2 Answers

Declare in global scope like below

var provider: OAuthProvider?
var authMicrosoft: Auth?


@IBAction func buttonTapped(_ sender: Any) {


     provider = OAuthProvider(providerID: "microsoft.com")


    provider?.customParameters = [
        "prompt": "consent",
                "login_hint": "",
    ]

    provider?.scopes = ["mail.read", "calendars.read"]




    provider?.getCredentialWith(nil ) { credential, error in
        if error != nil {
            // Handle error.
        }

        print(credential?.provider)



        if let x = credential {
            self. authMicrosoft?.signIn(with: x) { authResult, error in
                if error != nil {
                    // Handle error.
                }


                print(authResult?.additionalUserInfo?.profile)
                print(authResult?.user.providerID)


            }
        } else {

        }

    }

}
like image 114
yug k Avatar answered Oct 22 '25 21:10

yug k


It seems that you define a provider locally. While the getCredentialWith(_:completion) call gets executed asynchronously, your local function may already finished executing, and the provider may already be deallocated by ARC.

To solve your problem, you may need to specifically retain the provider pointer somewhere --- for instance in your view controller as a property or ivar, and by that way the provider can be created/recycled when your view controller is initialized/deallocated.

like image 22
Yue Wang Avatar answered Oct 22 '25 21:10

Yue Wang



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!