Skip to content
Permalink
4b4c33655e
Switch branches/tags

Name already in use

A tag already exists with the provided branch name. Many Git commands accept both tag and branch names, so creating this branch may cause unexpected behavior. Are you sure you want to create this branch?
Go to file
 
 
Cannot retrieve contributors at this time
242 lines (178 sloc) 7.99 KB
package com.example.booksharing21;
import android.content.Intent;
import android.os.Bundle;
import android.util.Patterns;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageView;
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.database.FirebaseDatabase;
import java.security.MessageDigest;
public class RegisterActivity extends AppCompatActivity implements View.OnClickListener {
private FirebaseAuth mAuth;
public ImageView banner;
private EditText editp1,editp2,editusername,editemail , editaddress,editphone ;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_register);
mAuth = FirebaseAuth.getInstance();
// Banner/Logo click listener
banner = (ImageView) findViewById(R.id.banner);
banner.setOnClickListener(this);
// Register Listener
TextView RegisterUser = (Button) findViewById(R.id.reg1);
RegisterUser.setOnClickListener(this);
// Assigning variables to Design ID
editp1 = findViewById(R.id.password);
editp2 = findViewById(R.id.password2);
editusername = findViewById(R.id.username);
editemail = findViewById(R.id.email);
editaddress = findViewById(R.id.address);
editphone = findViewById(R.id.phone);
}
@Override
public void onClick(View v) {
switch (v.getId()){
// banner connection to login
case R.id.banner:
startActivity(new Intent(this, MainActivity.class));
break;
// registering user and moving to login
case R.id.reg1:
// checking all the validations made
if (!usernameVal() ||!passwordVal() || !password2Val() || !emailVal()|| !addressVal() || !phoneVal())
{
return;
}else {
RegisterUser();
startActivity(new Intent(this, MainActivity.class));
break;
}
}
}
private void RegisterUser() {
// managing data in inserted in the text views
String email = editemail.getText().toString().trim();
String password = editp1.getText().toString().trim();
String password2 = editp2.getText().toString().trim();
String username = editusername.getText().toString().trim();
String address = editaddress.getText().toString().trim();
String phone = editphone.getText().toString().trim();
// verifying if all the fields are filled and password and email are correct
// Authenticate data using Firebase Authentication (creating user UID)
// Creating User with email and password inserted
mAuth.createUserWithEmailAndPassword(email, password)
.addOnCompleteListener(new OnCompleteListener<AuthResult>() {
@Override
public void onComplete(@NonNull Task<AuthResult> task) {
if (task.isSuccessful()) {
// using hash function to insert encrypted password in firebase
String newPass = sha256(password);
// adding logo as default user image
String url = "https://firebasestorage.googleapis.com/v0/b/book-sharing2.appspot.com/o/users%2F2dXWTQiIMuYlur7brfk0P0ICmWr1?alt=media&token=3965016b-0c9a-4bfd-af16-70e3ee6fd530";
// inserting data into database
User user = new User(username, newPass, email, address, phone, url);
FirebaseDatabase.getInstance().getReference("Book Sharing")
.child(FirebaseAuth.getInstance().getCurrentUser().getUid())
.setValue(user).addOnCompleteListener(new OnCompleteListener<Void>() {
@Override
public void onComplete(@NonNull Task<Void> task) {
if (task.isSuccessful()) {
Toast.makeText(RegisterActivity.this, "User has been registered successfully", Toast.LENGTH_LONG).show();
} else {
Toast.makeText(RegisterActivity.this, "Failed Try Again", Toast.LENGTH_LONG).show();
}
}
});
} else {
Toast.makeText(RegisterActivity.this, "Failed Try Again", Toast.LENGTH_LONG).show();
}
}
});
}
// hashing password
// https://stackoverflow.com/questions/49298903/how-to-hash-password-and-store-in-firebase --> stack overflow code to hash password with sha-256
public static String sha256(String base) {
try {
MessageDigest digest = MessageDigest.getInstance("SHA-256");
byte[] hash = digest.digest(base.getBytes("UTF-8"));
StringBuffer hexString = new StringBuffer();
for (int i = 0; i < hash.length; i++) {
String hex = Integer.toHexString(0xfff & hash[i]);
if (hex.length() == 3) hexString.append('2');
hexString.append(hex);
}
return hexString.toString();
} catch (Exception ex) {
throw new RuntimeException(ex);
}
}
// Validation
public boolean emailVal (){
String email = editemail.getText().toString().trim();
if (email.isEmpty() || !Patterns.EMAIL_ADDRESS.matcher(email).matches()) {
editemail.setError("Email is Required");
editemail.requestFocus();
return Boolean.FALSE;
}else{
return Boolean.TRUE;
}
}
public Boolean addressVal(){
String address = editaddress.getText().toString().trim();
if (address.isEmpty()) {
editaddress.setError("Address is Required");
editaddress.requestFocus();
return Boolean.FALSE;
} else {
return Boolean.TRUE;
}
}
public Boolean phoneVal(){
String phone = editphone.getText().toString().trim();
if (phone.isEmpty()) {
editphone.setError("Phone is Required");
editphone.requestFocus();
}
return true;
}
public Boolean passwordVal(){
String password = editp1.getText().toString().trim();
if (password.isEmpty() & password.length()<6 & password.length()>16 ) {
editp1.setError("Invalid Password");
editp1.requestFocus();
return Boolean.FALSE;
} else {
return Boolean.TRUE;
}
}
public Boolean password2Val(){
String password2 = editp2.getText().toString().trim();
if (password2.isEmpty()& password2.length()<6 & password2.length()>16) {
editp2.setError("Invalid Password");
editp2.requestFocus();
return Boolean.FALSE;
}else{
return Boolean.TRUE;
}
}
public Boolean usernameVal(){
String username = editusername.getText().toString().trim();
if (username.isEmpty()) {
editusername.setError("Invalid Username");
editusername.requestFocus();
return Boolean.FALSE;
}else{
return Boolean.TRUE;
}
}
}