shinmin
shinmin

Reputation: 83

Store user's email and password after sign up

I'm making social media app that has user profile. I want to save their profile data after they have done their registration. Although the registration is successful, but the user's email and password are not saving in the Firebase database. I've also checked the rules, I use test mode.

Here's my rule:

{
  "rules": {
".read": true,
".write": true
  }
}

Here's my codes:

public class SignUpActivity extends AppCompatActivity
{
    private Button btn_signin,btn_signup;
    private EditText inputEmail, inputPassword, inputconPassword;
    private ProgressBar progressBar;
    private FirebaseAuth auth;
    private FirebaseUser firebaseuser;

    private static final String PASSWORD_PATTERN ="((?=.*\\d)(?=.*[a-z])(?=.*[A-Z]).{6,20})";
    private static  final String expression = "^[\\w\\.-]+@([\\w\\-]+\\.)+[A-Z]{2,4}$";

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_sign_up);
        auth = FirebaseAuth.getInstance();

        btn_signin = (Button) findViewById(R.id.btn_signin);
        btn_signup = (Button) findViewById(R.id.btn_signup);
        inputEmail = (EditText) findViewById(R.id.u_email);
        inputPassword = (EditText) findViewById(R.id.u_password);
        inputconPassword = (EditText) findViewById(R.id.u_conpassword);
        progressBar = (ProgressBar) findViewById(R.id.progressBar);

        btn_signin.setOnClickListener(new View.OnClickListener()
        {
            @Override
            public void onClick(View v)
            {
                startActivity(new Intent(SignUpActivity.this, LoginActivity.class));
            }
        });

        btn_signup.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {

            final String email = inputEmail.getText().toString().trim();
            final String password = inputPassword.getText().toString().trim();

            if (!validateForm())
            {
                return;
            }

            progressBar.setVisibility(View.VISIBLE);

            //create user
            auth.createUserWithEmailAndPassword(email, password)
                    .addOnCompleteListener(SignUpActivity.this, new OnCompleteListener<AuthResult>() {
                        @Override
                        public void onComplete(@NonNull Task<AuthResult> task) {
                            progressBar.setVisibility(View.GONE);
                            // If sign in fails, display a message to the user. If sign in succeeds
                            // the auth state listener will be notified and logic to handle the
                            // signed in user can be handled in the listener.
                            if (!task.isSuccessful())
                            {
                                Toast.makeText(SignUpActivity.this, "Authentication failed." + task.getException(),
                                        Toast.LENGTH_SHORT).show();
                            }
                            else
                                {
                                    Toast.makeText(SignUpActivity.this, "createUserWithEmail:onComplete:" + task.isSuccessful(), Toast.LENGTH_SHORT).show();
                                    firebaseuser = auth.getCurrentUser();
                                    User myUserInsertObj = new User(inputEmail.getText().toString().trim(),inputconPassword.getText().toString().trim());
                                    DatabaseReference ref = FirebaseDatabase.getInstance().getReference("Users");
                                    String uid = firebaseuser.getUid();

                                    ref.child(uid).setValue(myUserInsertObj).addOnCompleteListener(new OnCompleteListener<Void>() {
                                            @Override
                                            public void onComplete(@NonNull Task<Void> task)
                                            {
                                                if(task.isSuccessful())
                                                {
                                                    Toast.makeText(SignUpActivity.this, "User data stored.",Toast.LENGTH_SHORT).show();
                                                    finish();
                                                    startActivity(new Intent(getApplicationContext(), Main2Activity.class));
                                                }
                                                else
                                                {
                                                    Toast.makeText(SignUpActivity.this, "Error.",Toast.LENGTH_SHORT).show();
                                                    finish();
                                                    startActivity(new Intent(getApplicationContext(), Main3Activity.class));
                                                }
                                            }
                                        });
                            }
                        }
                    });
        }
    });
}

    private boolean validateForm()
    {
        boolean valid = true;

        String email = inputEmail.getText().toString();
        if (TextUtils.isEmpty(email))
        {
            inputEmail.setError("Required.");
            valid = false;
        }

        String password =inputPassword.getText().toString();
        String conpassword = inputconPassword.getText().toString();
        if (TextUtils.isEmpty(password))
        {
            inputPassword.setError("Required.");
            valid = false;
        }

        if (TextUtils.isEmpty(conpassword))
        {
            inputconPassword.setError("Required.");
            valid = false;
        }

        if(email.length()>0 && password.length()>0 && conpassword.length()>0)
        {
            if (isEmailValid(email))
            {
                inputEmail.setError(null);
                if (isValidPassword(password))
                {
                    inputPassword.setError(null);
                    if (isValidPassword(conpassword))
                    {
                        inputconPassword.setError(null);
                        if (password.equals(conpassword))
                        {
                            return valid;
                        }
                        else
                        {
                            Toast.makeText(getApplicationContext(), "Password not matched.Try again.", Toast.LENGTH_SHORT).show();
                            valid = false;
                        }
                    }
                    else
                    {
                        Toast.makeText(getApplicationContext(), "Password must contains minimum 6 characters at least 1 Lowercase, 1 Uppercase and, 1 Number.", Toast.LENGTH_SHORT).show();
                    valid = false;
                    }
                }
                else
                {
                    Toast.makeText(getApplicationContext(), "Password must contains minimum 6 characters at least 1 Lowercase, 1 Uppercase and, 1 Number.", Toast.LENGTH_SHORT).show();
                    valid = false;
                }
            }
            else
            {
                Toast.makeText(getApplicationContext(), "Email invalid.", Toast.LENGTH_SHORT).show();
                valid = false;
            }
        }
        return valid;
    }

    public static boolean isEmailValid(String email)
    {
       Pattern pattern = Pattern.compile(expression, Pattern.CASE_INSENSITIVE);
        Matcher matcher = pattern.matcher(email);
        return matcher.matches();
    }

    public static boolean isValidPassword(final String password)
    {
        Pattern pattern;
        Matcher matcher;
        pattern = Pattern.compile(PASSWORD_PATTERN);
        matcher = pattern.matcher(password);
        return matcher.matches();
    }

    @Override
    protected void onResume() {
        super.onResume();
        progressBar.setVisibility(View.GONE);
    }
}

