Created
June 28, 2019 06:33
-
-
Save SteveOye/a8cf21f7af843a6eb59afe65cf077b09 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package com.druve.druve; | |
import android.annotation.SuppressLint; | |
import android.app.AlertDialog; | |
import android.content.DialogInterface; | |
import android.content.Intent; | |
import android.os.Bundle; | |
import android.util.Log; | |
import android.view.LayoutInflater; | |
import android.view.View; | |
import android.widget.Button; | |
import android.widget.EditText; | |
import android.widget.TextView; | |
import android.widget.Toast; | |
import androidx.annotation.NonNull; | |
import androidx.appcompat.app.AppCompatActivity; | |
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; | |
import com.google.firebase.auth.FirebaseUser; | |
import com.google.firebase.database.DataSnapshot; | |
import com.google.firebase.database.DatabaseError; | |
import com.google.firebase.database.DatabaseReference; | |
import com.google.firebase.database.FirebaseDatabase; | |
import com.google.firebase.database.ValueEventListener; | |
import java.util.Objects; | |
public class SignUpActivity extends AppCompatActivity implements View.OnClickListener { | |
private static final String TAG = "SignUpActivity"; | |
Button signUpBtn; | |
EditText fullName, emailAddress, passwordOne, confirmPassword; | |
AlertDialog.Builder builder; | |
AlertDialog alertDialog; | |
private FirebaseDatabase mFirebaseDatabase; | |
private FirebaseAuth mAuth; | |
private FirebaseAuth.AuthStateListener mAuthStateListener; | |
private DatabaseReference mDatabaseReference; | |
private String userID; | |
@Override | |
protected void onCreate(Bundle savedInstanceState) { | |
super.onCreate(savedInstanceState); | |
setContentView(R.layout.activity_sign_up); | |
Objects.requireNonNull(getSupportActionBar()).hide(); | |
initViews(); | |
mAuth = FirebaseAuth.getInstance(); | |
mFirebaseDatabase = FirebaseDatabase.getInstance(); | |
mDatabaseReference = mFirebaseDatabase.getReference("users"); | |
FirebaseUser user = mAuth.getCurrentUser(); | |
dataBaseCalls(); | |
} | |
public void dataBaseCalls() { | |
mAuthStateListener = new FirebaseAuth.AuthStateListener() { | |
@Override | |
public void onAuthStateChanged(@NonNull FirebaseAuth firebaseAuth) { | |
FirebaseUser user = firebaseAuth.getCurrentUser(); | |
if (user != null) { | |
Log.d(TAG, "onAuthStateChanged: signed_in"); | |
Toast.makeText(SignUpActivity.this, | |
"Successfully sign in with" + user.getUid(), Toast.LENGTH_SHORT).show(); | |
} else { | |
Log.d(TAG, "onAuthStateChanged: Signed_out"); | |
Toast.makeText(SignUpActivity.this, "Signed out", Toast.LENGTH_SHORT).show(); | |
} | |
} | |
}; | |
mDatabaseReference.addValueEventListener(new ValueEventListener() { | |
@Override | |
public void onDataChange(@NonNull DataSnapshot dataSnapshot) { | |
Toast.makeText(SignUpActivity.this, "Added data to database", Toast.LENGTH_SHORT).show(); | |
} | |
@Override | |
public void onCancelled(@NonNull DatabaseError databaseError) { | |
} | |
}); | |
} | |
//init views | |
public void initViews() { | |
//EditText Field | |
fullName = findViewById(R.id.full_name_text); | |
emailAddress = findViewById(R.id.email_text); | |
passwordOne = findViewById(R.id.password_edit); | |
confirmPassword = findViewById(R.id.confirm_password_text); | |
//Buttons | |
signUpBtn = findViewById(R.id.signup_button); | |
signUpBtn.setOnClickListener(this); | |
} | |
public void validateFields() { | |
String name = fullName.getText().toString().trim(); | |
String eAddress = emailAddress.getText().toString().trim(); | |
String passOne = passwordOne.getText().toString().trim(); | |
String passTwo = confirmPassword.getText().toString().trim(); | |
if (name.isEmpty()) { | |
Toast.makeText(this, "Enter Full name.", Toast.LENGTH_SHORT).show(); | |
} else if (eAddress.isEmpty()) { | |
Toast.makeText(this, "Enter a valid Email address", Toast.LENGTH_SHORT).show(); | |
} else if (passOne.isEmpty()) { | |
Toast.makeText(this, "Enter Password.", Toast.LENGTH_SHORT).show(); | |
} else if (!passOne.equals(passTwo)) { | |
Toast.makeText(this, "Passwords do not match", Toast.LENGTH_SHORT).show(); | |
} else { | |
createUser(eAddress, passOne, name); | |
} | |
} | |
private void createUser(final String eAddress, String passwordCode, final String name) { | |
showDialog(); | |
mAuth.createUserWithEmailAndPassword(eAddress, passwordCode) | |
.addOnCompleteListener(this, new OnCompleteListener<AuthResult>() { | |
@Override | |
public void onComplete(@NonNull Task<AuthResult> task) { | |
if (task.isSuccessful()) { | |
// Sign in success, update UI with the signed-in user's information | |
Log.d(TAG, "createUserWithEmail:success"); | |
emailVerification(); | |
userID = mDatabaseReference.push().getKey(); | |
User user = new User(name, eAddress); | |
mDatabaseReference.child("users").child(userID).setValue(user); | |
Toast.makeText(SignUpActivity.this, | |
user.getEmail(), Toast.LENGTH_SHORT).show(); | |
alertDialog.hide(); | |
} else { | |
// If sign in fails, display a message to the user. | |
Log.w(TAG, "createUserWithEmail:failure", task.getException()); | |
Toast.makeText(SignUpActivity.this, "Authentication failed.", | |
Toast.LENGTH_SHORT).show(); | |
alertDialog.hide(); | |
} | |
} | |
}); | |
} | |
public void emailVerification() { | |
final FirebaseUser user = mAuth.getCurrentUser(); | |
if (user != null) { | |
user.sendEmailVerification() | |
.addOnCompleteListener(this, new OnCompleteListener<Void>() { | |
@Override | |
public void onComplete(@NonNull Task<Void> task) { | |
if (task.isSuccessful()) { | |
// String mail = user.getEmail(); | |
mailSent(); | |
} else { | |
Log.e(TAG, "sendEmailVerification", task.getException()); | |
Toast.makeText(SignUpActivity.this, | |
"Failed to send verification email.", | |
Toast.LENGTH_SHORT).show(); | |
} | |
} | |
}); | |
} | |
} | |
@Override | |
public void onClick(View v) { | |
int id = v.getId(); | |
if (id == R.id.signup_button) { | |
validateFields(); | |
} | |
} | |
public void showDialog() { | |
//Now we need an AlertDialog.Builder object | |
builder = new AlertDialog.Builder(this); | |
LayoutInflater layoutInflater = getLayoutInflater(); | |
@SuppressLint("InflateParams") | |
View dialogView = layoutInflater.inflate(R.layout.alert_dialog, null); | |
//setting the view of the builder to our custom view that we already inflated | |
builder.setView(dialogView); | |
TextView textView = dialogView.findViewById(R.id.progress_text); | |
textView.setText(getResources().getString(R.string.signin_dialog)); | |
//finally creating the alert dialog and displaying it | |
alertDialog = builder.create(); | |
alertDialog.show(); | |
} | |
public void mailSent() { | |
AlertDialog.Builder builder = new AlertDialog.Builder(this); | |
builder.setMessage | |
("Verify your Email address in the mail sent to you."); | |
builder.setPositiveButton("OK", new DialogInterface.OnClickListener() { | |
@Override | |
public void onClick(DialogInterface dialog, int which) { | |
finish(); | |
startActivity(new Intent(SignUpActivity.this, SignInActivity.class)); | |
} | |
}); | |
AlertDialog alertDialog = builder.create(); | |
alertDialog.show(); | |
} | |
@Override | |
protected void onStart() { | |
super.onStart(); | |
mAuth.addAuthStateListener(mAuthStateListener); | |
} | |
@Override | |
protected void onStop() { | |
super.onStop(); | |
if (mAuthStateListener != null) { | |
mAuth.removeAuthStateListener(mAuthStateListener); | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment