-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathVerification.java
205 lines (159 loc) · 7.93 KB
/
Verification.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
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
package org.colibrisoft.colibri;
import android.content.Intent;
import android.support.annotation.NonNull;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
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.Task;
import com.google.firebase.FirebaseException;
import com.google.firebase.auth.AuthResult;
import com.google.firebase.auth.FirebaseAuth;
import com.google.firebase.auth.FirebaseAuthInvalidCredentialsException;
import com.google.firebase.auth.FirebaseUser;
import com.google.firebase.auth.PhoneAuthCredential;
import com.google.firebase.auth.PhoneAuthProvider;
import com.google.firebase.database.DatabaseReference;
import com.google.firebase.database.FirebaseDatabase;
import com.google.firebase.iid.FirebaseInstanceId;
import java.util.HashMap;
import java.util.concurrent.TimeUnit;
public class Verification extends AppCompatActivity {
//These are the objects needed
//It is the verification id that will be sent to the user
private String mVerificationId;
//The edittext to input the code
private EditText editTextCode;
//firebase auth object
private FirebaseAuth mAuth;
private TextView mCodeError;
protected Button verify_button;
protected TextView user_phone;
protected String PhoneVal;
protected EditText send_code;
private DatabaseReference mDatabase;
private DatabaseReference mUserDatabase;
private String UserNameVal;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_verification);
//initializing objects
mAuth = FirebaseAuth.getInstance();
editTextCode = findViewById(R.id.send_code);
mCodeError = (TextView) findViewById(R.id.code_error);
user_phone=findViewById(R.id.user_phone);
PhoneVal=getIntent().getExtras().getString("Value");
user_phone.setText(PhoneVal);
verify_button= findViewById(R.id.verify_button);
UserNameVal = getIntent().getExtras().getString("UserName");
mUserDatabase = FirebaseDatabase.getInstance().getReference().child("Users");
//getting mobile number from the previous activity
//and sending the verification code to the number
sendVerificationCode(PhoneVal);
//if the automatic sms detection did not work, user can also enter the code manually
//so adding a click listener to the button
findViewById(R.id.verify_button).setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
String code = editTextCode.getText().toString().trim();
if (code.isEmpty()) {
editTextCode.setError("Enter valid code");
editTextCode.requestFocus();
return;
}
//verifying the code entered manually
verifyVerificationCode(code);
}
});
}
//the method is sending verification code
//the country id is concatenated
//you can take the country id as user input as well
private void sendVerificationCode(String PhoneVal) {
PhoneAuthProvider.getInstance().verifyPhoneNumber(
PhoneVal,
60,
TimeUnit.SECONDS,
this,
mCallbacks);
}
//the callback to detect the verification status
private PhoneAuthProvider.OnVerificationStateChangedCallbacks
mCallbacks = new PhoneAuthProvider.OnVerificationStateChangedCallbacks() {
@Override
public void onVerificationCompleted(PhoneAuthCredential phoneAuthCredential) {
//Getting the code sent by SMS
String code = phoneAuthCredential.getSmsCode();
//sometime the code is not detected automatically
//in this case the code will be null
//so user has to manually enter the code
if (code != null) {
editTextCode.setText(code);
//verifying the code
verifyVerificationCode(code);
}
}
@Override
public void onVerificationFailed(FirebaseException e) {
Toast.makeText(Verification.this, e.getMessage(), Toast.LENGTH_LONG).show();
}
@Override
public void onCodeSent(String s, PhoneAuthProvider.ForceResendingToken forceResendingToken) {
super.onCodeSent(s, forceResendingToken);
//storing the verification id that is sent to the user
mVerificationId = s;
}
};
private void verifyVerificationCode(String code) {
//creating the credential
PhoneAuthCredential credential = PhoneAuthProvider.getCredential(mVerificationId, code);
//signing the user
signInWithPhoneAuthCredential(credential);
}
private void signInWithPhoneAuthCredential(PhoneAuthCredential credential) {
mAuth.signInWithCredential(credential)
.addOnCompleteListener(Verification.this, 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);
String deviceToken = FirebaseInstanceId.getInstance().getToken();
HashMap<String, String> userMap = new HashMap<>();
userMap.put("phone", PhoneVal);
userMap.put("name", UserNameVal);
userMap.put("status", "Hi there, I'm using Colibri Messenger!" );
userMap.put("image", "default");
userMap.put("thumb_image", "default");
userMap.put("device_token", deviceToken);
mDatabase.setValue(userMap).addOnCompleteListener(new OnCompleteListener<Void>() {
@Override
public void onComplete(@NonNull Task<Void> task) {
if (task.isSuccessful()){
Intent intent = new Intent(Verification.this, HomePage.class);
intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_CLEAR_TASK);
//intent.putExtra("Phone_Val", PhoneVal);
//intent.putExtra("Name_Val", UserNameVal);
startActivity(intent);
finish();
}
}
});
//verification successful we will start the profile activity
} else {
//verification unsuccessful.. display an error message
Toast.makeText(Verification.this, "Something is wrong, we will fix it soon...", Toast.LENGTH_SHORT).show();
if (task.getException() instanceof FirebaseAuthInvalidCredentialsException) {
send_code.setError("Enter valid code");
}
}
}
});
}
}