Linking oAuth Providers in Firebase iOS - ios

I'm new to Firebase and iOS and I was wondering if someone knew how to link multiple oAuth Providers. I followed the Firebase docs and tried to implement this function:
func firebaseSignInWithLink(credential: FIRAuthCredential) {
FIRAuth.auth()?.signIn(with: credential, completion: { (user, error) in
if error != nil {
debugPrint("APP: there has been an error signing into firebase, perhaps another account with same email")
debugPrint("APP: \(error)")
// if existing email, try linking
FIRAuth.auth()?.currentUser?.link(with: credential, completion: { (user, error) in
if error != nil {
debugPrint("APP: there has been an error signing into firebase")
debugPrint("APP: \(error)")
}
else {
debugPrint("APP: successfully signed into firebase")
}
})
}
else {
debugPrint("APP: successfully signed into firebase")
}
})
}
The FIRAuth.auth()?.currentUser?.link function never gets called despite the above debugPrint("APP: \(error)") being called. Because this doesn't work, I keep getting the error below:
Optional(Error Domain=FIRAuthErrorDomain Code=17007 \"The email address is already in use by another account.\" UserInfo={NSLocalizedDescription=The email address is already in use by another account., error_name=ERROR_EMAIL_ALREADY_IN_USE, FIRAuthErrorUserInfoEmailKey=example#gmail.com})"
Any help would be greatly appreciated! Thank you :D

I believe you are confused by the instructions. On firebase documentation it reads
To link auth provider credentials to an existing user account:
Sign in the user using any authentication provider or method.
Complete the sign-in flow for the new authentication provider up to, but not including, calling one of the FIRAuth.signInWith methods. For example, get the user's Google ID token, Facebook access token, or email and password.
Get a FIRAuthCredential for the new authentication provider
So you should not call the method FIRAuth.signInWith. I should also point out that you want to create a link to an existing account, so you should have signed in first and then you can link it. This is why you should have a currentUser.

It occurred because of unchecking "Multiple accounts per email address setting firebase setting" from firebase console.

Related

In Docusign iOS sdk DSMManager.login(withEmail: ..) having some trouble

Docusign iOS sdk DSMManager.login(withEmail: ..) method have parameter withEmail, password, integrationkey, host. In integration key I'm passing my app's integration key and for host "https://demo.docusign.net/restapi/"
for email and password i did try my developer account credentials but it gaves
some : Error Domain=user.additional.data.fetch.error Code=-1011 "Unable to fetch additional data for user - The URL provided does not resolve to a resource. Consumer Disclosure does not exist." UserInfo={DSM_com.alamofire.serialization.response.error.response=<NSHTTPURLResponse: 0x600001c94120> { URL: https://demo.docusign.net/restapi/v2/accounts/********/consumer_disclosure } { Status Code: 404, Headers {
Code:
DSMManager.login(withEmail: "email*#gmail.com",
password: "123456",
integratorKey: integratorKey,
host: hostUrl) { (accountInfo, error) in
if (error != nil)
{
NSLog("Error logging in")
// display error prompt
}
else
{
NSLog("User authenticated")
// segue to main navigation controller
}
}
Please note that Legacy Authentication (using cleartext password) is no longer allowed for new applications being built with DocuSign.
Please see more details in the OAuth 2.0 migration page.
Blog post announcing the change..

Firebase Anonymous Login - Why FirebaseID changes when logged to Facebook, but it remains the same when not logged to Facebook?

I have the following logic for allowing guest users to login to my app:
(1) Login as Anonymous.
(2) Check if Facebook is logged.
(3) If it is logged to Facebook, link to Anonymous.
(4) If link fails, Login to firebase passing facebook token to Firebase
If I am not logged in Facebook the Anonymous ID given by firebase after step (1) is always the same. However, the first time I login to Facebook, I link the account to firebase as in step (3). And from that onwards, I get a different Anonymous ID every time I go through the login process.
Question 1. Will the Anonymous ID in step (1) ALWAYS be the same until I login to Facebook for the first time?
Question 2. What is the best login flow to allow users to save data in the backend as guests, and link to facebook later when the user decides to do so?
Here is my swift code that implements my pseudo code:
func login() -> Promise<AuthCredential?> {
// Login as Anonymous. Check if FB is logged. If it is, link to Anonymous. If fails, pass FB token to Firebase
print("........................")
print("Starting Login")
return Promise { seal in
Auth.auth().signInAnonymously() { (authResult, error) in
print("-Done Login Anonymously", authResult?.credential, Auth.auth().currentUser?.uid)
if let error = error {
print("-DID NOT SIGNED UP WITH FIREBASE:", error)
seal.reject(error)
} else {
if AccessToken.isCurrentAccessTokenActive { // If Facebook token is active exchange for Firebase
let credential = FacebookAuthProvider.credential(withAccessToken: AccessToken.current!.tokenString)
print("-Loggged with FACEBOOK!!!!", credential)
Auth.auth().currentUser!.link(with: credential) { (result, error) in
print("-Linked Account!!!!??????????", result as Any, error as Any)
if error != nil {
Auth.auth().signIn(with: credential) { (authResult, error) in
print("-Signed UP in Firebase USIG FB. No Linking")
if let error = error {
print("-DID NOT SIGNED UP WITH FIREBASE:", error)
seal.reject(error)
} else {
print("-DID SIGNED UP WITH FIREBASE using FB:", Auth.auth().currentUser?.uid)
seal.fulfill(authResult?.credential)
}
}
}
}
}
// print("-Done Login", Auth.auth().currentUser?.uid)
// seal.fulfill(authResult?.credential)
}
}
}
}
The API documentation for signInAnonymously (javascript) reads:
If there is already an anonymous user signed in, that user will be
returned; otherwise, a new anonymous user identity will be created and
returned.
You probably only want to call signInAnonymously if there is no user signed into the app. It's best to wait to see if a user is already signed in using an auth state listener, as the sign-in process is not immediate.
Once you link the anonymous account with a full account, you should probably not call signInAnonymously again, since you probably want the user to stay signed in with their full account and no create another new anon account.

Swift, FirebaseAuth: After unlinking provider user.providerData still contains providerID

After I have run the required methods to unlink the password provider, it still appears when I query it using user.providerData. The FirebaseAuth-Console registers that the password providers has been unlinked, also if I run the unlink method again I get the appropriate error: "User was not linked to an account with the given provider."
Doing the same with facebook or google login runs without problems. The error only occurs if I unlink an additional password login.
Only after I logout and login again the query is correct.
All other changes are displayed immediately/correctly. Google link/unlik, facbeook unlink/link. Even the password link method is registered instantly in user.providerData, only the unlink method is not registered immediately.
Any Ideas?
Flow:
1. Query providers IDs
1.2. result -> user.providerData = facebook.com, password
2. Execute unlink "password" provider method (runs without error)
3. Query provider IDs again
3.1. ❌ result -> user.providerData = facebook.com, password
Query method:
if let user = Auth.auth().currentUser {
for profile in user.providerData {
// Id of the provider
print(profile.providerID)
}
} else {
// No user is signed in.
}
Unlink method:
Auth.auth().currentUser?.unlink(fromProvider: "password", completion: { (user, error) in
if let error = error {
print(error)
} else {
print("successfully unlinked eMail & password login")
}
})
Google has confirmed to me that this is a current bug.
They told me they were working on the issue, and will be available on the next release, but can't provide any details or exact timeline when this would be available.

Firebase : Facebook authentification with Firebase token not working

I'm trying to get working Facebook authentification in my app.
I'm following instructions here : https://firebase.google.com/docs/auth/ios/facebook-login
When I clicked on the Facebook login button, everything is working fine : the user is logged. In the facebook login delegate method, it is ok :
func loginButton(_ loginButton: FBSDKLoginButton!, didCompleteWith result: FBSDKLoginManagerLoginResult!, error: Error!)
In this method, I have to create a Firebase token to authenticate finally with Firebase (step 5). This is what I do :
let credential = FacebookAuthProvider.credential(withAccessToken:
FBSDKAccessToken.current().tokenString)
Auth.auth().signIn(with: credential) { (user, error) in
if let error = error {
print(error.localizedDescription)
self.errorServer()
} else {
FitUtils.setFitUser(user: user)
}
}
The problem : My credential variable is not nil, everything is working, but when i'm calling Auth.auth().signIn(...), the method throwns the error variable, and user is nil.
I don't have many information about the error :
An internal error has occurred, print and inspect the error details
for more information.
And
error NSError domain: "FIRAuthErrorDomain" - code: 17999 0x0000604000640630
I suspect an error about the Facebook API key or something like that in the Firebase console, but I already checked everything is fine.
Any idea ?
EDIT
I found an explicit description of the error :
UserInfo={FIRAuthErrorUserInfoDeserializedResponseKey={
code = 400;
errors = (
{
domain = global;
message = "Unsuccessful debug_token response from Facebook: {\"error\":{\"message\":\"(#100) You must provide an app access token or a user access token that is an owner or developer of the app\",\"type\":\"OAuthException\",\"code\":100,\"fbtrace_id\":\"GkGVPPBP7vo\"}}";
reason = invalid;
}
);
message = "Unsuccessful debug_token response from Facebook: {\"error\":{\"message\":\"(#100) You must provide an app access token or a user access token that is an owner or developer of the app\",\"type\":\"OAuthException\",\"code\":100,\"fbtrace_id\":\"GkGVPPBP7vo\"}}";
}}}}
(lldb)
Finally found an answer thanks to this post :
https://github.com/firebase/FirebaseUI-iOS/issues/83#issuecomment-232523935
I have to disable the option "Is App Secret embedded in the client?" in the Facebook console :
Hope this helps.
On the Facebook Developer site, navigate to:
Settings > Advanced > Security > Require App Secret
Set to YES.
Fixed it for me.
The following worked for me:
Go to android/app/src/main/res/values/strings.xml. Make sure you have the following line in your <resources>:
<string name="facebook_app_id">FACEBOOK*APP*ID</string>
where FACEBOOK*APP*ID is the your app ID from developers.facebook.com/apps
In my case, there was set wrong App secret in Firebase 🤷🏻‍♂️
Go to the Firebase console and choose the project.
Under Project shortcuts choose Authentication.
Select Sign-in method tab.
In Sign-in providers click on Facebook to edit.
Set the correct App secret from your Facebook app.

Error adding Firebase users Swift

By following the information on this link, I am currently working on trying to create a user using Firebase, as shown in the code below. However, it appears no user is created when the app is run. Any input is greatly appreciated. The Firebase URL was taken from my test database.
let ref = Firebase(url: "https://test-96df2.firebaseio.com")
ref.createUser("bobtony#example.com", password: "correcthorsebatterystaple",
withValueCompletionBlock: { error, result in
if error != nil {
// There was an error creating the account
print("error creating account")
} else {
let uid = result["uid"] as? String
print("Successfully created user account with uid: \(uid)")
}
})
Edit:
The propagated error is:
Error Code: AUTHENTICATION_DISABLED) Projects created at
console.firebase.google.com must use the new Firebase Authentication
SDKs available from firebase.google.com/docs/auth/
However, as shown below, the email/password authentication is enabled in the Firebase console.
You have already authorized the email registration as I can see in your screenshot. So the problem doesnt realies on AUTHENTICATION_DISABLED.
From your error message and the snippet code I can see that you have created a new project but you are trying to use the legacy firebase SDK and so you will have compatibility issues. Also, you are looking into the old firebase documentation.
First of all you will need to make sure you have configured your project as documented in new firebase start guide.
After, you can take a look at the documentation for creating a new user with the new sdk. You can find the 3.x create user call bellow.
FIRAuth.auth()?.createUserWithEmail(email, password: password) { (user, error) in
// ...
}

Resources