Created
May 4, 2018 13:55
-
-
Save kantapp/806e4fe36074496c7bf469195b567dcd to your computer and use it in GitHub Desktop.
Phone Auth Using Firebase
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package com.kantapp.myfireapp.Activity; | |
import android.content.Intent; | |
import android.support.annotation.NonNull; | |
import android.support.design.widget.Snackbar; | |
import android.support.v7.app.AppCompatActivity; | |
import android.os.Bundle; | |
import android.text.TextUtils; | |
import android.util.Log; | |
import android.view.View; | |
import android.widget.Button; | |
import android.widget.EditText; | |
import android.widget.TextView; | |
import com.google.android.gms.tasks.OnCompleteListener; | |
import com.google.android.gms.tasks.Task; | |
import com.google.firebase.FirebaseException; | |
import com.google.firebase.FirebaseTooManyRequestsException; | |
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.kantapp.myfireapp.MainActivity; | |
import com.kantapp.myfireapp.R; | |
import java.util.concurrent.TimeUnit; | |
public class PhoneAuth extends AppCompatActivity { | |
private static final String TAG = "PhoneAuth"; | |
private TextView textView; | |
private FirebaseAuth auth; | |
EditText mPhoneNumberField, mVerificationField; | |
Button mStartButton, mVerifyButton, mResendButton; | |
private PhoneAuthProvider.ForceResendingToken mResendToken; | |
private PhoneAuthProvider.OnVerificationStateChangedCallbacks mCallbacks; | |
String mVerificationId; | |
@Override | |
protected void onCreate(Bundle savedInstanceState) { | |
super.onCreate(savedInstanceState); | |
setContentView(R.layout.activity_phone_auth); | |
textView=findViewById(R.id.mobileNo); | |
auth=FirebaseAuth.getInstance(); | |
mPhoneNumberField = (EditText) findViewById(R.id.field_phone_number); | |
mVerificationField = (EditText) findViewById(R.id.field_verification_code); | |
mStartButton = (Button) findViewById(R.id.button_start_verification); | |
mVerifyButton = (Button) findViewById(R.id.button_verify_phone); | |
mResendButton = (Button) findViewById(R.id.button_resend); | |
mStartButton.setOnClickListener(new View.OnClickListener() { | |
@Override | |
public void onClick(View v) { | |
if (!validatePhoneNumber()) { | |
return; | |
} | |
startPhoneNumberVerification(mPhoneNumberField.getText().toString()); | |
} | |
}); | |
mVerifyButton.setOnClickListener(new View.OnClickListener() { | |
@Override | |
public void onClick(View v) { | |
String code = mVerificationField.getText().toString(); | |
if (TextUtils.isEmpty(code)) { | |
mVerificationField.setError("Cannot be empty."); | |
return; | |
} | |
verifyPhoneNumberWithCode(mVerificationId, code); | |
} | |
}); | |
mResendButton.setOnClickListener(new View.OnClickListener() { | |
@Override | |
public void onClick(View v) { | |
resendVerificationCode(mPhoneNumberField.getText().toString(), mResendToken); | |
} | |
}); | |
mCallbacks=new PhoneAuthProvider.OnVerificationStateChangedCallbacks() { | |
@Override | |
public void onVerificationCompleted(PhoneAuthCredential phoneAuthCredential) { | |
Log.d(TAG, "onVerificationCompleted:" + phoneAuthCredential); | |
signInWithPhoneAuthCredential(phoneAuthCredential); | |
} | |
@Override | |
public void onVerificationFailed(FirebaseException e) { | |
Log.w(TAG, "onVerificationFailed", e); | |
if (e instanceof FirebaseAuthInvalidCredentialsException) | |
{ | |
mPhoneNumberField.setError("Invalid phone number."); | |
} else if (e instanceof FirebaseTooManyRequestsException) | |
{ | |
Snackbar.make(findViewById(android.R.id.content), "Quota exceeded.", | |
Snackbar.LENGTH_SHORT).show(); | |
} | |
} | |
@Override | |
public void onCodeSent(String s, PhoneAuthProvider.ForceResendingToken forceResendingToken) { | |
Log.d(TAG, "onCodeSent:" + s); | |
mVerificationId = s; | |
mResendToken = forceResendingToken; | |
} | |
}; | |
} | |
private boolean validatePhoneNumber() { | |
String phoneNumber = mPhoneNumberField.getText().toString(); | |
if (TextUtils.isEmpty(phoneNumber)) { | |
mPhoneNumberField.setError("Invalid phone number."); | |
return false; | |
} | |
return true; | |
} | |
private void startPhoneNumberVerification(String phoneNumber) { | |
PhoneAuthProvider.getInstance().verifyPhoneNumber( | |
phoneNumber, // Phone number to verify | |
60, // Timeout duration | |
TimeUnit.SECONDS, // Unit of timeout | |
this, // Activity (for callback binding) | |
mCallbacks); // OnVerificationStateChangedCallbacks | |
} | |
private void verifyPhoneNumberWithCode(String mVerificationId, String code) { | |
PhoneAuthCredential credential = PhoneAuthProvider.getCredential(mVerificationId, code); | |
signInWithPhoneAuthCredential(credential); | |
} | |
private void resendVerificationCode(String phoneNumber, PhoneAuthProvider.ForceResendingToken mResendToken) { | |
PhoneAuthProvider.getInstance().verifyPhoneNumber( | |
phoneNumber, // Phone number to verify | |
60, // Timeout duration | |
TimeUnit.SECONDS, // Unit of timeout | |
this, // Activity (for callback binding) | |
mCallbacks, // OnVerificationStateChangedCallbacks | |
mResendToken); // ForceResendingToken from callbacks | |
} | |
private void signInWithPhoneAuthCredential(PhoneAuthCredential phoneAuthCredential) { | |
auth.signInWithCredential(phoneAuthCredential) | |
.addOnCompleteListener(this, new OnCompleteListener<AuthResult>() { | |
@Override | |
public void onComplete(@NonNull Task<AuthResult> task) { | |
if (task.isSuccessful()) { | |
Log.d(TAG, "signInWithCredential:success"); | |
FirebaseUser user = task.getResult().getUser(); | |
startActivity(new Intent(getApplicationContext(), MainActivity.class)); | |
finish(); | |
} else { | |
Log.w(TAG, "signInWithCredential:failure", task.getException()); | |
if (task.getException() instanceof FirebaseAuthInvalidCredentialsException) { | |
mVerificationField.setError("Invalid code."); | |
} | |
} | |
} | |
}); | |
} | |
@Override | |
protected void onStart() { | |
super.onStart(); | |
if(auth.getCurrentUser()!=null) | |
{ | |
textView.setText(auth.getCurrentUser().getPhoneNumber()); | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment