1

Firstly, I tried alone to make a simple registration code to my application and it worked:

public class MainActivity extends AppCompatActivity {
private static final String FIREBASE_URL="https://***.firebaseio.com/";
private Firebase firebaseRef;
@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    Firebase.setAndroidContext(this);
    firebaseRef = new Firebase(FIREBASE_URL);





    findViewById(R.id.button1).setOnClickListener(new View.OnClickListener() {


        @Override
        public void onClick(View view) {
            EditText fullname=(EditText)findViewById(R.id.full_name);
            EditText email=(EditText)findViewById(R.id.mail);
            EditText restname=(EditText)findViewById(R.id.rest_name);
            EditText phonenum=(EditText)findViewById(R.id.phone_num);
            EditText userpass=(EditText)findViewById(R.id.user_pass);
            EditText personalcode=(EditText)findViewById(R.id.personal_code);
            String full_name=fullname.getText().toString();
            String mail=email.getText().toString();
            String rest_name=restname.getText().toString();
            String phone_num=phonenum.getText().toString();
            String user_pass=userpass.getText().toString();
            String personal_code=personalcode.getText().toString();

           if(full_name!=""&&mail!=""&&rest_name!=""&&phone_num!=""&&user_pass!=""&&personal_code!="")
            {
               UserClass user= new UserClass(full_name,mail,rest_name,phone_num,user_pass,personal_code);
                firebaseRef.push().setValue(user);
                fullname.setText("");
                email.setText("");
                clubname.setText("");
                phonenum.setText("");
                userpass.setText("");
                personalcode.setText("");
                Toast.makeText(MainActivity.this,"Account Inserted",Toast.LENGTH_LONG).show();
            }

        }
    });

}

So as I said, it worked well and the information was inserted to the Firebase database well.

But I found a problem with the login part. I could'nt find any guide that shows how to make a login from the information I've inserted to the Firebase database with the registration code I wrote above.

What are you recommending me to do? Did the way I write the registration code is recommended? If yes - How should I code the login page? If no - what is the best way to make registeration or login for an android application with Firebase?

Thanks alot!

Frank van Puffelen
  • 565,676
  • 79
  • 828
  • 807
Gilad Neiger
  • 319
  • 1
  • 3
  • 14
  • off-topic note on the code: `full_name`, `rest_name`, etc. are String objects, using != or == operators on them to see if they're empty won't work as you expect. Read for example this: http://stackoverflow.com/questions/513832/how-do-i-compare-strings-in-java – Gimby Mar 14 '16 at 15:44
  • @Gimby Ok, thanks. Before I started to learn Java, I learned C# and in C# it works well. Thanks for correcting me. – Gilad Neiger Mar 14 '16 at 15:50
  • There is a guide on storing user data, which seems to answer your question here: https://www.firebase.com/docs/android/guide/user-auth.html#section-storing – Kato Mar 16 '16 at 15:33

2 Answers2

1

There is a difference between log and store user data. You can save user data just after using the method of creating user Firebase.

ref.createUser(email, pass, new Firebase.ValueResultHandler<Map<String, Object>>() {
     @Override
     public void onSuccess(Map<String, Object> result) {
         System.out.println("Successfully created user account with uid: " + result.get("uid"));


        // read data ...

        // save user data
        Map<String, String> map = new HashMap<String, String>();
        map.put("provider", authData.getProvider());
        map.put("genre", genre);
        map.put("birthDate", birthDate);
        map.put("name", name);

        ref.child("users").child(result.get("uid")).setValue(map);
     }

     @Override
     public void onError(FirebaseError firebaseError) {
         // there was an error
     }
});

Then you can log in using the following function Firebase.

ref.authWithPassword(email, pass, authResultHandler);
  • I tried what you said, but I got an error with the line: ref.child("users").child(result.get("uid")).setValue(map); the result.get requires and Object and not String, so what should I put there? – Gilad Neiger Mar 14 '16 at 18:26
  • By the way I changed the authData.getProvider() to: firebaseRef.getAuth().getProvider() because it showed an error – Gilad Neiger Mar 14 '16 at 18:41
  • Remove the line: map.put("provider", authData.getProvider()); It is only necessary if you want to save it. – Daniel Alvarez Mar 14 '16 at 19:04
  • Ok, and what about the line: ref.child("users").child(result.get("uid")).setValue(map)? the result.get requires and Object and not String, so what should I put there? – Gilad Neiger Mar 14 '16 at 19:08
  • Please cast result.get("uid") to String: String uidString = result.get("uid").toString(); and use uidString – Daniel Alvarez Mar 14 '16 at 19:20
  • It worked! but I didn't understand what you wrote about the login function. How do I use it? – Gilad Neiger Mar 14 '16 at 19:40
  • After creating the user you can use this method to authenticate the user. Look at the following example. [Code](https://gist.github.com/alvareztech/c47f49cba34ffbf31cca) – Daniel Alvarez Mar 14 '16 at 20:42
  • Thanks it helped me. but now I'm not sure that the information i wanted to input into the database got in. I mean, the user was created, but the other information like "genre" or "birthDate" was not. this is my code: http://pastebin.com/tYpHDeWB thanks for helping! – Gilad Neiger Mar 14 '16 at 20:54
  • Thanks alot! With your answer I've resolved my errors, thanks you. – Gilad Neiger Mar 15 '16 at 15:34
0

I have a best way for u bellow:

  1. step one: You still insert user information as above code.
  2. at the same time (after insert user infor) you have to use firebaseRef.createUser(..) to pass username and password to register your user account to firebase account managerment

Ex:

firebaseRef.createUser("username or email here", "user password here", new Firebase.ValueResultHandler<Map<String, Object>>() {
    @Override
    public void onSuccess(Map<String, Object> result) {
        System.out.println("Successfully created user account with uid: " + result.get("uid"));
       //You can push(...) your user informaton here (which same username or email as primerykey)
    }
    @Override
    public void onError(FirebaseError firebaseError) {
        // there was an error
    }
});

and then you can help user login with code bellow:

firebaseRef.authWithPassword("username or email registered", "registerd password", new Firebase.AuthResultHandler() {
    @Override
    public void onAuthenticated(AuthData authData) {
        System.out.println("User ID: " + authData.getUid() + ", Provider: " + authData.getProvider());
    }
    @Override
    public void onAuthenticationError(FirebaseError firebaseError) {
        // there was an error
    }
});
GiapLee
  • 436
  • 2
  • 8