4

I have been trying for a few days to do Simple User Authentication with Java (not Android) and Firebase. Before I started out with Firebase's user authentication classes I tried using Java to do a simple save and retrieve on my Firebase Reference with no success. No errors are reported but nothing happens in Firebase (no changes to the data). In the past, I have been able to integrate Firebase with Android SDK with no issues. I am making use of Firebase JAR for JVM but nothing is happening.

import com.firebase.client.AuthData;
import com.firebase.client.DataSnapshot;
import com.firebase.client.Firebase;
import com.firebase.client.FirebaseError;
import com.firebase.client.ValueEventListener;
import java.math.BigInteger;
import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;
import java.util.HashMap;

/**
 *
 * @author Chidi
 */
public class Auth {
    private final Firebase usersRef;
    private boolean isNewUser = false;
    private String uid = null;
    private HashMap<String, Object> userHash;

    public Auth() {
        Firebase rootRef = new Firebase("https://tranzchat.firebaseio.com/");
        this.usersRef = rootRef.child("users");  
        userHash = new HashMap<>();
    }

    public void createUser(final String email, final String password, String name, String default_lang) {
        userHash.put("email", email);
        userHash.put("password", this.hashPassword(password));
        userHash.put("name", name);
        userHash.put("default_lang", default_lang);
        Vars.dbcon.createUser(email, password, new Firebase.ResultHandler() {
            @Override
            public void onSuccess() {
                System.out.println("Created the new user");
                // Sets isNewUser to true after creating the new user
                isNewUser = true;
                signIn(email, password);
                if(uid != null) {
                    // Try to store use information
                    Firebase newUserRef = usersRef.child(uid);
                    newUserRef.setValue(userHash, new Firebase.CompletionListener(){
                        @Override
                        public void onComplete(FirebaseError fe, Firebase frbs) {
                            System.out.println("Completed Database Insertion");
                        }

                    });
                } else {
                    System.out.println("Could not retrieve UID");
                }
            }

            @Override
            public void onError(FirebaseError fe) {
                System.out.println("An error occured while creating the user.");
            }
        });
    }

    public void signIn(String email, String password) {
        Vars.dbcon.authWithPassword(email, password, new Firebase.AuthResultHandler() {
            @Override
            public void onAuthenticated(AuthData authData) {
                uid = authData.getUid();
                if(!isNewUser) {
                    // Try to get new user information and store it in the user object
                    usersRef.child(uid).addValueEventListener(new ValueEventListener() {
                        @Override
                        public void onDataChange(DataSnapshot snapshot) {
                            userHash = (HashMap<String, Object>) snapshot.getValue();
                        }
                        @Override
                        public void onCancelled(FirebaseError firebaseError) {
                            System.out.println("The read failed: " + firebaseError.getMessage());
                        }
                    });
                }
            }
            @Override
            public void onAuthenticationError(FirebaseError firebaseError) {
                // there was an error
            }
        });
    }
    public String hashPassword(String password) {
        String md5 = null;
        if(password == null) return null;
        try {
            // Create MessageDigest object for MD5
            MessageDigest digest = MessageDigest.getInstance("MD5");
            // Update input string in message digest
            digest.update(password.getBytes(), 0, password.length());
            // Converts message digest value in base 16 (hex)
            md5 = new BigInteger(1, digest.digest()).toString();
        } catch(NoSuchAlgorithmException e) {
            e.printStackTrace();
        }
        return md5;
    }

    // Test Suite
    public static void main(String[] args) throws InterruptedException {
        Vars.init();
        Thread firebaseAuth = new Thread(new Runnable() {
            @Override
            public void run() {
                Auth au = new Auth();
                au.createUser("chidiebere.nnadi@gmail.com", "password", "Chidiebere Nnadi", "en");
            }
        });
        firebaseAuth.start();
        firebaseAuth.join();
    }
}
chidieberennadi
  • 143
  • 2
  • 9
  • 2
    You should handle errors in your `onComplete` callback. Firebase may be reporting an error there, but you're simply ignoring it. See the example here: https://www.firebase.com/docs/android/guide/saving-data.html#section-completion-callback – Frank van Puffelen Oct 31 '14 at 11:24
  • @FrankvanPuffelen That was not the problem. I put adequate error checking everywhere and errors should actually pop up but I see no errors. I tried checking for errors onComplete but nothing still shows up. – chidieberennadi Nov 01 '14 at 22:21

1 Answers1

4

I just found out the issue with it. The thread was ending before Firebase could call the onComplete function. You can just call a Thread.sleep(x_ms) within a loop after the last line of code in the application.

chidieberennadi
  • 143
  • 2
  • 9