Firebase login and signup with username

后端 未结 2 743
礼貌的吻别
礼貌的吻别 2021-01-07 10:59

I am currently using AndroidHive\'s tutorial to learn how to use Firebase, I am starting to understand the methods and documentation well now - I realised that Firebase offe

相关标签:
2条回答
  • 2021-01-07 11:32

    Undo the addition of .getDatabase(); first. This is not a native Firebase method, my mistake.

    I got your problem.

    Make FirebaseDatabase a global variable and initialise it in onCreate. It requires a context at the point.

    Just take FirebaseDatabase database; before onCreate.

    And In, onCreate: initialise, database=FirebaseDatabase.getInstance();.

    Don't initialise in method. I'll edit my previous answer soon.

    0 讨论(0)
  • 2021-01-07 11:56

    Try this:

    auth.createUserWithEmailAndPassword(email, password)
    .addOnCompleteListener(SignupActivity.this, new OnCompleteListener<AuthResult>() {
    @Override
    public void onComplete(@NonNull Task<AuthResult> task) {
        Toast.makeText(SignupActivity.this, "createUserWithEmail:onComplete:" + task.isSuccessful(), Toast.LENGTH_SHORT).show();
        progressBar.setVisibility(View.GONE);
        generateUser(email, password)
    
        if (!task.isSuccessful()) {
            Toast.makeText(SignupActivity.this, "Authentication failed." + task.getException(),
                Toast.LENGTH_SHORT).show();
        } else {
            startActivity(new Intent(SignupActivity.this, MainActivity.class));
            finish();
        }
    }
    });
    

    This is the method that is being called above.

    public void generateUser(String username, String password)
    {
    
    FirebaseDatabase database = FirebaseDatabase.getInstance();
    DatabaseReference users = database.getReference("users"); //users is a node in your Firebase Database.
    User user = new User(username, password); //ObjectClass for Users
    users.push().setValue(user);
    
    }
    

    Also: User.class

       public class User {
    
        String username;
        String password;
    
        public User() {
        //Empty Constructor For Firebase
        }
    
    
    public User(String username, String password)
            {
            this.username = username; //Parameterized for Program-Inhouse objects.
            this.password = password;
            }
    
        //Getters and Setters
        public String getUsername()
        {
        return username;
        }
        public void setUsername(String username)
        {
        this.username = username;
        }
        public String getPassword()
        {
        return password;
        }
        public void setPassword(String password)
        {
        this.password = password;
        }
    }
    
    0 讨论(0)
提交回复
热议问题