Следуйте приведенному ниже коду, который вы могли бы получить.
1) Зарегистрируйте активность.
Вызовите эту функцию при нажатии кнопки.
//Entry on firebase-auth
private void register_user(final String display_name, String email, String password) {
mAuth.createUserWithEmailAndPassword(email, password).addOnCompleteListener(new OnCompleteListener<AuthResult>() {
@Override
public void onComplete(@NonNull Task<AuthResult> task) {
if (task.isSuccessful()) {
FirebaseUser current_user = FirebaseAuth.getInstance().getCurrentUser();
String Uid = current_user.getUid();
mDatabase = FirebaseDatabase.getInstance().getReference().child("Users")
.child(Uid);
HashMap<String, String> userHashmap = new HashMap<>();
userHashmap.put("name", display_name);
userHashmap.put("status", "Hi there! i am using Phoenix ChatApp");
userHashmap.put("image", "default");
userHashmap.put("thumb_image", "default");
// Generating Device Token
final String curr_user = mAuth.getCurrentUser().getUid();
final String device_token = FirebaseInstanceId.getInstance().getToken();
mDatabase.setValue(userHashmap).addOnCompleteListener(new OnCompleteListener<Void>() {
@Override
public void onComplete(@NonNull Task<Void> task) {
if (task.isSuccessful()) {
mUserDatabase.child(curr_user).child("devie_token")
.setValue(device_token).addOnSuccessListener(new OnSuccessListener<Void>() {
@Override
public void onSuccess(Void aVoid) {
mRegProgress.dismiss();
Intent mainIntent = new Intent(RegisterActivity.this, MainActivity.class);
mainIntent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_CLEAR_TASK);
startActivity(mainIntent);
finish();
}
});
}
}
});
} else {
mRegProgress.hide();
Toast.makeText(RegisterActivity.this, "Cannot Sign Up,Please Check The Form And Try Again.", Toast.LENGTH_SHORT).show();
}
}
});
}
2) Активность при входе в систему
Я вызвал эту функцию в событии Click кнопки входа в систему.
private void login_user(String email, String password) {
mAuth.signInWithEmailAndPassword(email, password).addOnCompleteListener(new OnCompleteListener<AuthResult>() {
@Override
public void onComplete(@NonNull Task<AuthResult> task) {
if (task.isSuccessful()) {
String curr_user = mAuth.getCurrentUser().getUid();
String device_token = FirebaseInstanceId.getInstance().getToken();
mUserDatabase.child(curr_user).child("devie_token")
.setValue(device_token).addOnSuccessListener(new OnSuccessListener<Void>() {
@Override
public void onSuccess(Void aVoid) {
mLoginProgress.dismiss();
Intent mainIntent = new Intent(LoginActivity.this, MainActivity.class);
mainIntent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_CLEAR_TASK);
startActivity(mainIntent);
finish();
}
});
} else {
String error = "";
try {
throw task.getException();
} catch (FirebaseAuthInvalidUserException e) {
error = "Invalid Email!";
} catch (FirebaseAuthInvalidCredentialsException e) {
error = "Invalid Password!";
} catch (Exception e) {
error = "Default error!";
e.printStackTrace();
}
mLoginProgress.hide();
Toast.makeText(LoginActivity.this, error, Toast.LENGTH_LONG).show();
}
}
});
}
Надеюсь, это может вам помочь.