11package com .example .ffccapp ;
22
3+ import androidx .annotation .NonNull ;
34import androidx .appcompat .app .AppCompatActivity ;
45
56import android .content .Intent ;
910import android .view .accessibility .AccessibilityNodeInfo ;
1011import android .widget .EditText ;
1112import android .widget .TextView ;
13+ import android .widget .Toast ;
1214
15+ import com .google .android .gms .tasks .OnCompleteListener ;
16+ import com .google .android .gms .tasks .Task ;
1317import com .google .firebase .FirebaseException ;
1418import com .google .firebase .FirebaseTooManyRequestsException ;
19+ import com .google .firebase .auth .AuthResult ;
1520import com .google .firebase .auth .FirebaseAuth ;
1621import com .google .firebase .auth .FirebaseAuthInvalidCredentialsException ;
22+ import com .google .firebase .auth .FirebaseUser ;
1723import com .google .firebase .auth .PhoneAuthCredential ;
1824import com .google .firebase .auth .PhoneAuthProvider ;
1925
2026import java .util .concurrent .TimeUnit ;
2127
2228public class MainActivity extends AppCompatActivity {
23- public static PhoneAuthProvider . OnVerificationStateChangedCallbacks mCallbacks ;
29+
2430 static FirebaseAuth mAuth ;
25- TextView phone ;
31+ static TextView phone ,code ;
32+ static String codesent ,cod ;
33+
34+ public void logo (View v )
35+ {
36+ cod =code .getText ().toString ();
37+ Toast .makeText (this , codesent , Toast .LENGTH_SHORT ).show ();
38+ PhoneAuthCredential credential =PhoneAuthProvider .getCredential (codesent ,cod );
39+ signInWithPhoneAuthCredential (credential );
40+ }
41+ public void clickStartVerification (View v ){
2642
27- public void clickStartVerification (){
28- getOtp (phone .getText ().toString ());
43+ String z =(phone .getText ().toString ());
44+ if (z ==null )
45+ {
46+ Toast .makeText (this , "Enter a valid number" , Toast .LENGTH_SHORT ).show ();
47+ }
48+ else
49+ {
50+ getOtp (z );
51+ }
52+ }
53+ private void signInWithPhoneAuthCredential (PhoneAuthCredential credential ) {
54+ mAuth .signInWithCredential (credential )
55+ .addOnCompleteListener (this , new OnCompleteListener <AuthResult >() {
56+ @ Override
57+ public void onComplete (@ NonNull Task <AuthResult > task ) {
58+ if (task .isSuccessful ()) {
59+ Toast .makeText (MainActivity .this , "Login Successful" , Toast .LENGTH_SHORT ).show ();
60+
61+ } else {
62+ if (task .getException () instanceof FirebaseAuthInvalidCredentialsException ) {
63+ // The verification code entered was invalid
64+
65+ Toast .makeText (MainActivity .this , "Login UnSuccessful" , Toast .LENGTH_SHORT ).show ();
66+ }
67+ }
68+ }
69+ });
2970 }
3071 private void getOtp (String phoneNumber ) {
3172 PhoneAuthProvider .getInstance ().verifyPhoneNumber (
@@ -36,18 +77,29 @@ private void getOtp(String phoneNumber) {
3677 mCallbacks ); // OnVerificationStateChangedCallbacks
3778 }
3879
39-
40- public void next (View v )
41- {
42- Intent I =new Intent (MainActivity .this ,Main .class );
43- startActivity (I );
44- }
4580 @ Override
4681 protected void onCreate (Bundle savedInstanceState ) {
4782 super .onCreate (savedInstanceState );
4883 setContentView (R .layout .activity_main );
4984 mAuth =FirebaseAuth .getInstance ();
5085 phone =(EditText )findViewById (R .id .phone );
51-
86+ code = findViewById ( R . id . code );
5287 }
88+ static PhoneAuthProvider .OnVerificationStateChangedCallbacks mCallbacks =new PhoneAuthProvider .OnVerificationStateChangedCallbacks () {
89+ @ Override
90+ public void onVerificationCompleted (@ NonNull PhoneAuthCredential phoneAuthCredential ) {
91+
92+ }
93+
94+ @ Override
95+ public void onVerificationFailed (@ NonNull FirebaseException e ) {
96+
97+ }
98+
99+ @ Override
100+ public void onCodeSent (@ NonNull String s , @ NonNull PhoneAuthProvider .ForceResendingToken forceResendingToken ) {
101+ super .onCodeSent (s , forceResendingToken );
102+ codesent =s ;
103+ }
104+ };
53105}
0 commit comments