Upvotes: 0

Views: 321

Answers (4)

shinmin
shinmin

Reputation: 83

here is my code for sign up button. it seems like no changes from the previous, but this one works.

        btn_signup.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            final String email = inputEmail.getText().toString().trim();
            final String password = inputPassword.getText().toString().trim();

            if (!validateForm())
            {
                return;
            }

            progressBar.setVisibility(View.VISIBLE);

            //create user
            auth.createUserWithEmailAndPassword(email, password)
                    .addOnCompleteListener(SignUpActivity.this, new OnCompleteListener<AuthResult>() {
                        @Override
                        public void onComplete(@NonNull Task<AuthResult> task) {
                            progressBar.setVisibility(View.GONE);
                            // If sign in fails, display a message to the user. If sign in succeeds
                            // the auth state listener will be notified and logic to handle the
                            // signed in user can be handled in the listener.
                            if (task.isSuccessful())
                            {
                                DatabaseReference ref = FirebaseDatabase.getInstance().getReference("Users");

                                firebaseUser = auth.getCurrentUser();
                                String uid = firebaseUser.getUid();

                                User my = new User(email,password);

                                ref.child(uid).setValue(my).addOnCompleteListener(new OnCompleteListener<Void>() {
                                    @Override
                                    public void onComplete(@NonNull Task<Void> task)
                                    {
                                        if(task.isSuccessful())
                                        {
                                            Toast.makeText(SignUpActivity.this, "Sign Up successfully.",Toast.LENGTH_SHORT).show();
                                            finish();
                                            startActivity(new Intent(getApplicationContext(), Main2Activity.class));
                                        }
                                        else
                                        {
                                            Toast.makeText(SignUpActivity.this, "Error.",Toast.LENGTH_SHORT).show();
                                            finish();
                                            startActivity(new Intent(getApplicationContext(), Main3Activity.class));
                                        }
                                    }
                                });


                            }
                            else
                                {
                                    Toast.makeText(SignUpActivity.this, "Authentication failed." + task.getException(),
                                            Toast.LENGTH_SHORT).show();
                            }
                        }
                    });
        }
    });

Upvotes: 0

bojeil
bojeil

Reputation: 30838

Why are you storing plain text password in Firebase? This is a terrible idea. Firebase Auth already hashes and salts your users' passwords. If you ever need to migrate to an external system they provide multiple tools to do so via CLI SDK and Admin SDK.

Upvotes: 0

Peter Haddad
Peter Haddad

Reputation: 80924

To store the user's email and password after sign up do this:

String email=inputEmail.getText().toString().trim();
String password=inputconPassword.getText().toString().trim();
FirebaseUser currentUser= task.getResult().getUser();
String userid=currentUser.getUid();
DatabaseReference ref = FirebaseDatabase.getInstance().getReference().child("Users").child(userid);
ref.child("email").setValue(email);
ref.child("password").setValue(password);

Then you will have:

Users
  userid
    email: email_here
    password: password_here

more info here:

https://firebase.google.com/docs/database/android/read-and-write

Upvotes: 1

A. AMAIDI
A. AMAIDI

Reputation: 6849

This answer may give you more insight about how firebase handle auth information (email+password)..

Such information is stored in a separate database so if you want to store user data then you have to do it yourself.

You can find here more details on how to store user data.

Upvotes: 0

Related Questions