Skip to content

Commit 73b8494

Browse files
committed
PhoneAuth2
1 parent 30ffc8c commit 73b8494

4 files changed

Lines changed: 91 additions & 16 deletions

File tree

‎app/src/main/java/com/example/ffccapp/Main.java‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,7 @@ public boolean onCreateOptionsMenu(Menu menu) {
3434
menuInflater.inflate(R.menu.main_menu,menu);
3535
return super.onCreateOptionsMenu(menu);
3636
}
37+
3738
@Override
3839
public boolean onOptionsItemSelected(@NonNull MenuItem item) {
3940
super.onOptionsItemSelected(item);
Lines changed: 63 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
package com.example.ffccapp;
22

3+
import androidx.annotation.NonNull;
34
import androidx.appcompat.app.AppCompatActivity;
45

56
import android.content.Intent;
@@ -9,23 +10,63 @@
910
import android.view.accessibility.AccessibilityNodeInfo;
1011
import android.widget.EditText;
1112
import 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;
1317
import com.google.firebase.FirebaseException;
1418
import com.google.firebase.FirebaseTooManyRequestsException;
19+
import com.google.firebase.auth.AuthResult;
1520
import com.google.firebase.auth.FirebaseAuth;
1621
import com.google.firebase.auth.FirebaseAuthInvalidCredentialsException;
22+
import com.google.firebase.auth.FirebaseUser;
1723
import com.google.firebase.auth.PhoneAuthCredential;
1824
import com.google.firebase.auth.PhoneAuthProvider;
1925

2026
import java.util.concurrent.TimeUnit;
2127

2228
public 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
}

‎app/src/main/java/com/example/ffccapp/Verify.java‎

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,10 @@ public class Verify extends AppCompatActivity {
2020

2121
private PhoneAuthProvider.OnVerificationStateChangedCallbacks mCallbacks;
2222
private String verificationId;
23-
PhoneAuthCredential credential = PhoneAuthProvider.getCredential(verificationId, code);
23+
PhoneAuthCredential credential = PhoneAuthProvider.getCredential(verificationId, " ");
24+
private String mVerificationId;
25+
private PhoneAuthProvider.ForceResendingToken mResendToken;
26+
2427
@Override
2528
protected void onCreate(Bundle savedInstanceState) {
2629
super.onCreate(savedInstanceState);

‎app/src/main/res/layout/activity_main.xml‎

Lines changed: 23 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -40,18 +40,37 @@
4040
android:ems="10"
4141
android:hint="Phone Number"
4242
android:inputType="phone"
43-
android:layout_centerInParent="true"
43+
4444
/>
45+
<Button
46+
android:id="@+id/send"
47+
android:layout_width="162dp"
48+
android:layout_height="49dp"
49+
android:layout_below="@id/phone"
50+
android:layout_centerHorizontal="true"
51+
android:layout_marginTop="20dp"
52+
android:background="@drawable/rectangle_2"
53+
android:onClick="clickStartVerification"
54+
android:text="SEND" />
55+
<EditText
56+
android:id="@+id/code"
57+
android:layout_width="match_parent"
58+
android:layout_height="wrap_content"
59+
android:ems="10"
60+
android:hint="Verification code"
61+
android:inputType="phone"
62+
android:layout_below="@+id/send"
63+
/>
4564

4665
<Button
4766
android:id="@+id/login"
4867
android:layout_width="162dp"
4968
android:layout_height="49dp"
50-
android:layout_below="@id/phone"
69+
android:layout_below="@id/code"
5170
android:layout_centerHorizontal="true"
52-
android:layout_marginTop="70dp"
71+
android:layout_marginTop="30dp"
5372
android:background="@drawable/rectangle_2"
54-
android:onClick="next"
73+
android:onClick="logo"
5574
android:text="LOGIN" />
5675
</RelativeLayout>
5776
</RelativeLayout>

0 commit comments

Comments
 (0)