Skip to content

Instantly share code, notes, and snippets.

@buildbro
Created February 1, 2019 00:37
Show Gist options
  • Save buildbro/5d7919ac81030fba0f777cd49c22c1cf to your computer and use it in GitHub Desktop.
Save buildbro/5d7919ac81030fba0f777cd49c22c1cf to your computer and use it in GitHub Desktop.
Firebase email/password registration
package com.example.firebaseemailpasswordexample;
import android.content.Intent;
import android.support.annotation.NonNull;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ProgressBar;
import android.widget.Toast;
import com.google.android.gms.tasks.OnCompleteListener;
import com.google.android.gms.tasks.Task;
import com.google.firebase.auth.AuthResult;
import com.google.firebase.auth.FirebaseAuth;
public class RegistrationActivity extends AppCompatActivity {
private EditText emailTV, passwordTV;
private Button regBtn;
private ProgressBar progressBar;
private FirebaseAuth mAuth;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_registration);
mAuth = FirebaseAuth.getInstance();
initializeUI();
regBtn.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
registerNewUser();
}
});
}
private void registerNewUser() {
progressBar.setVisibility(View.VISIBLE);
String email, password;
email = emailTV.getText().toString();
password = passwordTV.getText().toString();
if (TextUtils.isEmpty(email)) {
Toast.makeText(getApplicationContext(), "Please enter email...", Toast.LENGTH_LONG).show();
return;
}
if (TextUtils.isEmpty(password)) {
Toast.makeText(getApplicationContext(), "Please enter password!", Toast.LENGTH_LONG).show();
return;
}
mAuth.createUserWithEmailAndPassword(email, password)
.addOnCompleteListener(new OnCompleteListener<AuthResult>() {
@Override
public void onComplete(@NonNull Task<AuthResult> task) {
if (task.isSuccessful()) {
Toast.makeText(getApplicationContext(), "Registration successful!", Toast.LENGTH_LONG).show();
progressBar.setVisibility(View.GONE);
Intent intent = new Intent(RegistrationActivity.this, LoginActivity.class);
startActivity(intent);
}
else {
Toast.makeText(getApplicationContext(), "Registration failed! Please try again later", Toast.LENGTH_LONG).show();
progressBar.setVisibility(View.GONE);
}
}
});
}
private void initializeUI() {
emailTV = findViewById(R.id.email);
passwordTV = findViewById(R.id.password);
regBtn = findViewById(R.id.register);
progressBar = findViewById(R.id.progressBar);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment