-2

I need to authenticate user with firebase (Google or Facebook). Then i want user to add some more deatils about him in NewUserActivity, and then enter the main core of the app. For checking if its first time launch i use

prefManager.isFirstTimeLaunch

This works ok with no problem But i have a bug. If user uninstall and reinstall the app, as normal its like he is launching the app first time. So i need to check if he already has an account. I use

 FirebaseUserMetadata metadata = mAuth.getCurrentUser().getMetadata();
                        if (metadata.getCreationTimestamp() == metadata.getLastSignInTimestamp())

So this its suppose to check if the user is not new one (had already an account).

My full checking code is:

 mAuthListner = new FirebaseAuth.AuthStateListener() {
                @Override
                public void onAuthStateChanged(@NonNull FirebaseAuth firebaseAuth) {
                    if (firebaseAuth.getCurrentUser() != null) {
                        FirebaseUserMetadata metadata = mAuth.getCurrentUser().getMetadata();
                        if (metadata.getCreationTimestamp() == metadata.getLastSignInTimestamp()) {
                            if (prefManager.isFirstTimeLaunch()) {
                            startActivity(new Intent(MainActivity.this, NewUserActivity.class));
                            }

                        }
                        else
                            {
                            startActivity(new Intent(MainActivity.this, UserProfile.class));
                            finishAffinity();
                    }




                    }

                }
            };

THE PROBLEM:

The code goes ALLWAYS in UserProfile Activity. So even if the user is new, doesnt go to NewUserActivity to add the details, and as the UserProfile Activity havent the details needed, throws null exception.

As i can understand the problem is that this code

if (metadata.getCreationTimestamp() == metadata.getLastSignInTimestamp())

is not working. Without this line app works fine EXCEPT if user uninstall and reinstall the app. In this case its like a new user.

Generally speaking is a very normal scenario of authenticate user in firebase, add some details in an activity for first time, and then always be in profile.

I had this part of code for long time with no problem. The strange behavior started a week before with no actually reason. So i m almost convinced its firebase problem.

Clarification

No need for logcat as the null exception has to do that code ALWAYS go to UserProfile so hasnt got the details needed.

  • If you app crashes, there should be an error message and a stack trace in your logcat output. See https://stackoverflow.com/questions/23353173/unfortunately-myapp-has-stopped-how-can-i-solve-this – Frank van Puffelen Feb 09 '19 at 23:24
  • What's the error? Please add it to your question. – Alex Mamo Feb 10 '19 at 09:32
  • Where is your logcat? – 0xA Feb 10 '19 at 10:01
  • there is no need for logcat. Logat throws null exception because even if its a new user goes to profile activity so the user didnt create ingame account so its null. Logcat just says as normal that user is null BECAUSE in all situations the user looks existing because of firebase metadata – Healthydev Developments Feb 10 '19 at 10:09
  • @FrankvanPuffelen Check my edit please. You are right for logcat but the null exception there is normal. The wrong is that if (metadata.getCreationTimestamp() == metadata.getLastSignInTimestamp()) dont work (i guess) and dont lead to the right activity. – Healthydev Developments Feb 13 '19 at 17:51
  • There was a similar report of this on iOS recently, and it turns out that that comparison doesn't always work anymore. See my answer here: https://stackoverflow.com/questions/54562798/firebase-auth-lastsignindate-and-creationdate-comparison-failure/54563395#54563395 – Frank van Puffelen Feb 13 '19 at 20:53
  • @FrankvanPuffelen Thank you for the answer. At least i know its firebase problem and not mine. I saw your answer in the other post but i dint get it how to make this check. Is possible to give an example? I use google, facebook and mail login as the IOS developer, so whats the option to get if someone is old user or a new one? – Healthydev Developments Feb 14 '19 at 13:52
  • It's the same for all of them: capture the [`AdditionalUserInfo`](https://firebase.google.com/docs/reference/android/com/google/firebase/auth/AdditionalUserInfo.html) when the user signs in, and check the [`isNewUser`](https://firebase.google.com/docs/reference/android/com/google/firebase/auth/AdditionalUserInfo.html#isNewUser()) method on there. – Frank van Puffelen Feb 14 '19 at 14:42
  • @FrankvanPuffelen I checked all the answers about AdditionalUserInfo and i didnt understand how to check for all mai/password, facebook, google login. As i understand i have to check for every AuthResult separated? Till now was just one line of code to make the check and worked excellent for me. Now i m totally lost how to do it. And i saw some answers of you that this way is not if use facebook/login/mail all together – Healthydev Developments Feb 14 '19 at 21:42
  • If you're having problems making ` AdditionalUserInfo.isNewUser` work, update your question to show: 1) how you get that value, 2) what check you do, 3) what result you got. – Frank van Puffelen Feb 14 '19 at 23:31
  • @FrankvanPuffelen according everything i checked and tried and https://github.com/firebase/firebase-ios-sdk/issues/2346 and other posts this is not working. Too pitty to touch something that worked fine and have to replace it with unessesary and not working code. Anyway thank you for your effort. – Healthydev Developments Feb 17 '19 at 17:13

1 Answers1

0

As i found out, its a firebase bug that metadata not working because of differences (some seconds or demical). Still testing all situations but the following code looks it works as it supposed without have to replace it with other buggy code:

mAuthListner = new FirebaseAuth.AuthStateListener() {
            @Override
            public void onAuthStateChanged(@NonNull FirebaseAuth firebaseAuth) {
                if (firebaseAuth.getCurrentUser() != null) {
                    FirebaseUserMetadata metadata = mAuth.getCurrentUser().getMetadata();
                    **if (Math.abs(metadata.getCreationTimestamp() - metadata.getLastSignInTimestamp()) <5000L){**
                        if (prefManager.isFirstTimeLaunch()) {
                            startActivity(new Intent(MainActivity.this, NewUserActivity.class));
                        }

                        else{

                            startActivity(new Intent(MainActivity.this, UserProfile.class));
                            finishAffinity();



                    }

                }
                else{
                        startActivity(new Intent(MainActivity.this, UserProfile.class));
                        finishAffinity();
                    }




                }

            }
        };

Hope it helps!!!