Firebase: Auth.signInWithEmailAndPassword doesn't exist - ios

So I've been trying to setup a default login system (while I create the rest of the app), so right now i just want my app to authenticate with my credentials (only 1 user, in dev), but I can't access the method the Firebase API says to use.
When I try to call the method, this is what the auto complete says
Basically, looking for a method like this
Auth.auth().signIn(withEmail: email, password: password) { (user, error) in
// code
}

Make sure you are importing both:
import Firebase
import FirebaseAuth
For sign in, Call this function and do your task inside completion handler:
FIRAuth.auth()?.signIn(withEmail: username, password: pass, completion: { (user, error) in
})
for Sign up, do this:
FIRAuth.auth()?.createUser(withEmail: "\(username)", password: "\(pass)", completion: { (user,error) in })

install angularfire2 this will solve the problem

Related

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.

Swift3 iOS -Firebase FIREmailPasswordAuthProvider Not Working

I just upgraded my Firebase Cocoapods from 3.15.0 to 4.0.4 and FirebaseAuth Cocoapods from 3.1.1 to 4.0.0.
I'm using email & password to authenticate users. When a user changes their email I was using this method below and everything was working fine:
FIREmailPasswordAuthProviderID.credential(withEmail: emailTextField.text!, password: passwordTextField.text!)
Now that I updated I get the error message
Value of type 'String' has no member 'credential'
I looked on the docs and it said to use the method below:
The problem is I get the same exact error:
FIREmailAuthProviderID.credential(withEmail: emailTextField.text!, password: passwordTextField.text!)
Value of type 'String' has no member 'credential'
My original code below
import Firebase
import FirebaseAuth
let user: User?
let credential = FIREmailPasswordAuthProviderID.credential(withEmail: emailTextField.text!, password: passwordTextField.text!)
if let user = user{
user.reauthenticate(with: credential, completion: {
(error) in
if error != nil{
//...do something
return
}
//...do something else
})
}
What am I doing wrong?
Hold up
FIREmailAuthProviderID is a constant that contains the auth providers id and is a string (which is EmailAuthProviderID in FB 4). There is no credential function which is why you are getting that error.
Here's a link to the Firebase 4 Migration Guide
The new format (and the correct function call) is
EmailAuthProvider.credential(withEmail email: String, password:
String) -> AuthCredential
20171014 Edit: updated link to latest migration guide.
I haven't used this feature of fireBase however it appears that this link may be helpful
https://firebase.google.com/docs/reference/ios/firebaseauth/api/reference/Classes/FIREmailAuthProvider
I think you needs use EmailAuthProviderID instead of FIREmailAuthProviderID. In the Firebase 4.0 no longer uses the FIR suffix.
Hope it help you :)

Create Firebase User on ios without auto-signin [duplicate]

This question already has answers here:
Firebase kicks out current user
(19 answers)
Closed 6 years ago.
i'm writing an ios-app with firebase as backend.
In my app, there are some admins, who should be able to create new users. A new user should not be able to create an account on his own.
On web, i'm using the function
ref.createUser({
email: ctrl.user.email,
password: ctrl.user.password
}, function(error, userData) { // }
with a random password and reset the password afterwards. So the user will receive an email with a password reset link and can set a new password and log in after that. After creating the user, i'm storing all relevant userdata to the database.
On ios i'm trying to create a new user with:
FIRAuth.auth()?.createUser(withEmail: email, password: password) { (user, error) in }
But if I do it this way, I'm signed out with my admin-account and logged in with my fresh created account. Due to this, I'm not able, to store the userdata to the database, because this is restricted to admins....
Is there a way on ios to only create an user, without to auto-signin with this user?
Best wishes
Tobi
EDIT
I've written an AWS lambda function with the firebase admin sdk for node.js and create the user inside the function and trigger the function via a webservice-call from my ios-app.
Not perfect, but working....
Yes, calling createUser(withEmail:, password:) will log in a new user. To achieve what you are trying to do, I recommend a workaround by doing something like this (assume that currently an Admin is logged in):
// Create the values
var email = alreadySelectedUserEmail
var password = arc4random_uniform(1000000) // or another random number
// Send the information to the database
FIRDatabase.database().reference().child("user_data").child(email) .
setValue("lorem ipsum") // or another value
// THEN create the user, AFTER sending the data
// This will automatically log you in to the new account
FIRAuth.auth()?.createUser(withEmail: email,
password: password) { (user, error) in
// Check for any errors
if let error = error {
print error
} else {
// If there are none, LOG OUT…
try! FIRAuth.auth()!.signOut()
// …and log back into Admin (with whatever login info they have)
FIRAuth.auth()?.signIn(withEmail: adminEmail,
password: adminPassword) { (user, error) in
// Check for any errors
if let error = error {
print error
} else {
// All done, new account created!
}
}
}

Linking oAuth Providers in Firebase 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.

Resources