简体   繁体   中英

Login activity in Firebase authentication is not launching from a new activity

I am using a new activity to launch the Firebase authentication Activity and post the authenticated user in my NodeJS API but the Firebase authentication activity is not launching even when i have not authenticated.

Here is the code:



    public class loginActivity extends AppCompatActivity {
        FirebaseAuth auth;
        private static final int RC_SIGN_IN=1;
        double latitude,longitude;
        boolean isAuthDone=false;
        @Override
        protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.activity_login);
            auth=FirebaseAuth.getInstance();
            if(auth.getCurrentUser() != null){
                //user already logged in
                Log.d("TAG     ", auth.getCurrentUser().getEmail()+" already logged in.");
                onBackPressed();
            }
            else {

                    loginFunction();

            }

            String name=auth.getCurrentUser().getDisplayName(); //LINE 54
            String email=auth.getCurrentUser().getEmail();

            // POST TO API
            GPSTracker tracker = new GPSTracker(this);
            if (!tracker.canGetLocation()) {
                tracker.showSettingsAlert();
            } else {
                latitude = tracker.getLatitude();
                longitude = tracker.getLongitude(); //LOCATION
            }


            String loc = latitude+", "+longitude;
            Users newUser=new Users(name, email, loc, "0", false);

    //        if(!isAuthDone){
    //            while(!isAuthDone){
    //                loginFunction();    //ASK FOR LOGIN UNTIL THE USER AUTHENTICATES.
    //            }
    //        }

            Retrofit retrofit = new Retrofit.Builder()
                    .baseUrl(ApiUrls.BASEURL)
                    .addConverterFactory(GsonConverterFactory.create(new GsonBuilder().serializeNulls().create()))
                    .build();
            ApiInterface apiI;
            apiI= retrofit.create(ApiInterface.class);
            Call call;
            call=apiI.saveUser(newUser);
            call.enqueue(new Callback() {
                @Override
                public void onResponse(Call call, Response response) {
                    if(response.isSuccessful()){
                        Log.e("User reg. in API",response.message()+response.code());
                        Users user=response.body();
                        ApiUrls.userName=auth.getCurrentUser().getDisplayName();
                        ApiUrls.userID=user.getID()+"";     //THIS IS VERY IMPORTANT.
                        ApiUrls.userEmail=user.getEmail();
                        ApiUrls.profilePicURL=auth.getCurrentUser().getPhotoUrl();
                        ApiUrls.isUserLoggedInAndSaved=true;
                    }
                }
                @Override
                public void onFailure(Call call, Throwable t) {
                    Log.e("TAG", "Not posted in API");
                    AuthUI.getInstance()
                            .signOut(loginActivity.this)
                            .addOnCompleteListener(new OnCompleteListener() {
                                @Override
                                public void onComplete(Task task) {
                                    Log.d("TAG    ", "Signed out from this app.");
                                    onBackPressed();
                                }
                            });
                    ApiUrls.isUserLoggedInAndSaved=false;
                    //Sign out from the email and launch a DIALOG to try again.
                }
            });
            onBackPressed();

        }

        private void loginFunction() {
                startActivityForResult(AuthUI.getInstance()
                        .createSignInIntentBuilder()
                        .setProviders(AuthUI.FACEBOOK_PROVIDER,
                                AuthUI.EMAIL_PROVIDER,
                                AuthUI.GOOGLE_PROVIDER).build(),RC_SIGN_IN);

        }

        @Override
        protected void onActivityResult(int requestCode, int resultCode, Intent data) {
            super.onActivityResult(requestCode, resultCode, data);
            if(requestCode == RC_SIGN_IN){
                if(resultCode == RESULT_OK){
                    Log.d("TAG logged in", auth.getCurrentUser().getEmail());
                    isAuthDone=true;
                }
                else{
                    Log.d("TAG    ", "USER NOT AUTHENTICATED");
                    //Launch a dialog to turn the internet ON and redirect to some other activity.
                }
            }
        }
    }

Earlier i was launching the Firebase authentication Activity from many places. There it worked but now it is not working. Error is:

Caused by: java.lang.NullPointerException: Attempt to invoke virtual method 'java.lang.String com.google.firebase.auth.FirebaseUser.getDisplayName()' on a null object reference at com.example.aavgeensingh.shuddhi.Activities.loginActivity.onCreate(loginActivity.java:54)

Edit: I know the cause now. But is there any way i can wait for the user to authenticate first and then post the details to my API using retrofit.

After you call loginFunction() the rest of the onCreate() function continues to execute so the user can be null. You should end execution before this point.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM