Parse Login iOS – Does not log in user after signing up - ios

I am currently using Parse to create and log in users for my iOS app. After a user signs up with a new account, Parse does not log them in––it just clears the text fields. Some users get confused thinking the signup was unsuccessful, and try to put their information in again, prompting the "username taken" alert. How can I modify the Parse login so that it automatically logs in the user once they create a successful account? (I currently have a ParseLoginHelper class––would I make this modification in the PFSignUpViewControllerDelegate extension?)
If anyone has example code they would like to share in their answer, please note that I'm using Swift for my project.

When you are signing up (register) a new users you don't need to call the logIn function again in order to log them in to the app.
After the signing up process they are already logged in. In order to check if they are logged in you can check if the current user is not nil
if PFUser.currentUser() != nil {
}
At the end your sign up code should look like the following:
let user = PFUser()
user.username = "USERNAME"
user.password = "PASSWORD"
user.email = "EMAIL"
// add more fields
user.signUpInBackgroundWithBlock {
(succeeded: Bool, error: NSError?) -> Void in
if succeeded {
// here the current user should not be nil.
// if it is then please check for sessionToken
if PFUser.currentUser() != nil {
// user is logged in
}
}
}

Related

Check if user already has an account just by email

How do I check if a user exists in my Firebase Database only by email within Swift?
The first screen the user should see allows him to type in his email. After that he gets to the sign up page or the login page depending on wether or not the user exists.
You're looking for fetchSignInMethodsForEmail, which returns the sign-in methods with which the given email is registered. With that information you can build a sign-in screen.
Try with this:
Auth.auth().fetchProviders(forEmail: email) { (providers, error) in
if error != nil {
print(error?.localizedDescription)
} else {
print(providers)
}
}
If the email address has not yet been used, the empty list (providers) will be printed.

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.

How to check after login user is login through Facebook in firebase for ios swift?

My app is having facebook & email password login with firebase. Now for those user who are signed with Facebook, I don't want to verify their emails. but in Auth.auth().currentUser?.isEmailVerified its return false always. so is there any other method to detect user is logged in with facebook. I know i can store value inside user default before login but after uninstall & reinstall app i will lost that userdefault. while firebase keep user logged in. i can use keychain for that but if firebase directly provide that then that will make easy for coding.
I find one solution with firebase methods:
if let providerData = Auth.auth().currentUser?.providerData {
for userInfo in providerData {
switch userInfo.providerID {
case "facebook.com":
print("Facebook Login")
//isVerifiededUser = true
default:
print("provider is \(userInfo.providerID)")
}
}
}
You can use:
if let user = Auth.auth().currentUser {
if FBSDKAccessToken.current() != nil {
// logged in using facebook
}
else {
// logged in using manual email/password method
}
}
so you can send verification emails to only those who are logged in using email/password method.
Firebase doesn't provide a method to show which was the first method used to create Firebase account. You will have a list of the all the providers attached to a firebase user with their emails/ phone number attached to them.
Firebase by default only set emails verified for Google SignIn, for other providers Firebase behavior is to set false (although some times it do set the email verified true randomly). The reason is that Firebase cannot guarantee that email is verified by facebook on their platform but incase of Google firebase has trust.
One option is you always send verification email no matter facebook or Email Auth. Second is that you ditch Email password login and instead use new Email Link Authentication, which eliminates the email auth needed in password login.
EDIT:
if you only allow one method to be used at a time then you can get the providers list from the firebase user and check if the providers list has 'password' signin method present, send a verification email after checking email verified, else don't send the email and continue the app
Here is the resource to email link authentication: https://firebase.google.com/docs/auth/ios/email-link-auth
This code means that at some point the user registered using Facebook, but it does not mean that at this point the user is using this provider for access. If the user has these providers ["google.com", "facebook.com"] it will also return true . 😊🖤
fileprivate func userHasFBProvider() -> Bool{
var fBProvider = false
guard let providerData = Auth.auth().currentUser?.providerData else {
return fBProvider
}
for userInfo in providerData {
switch userInfo.providerID {
case "facebook.com":
fBProvider = true
default:
fBProvider = false
}
}
return fBProvider
}

Do authenticated user stay signed in when i just terminate app without signing out?

I am novice in Firebase, and I an currently trying to handle (upon loading app) if user is logged in.
So according to a book i have to check it like that:
if Auth.auth().currentUser?.uid == nil {
//do smth
}
So as it turns out(if i am not missed something and not wrong) it only checks the last user in Authentication section of a console. I mean if i registered user that way:
Auth.auth().createUser(withEmail: email, password: password) { (User, Error) in
if Error != nil {
print(Error)
return
}
And just terminate my app,then during my check it will see only the last registered user. And after detecting this signed in user and if i sign out then app won't see rest signed in users in the next checking procedure.
So it seems like:
I ran app then create user several times (create then terminate app and again create then terminate app and back again)
Then after step 1 and check if user signed in. Is this last user or 1st user i don't know because i did not found and detail info about it .
if Auth.auth().currentUser?.uid == nil {
//do sign out
do {
try Auth.auth().signOut()
} catch let logoutError {
print(logoutError)
}
}
It finds signed in user then i sing out this user.
I terminate app.
I run this app again and during initial check of signed in users it does not found any of them.
override func viewDidLoad() {
super.viewDidLoad()
navigationItem.leftBarButtonItem = UIBarButtonItem(title: "Logout", style: .plain, target: self, action: #selector(handleLogout))
//user is not logged in
if Auth.auth().currentUser?.uid == nil {
signOut()
}
}
Why? I mean I did not sign out by myself the rest registered users, and I know upon calling createUser func, this createUser func also signs user in.
Maybe app signs out user if i create new? then why i did not found any info about this behavior( Please, if someone have any clue i would appreciate it.
Firebase Authentication users stay signed in until:
either your code signs them out explicitly
or you create another user in the same app on the same device
or until an event happens that requires them to sign in again (such as a password change)
A single app on a single device can only have a single signed in user. If you sign in (or create) another user on the same device, the previous user will be signed out.

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!
}
}
}

Resources