-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathSignup.java
117 lines (108 loc) · 4.95 KB
/
Signup.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
package change.com.animationwithsplash;
import androidx.annotation.NonNull;
import androidx.appcompat.app.AppCompatActivity;
import android.app.ProgressDialog;
import android.content.Intent;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;
import com.google.android.gms.tasks.OnCompleteListener;
import com.google.android.gms.tasks.OnFailureListener;
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.DatabaseReference;
import com.google.firebase.database.FirebaseDatabase;
import java.util.HashMap;
public class Signup extends AppCompatActivity {
TextView login ;
EditText Username,Email,Password;
Button btn_register;
FirebaseAuth auth;
DatabaseReference reference;
ProgressDialog pd;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_signup);
login = findViewById(R.id.logintext);
Username = findViewById(R.id.username);
Email = findViewById(R.id.email);
Password = findViewById(R.id.password);
btn_register = findViewById(R.id.register);
auth = FirebaseAuth.getInstance();
pd = new ProgressDialog(this);
btn_register.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
String txt_username = Username.getText().toString();
String txt_email = Email.getText().toString();
String txt_password = Password.getText().toString();
if(TextUtils.isEmpty(txt_username)||TextUtils.isEmpty(txt_email)||TextUtils.isEmpty(txt_password))
{
Toast.makeText(Signup.this,"Fields cannot be empty",Toast.LENGTH_SHORT).show();
}
else if(txt_password.length()< 6)
{
Toast.makeText(Signup.this,"Too short password",Toast.LENGTH_SHORT).show();
}
else
{
register(txt_username,txt_email,txt_password);
}
}
});
login.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Intent intent = new Intent(Signup.this,Login.class);
startActivity(intent);
}
});
}
private void register(final String username, String email, String password){
pd.setMessage("Please Wait");
pd.show();
auth.createUserWithEmailAndPassword(email,password)
.addOnCompleteListener(new OnCompleteListener<AuthResult>() {
@Override
public void onComplete(@NonNull Task<AuthResult> task) {
if(task.isSuccessful())
{
FirebaseUser firebaseUser = auth.getCurrentUser();
assert firebaseUser != null;
String userId = firebaseUser.getUid();
reference = FirebaseDatabase.getInstance().getReference("Users").child(userId);
HashMap<String,String> hashMap = new HashMap<>();
hashMap.put("id",userId);
hashMap.put("Username",username);
reference.setValue(hashMap).addOnCompleteListener(new OnCompleteListener<Void>() {
@Override
public void onComplete(@NonNull Task<Void> task) {
if(task.isSuccessful())
{
Intent intent = new Intent(Signup.this,HomePage.class);
intent.addFlags(intent.FLAG_ACTIVITY_CLEAR_TASK|intent.FLAG_ACTIVITY_NEW_TASK);
startActivity(intent);
finish();
}
}
});
}else {
Toast.makeText(Signup.this,"You cannot register with this email or password",Toast.LENGTH_SHORT).show();
}
}
}).addOnFailureListener(new OnFailureListener() {
@Override
public void onFailure(@NonNull Exception e) {
pd.dismiss();
Toast.makeText(Signup.this, e.getMessage(), Toast.LENGTH_SHORT).show();
}
});
}
}