Gyan S Awasthi
Gyan S Awasthi

Reputation: 247

Why FirebaseAuth.getInstance().getCurrentUser() is returning null value in android

I am following Code for sending images using firebase

I have configured the firebase storage url,database url ,database rule(true),sha1 placed google-services.json in app folder but when i debug the code the it(user) shows null value FirebaseUser user = firebaseAuth.getCurrentUser(); .My code is given below:

import android.content.Intent;
import android.os.Bundle;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.widget.Button;
import android.widget.Toast;

import com.google.firebase.auth.FirebaseAuth;
import com.google.firebase.auth.FirebaseUser;

import butterknife.Bind;
import butterknife.ButterKnife;

public class LoginActivity
    extends AppCompatActivity
    implements FirebaseAuth.AuthStateListener
{

    @Bind( R.id.login_button )
    Button loginButton;

    FirebaseAuth firebaseAuth = FirebaseAuth.getInstance();

    @Override
    protected void onCreate( @Nullable Bundle savedInstanceState )
    {
        super.onCreate( savedInstanceState );
        setContentView( R.layout.activity_login );
        ButterKnife.bind( this );
    }

    @Override
    public void onStart()
    {
        super.onStart();
        firebaseAuth.addAuthStateListener( this );
        verifyCurrentUserAndStartMainActivityIfLoggedIn();
    }

    @Override
    public void onStop()
    {
        super.onStop();
        firebaseAuth.removeAuthStateListener( this );
    }

    public void onLoginClicked( View view )
    {
        loginButton.setEnabled( false );
        firebaseAuth.signInAnonymously();
    }

    @Override
    public void onAuthStateChanged( @NonNull FirebaseAuth firebaseAuth )
    {
        verifyCurrentUserAndStartMainActivityIfLoggedIn();
    }

    private void verifyCurrentUserAndStartMainActivityIfLoggedIn()
    {
        FirebaseUser user = firebaseAuth.getCurrentUser();

        if ( user != null )  //HERE I AM GETTING "user=null" ??
        {

            startActivity( new Intent( this, MainActivity.class ) );
            Toast.makeText(this,"Login success",Toast.LENGTH_LONG).show();
            finish();
        }
        else
        {
            loginButton.setEnabled( true );
        }
    }

}

After successfully login when (user !=null) the next activity displays now the sample url of next activity is

//    StorageReference storageRef = storage.getReferenceFromUrl( "gs://funchat-ef3ed.appspot.com" );

and i have created new storage in firebase get the url is:

 StorageReference storageRef = storage.getReferenceFromUrl( "gs://fir-imagesending-1a8c1.appspot.com");

When I use above url application crashes. The code is given below:

public class MainActivity extends AppCompatActivity
{

    static final int REQUEST_IMAGE_CAPTURE = 1;

    FirebaseDatabase database = FirebaseDatabase.getInstance();

    FirebaseAuth firebaseAuth = FirebaseAuth.getInstance();

    DatabaseReference databaseReference = database.getReference( "messages" );

    FirebaseStorage storage = FirebaseStorage.getInstance();

//    StorageReference storageRef = storage.getReferenceFromUrl( "gs://funchat-ef3ed.appspot.com" );
    StorageReference storageRef = storage.getReferenceFromUrl( "gs://fir-imagesending-1a8c1.appspot.com");

I am unable to find where I am missing?

Upvotes: 1

Views: 8624

Answers (2)

VizGhar
VizGhar

Reputation: 3128

Your code seems to be far from oficial firebase documentation

  1. ensure you have anonymous login enabled in firebase console
  2. sign in anonymously with callback mAuth.signInAnonymously().addOnCompleteListener
  3. in onComplete() observe whether Task.isSuccessful() is true and if not what task.getException() return.
  4. and always follow official documentation not just somebody you have found on github

If you really want to know why your FirebaseUser is null, then read firebase docs

If you post the exception I can help you.

Upvotes: 2

Rahim Khalid
Rahim Khalid

Reputation: 469

You have to allow anonymous logins if you havn't and then signInAnonymously()... or you can make user login with google signin provided by firebase (for this first allow user signin with google in firebase console) and then just store all the instances as in singleton class. will be much easy to do with much security.

Upvotes: 0

Related Questions