com.google.firebase.auth.PhoneAuthCredential Java Examples
The following examples show how to use
com.google.firebase.auth.PhoneAuthCredential.
You can vote up the ones you like or vote down the ones you don't like,
and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: OtpGenerator.java From GetIntoClub with GNU General Public License v3.0 | 6 votes |
private void SigninWithPhone(PhoneAuthCredential credential) { auth.signInWithCredential(credential) .addOnCompleteListener(new OnCompleteListener<AuthResult>() { @Override public void onComplete(@NonNull Task<AuthResult> task) { if (task.isSuccessful()) { FirebaseAuth.getInstance().signOut(); startActivity(new Intent(OtpGenerator.this, DashActivity.class)); finish(); } else { Toast.makeText(OtpGenerator.this, "Incorrect OTP", Toast.LENGTH_SHORT).show(); } } }); }
Example #2
Source File: OtpGenerator.java From GetIntoClub with GNU General Public License v3.0 | 6 votes |
private void StartFirebaseLogin() { auth = FirebaseAuth.getInstance(); mCallback = new PhoneAuthProvider.OnVerificationStateChangedCallbacks() { @Override public void onVerificationCompleted(PhoneAuthCredential phoneAuthCredential) { Toast.makeText(OtpGenerator.this, "verification completed", Toast.LENGTH_SHORT).show(); } @Override public void onVerificationFailed(FirebaseException e) { Toast.makeText(OtpGenerator.this, "verification failed", Toast.LENGTH_SHORT).show(); } @Override public void onCodeSent(String s, PhoneAuthProvider.ForceResendingToken forceResendingToken) { super.onCodeSent(s, forceResendingToken); verificationCode = s; Toast.makeText(OtpGenerator.this, "Code sent", Toast.LENGTH_SHORT).show(); btnSignIn.setVisibility(View.VISIBLE); btnGenerateOTP.setVisibility(View.GONE); } }; }
Example #3
Source File: verifyPatient2.java From Doctorave with MIT License | 6 votes |
private void signInWithPhoneAuthCredential(PhoneAuthCredential credential) { FirebaseAuth.getInstance().signInWithCredential(credential) .addOnCompleteListener(this, new OnCompleteListener<AuthResult>() { @Override public void onComplete(@NonNull Task<AuthResult> task) { if (task.isSuccessful()) { // Sign in success, update UI with the signed-in user's information FirebaseUser user = task.getResult().getUser(); // ... putInformationInFb(verifyPatient2.this); Intent intent1 = new Intent(verifyPatient2.this, hospitalActivity.class); intent1.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TASK); intent1.putExtra("phone", phone); startActivity(intent1); } else { // Sign in failed, display a message and update the UI if (task.getException() instanceof FirebaseAuthInvalidCredentialsException) { // The verification code entered was invalid } } } }); }
Example #4
Source File: verifyPatient.java From Doctorave with MIT License | 6 votes |
private void firebasePhoneVerification() { mCallbacks = new PhoneAuthProvider.OnVerificationStateChangedCallbacks() { @Override public void onVerificationCompleted(PhoneAuthCredential phoneAuthCredential) { } @Override public void onVerificationFailed(FirebaseException e) { progressDialog.dismiss(); Toast.makeText(verifyPatient.this, e.getMessage(), Toast.LENGTH_LONG).show(); } @Override public void onCodeSent(String s, PhoneAuthProvider.ForceResendingToken forceResendingToken) { progressDialog.dismiss(); Toast.makeText(verifyPatient.this, "Code Sent ", Toast.LENGTH_LONG).show(); Intent intent = new Intent(verifyPatient.this, verifyPatient2.class); intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TASK); intent.putExtra("verificationId", s); intent.putExtra("phone", fullPhoneNo); startActivity(intent); } }; }
Example #5
Source File: MultiFactorSignInActivity.java From quickstart-android with Apache License 2.0 | 6 votes |
private PhoneAuthProvider.OnVerificationStateChangedCallbacks generateCallbacks() { return new PhoneAuthProvider.OnVerificationStateChangedCallbacks() { @Override public void onVerificationCompleted(@NonNull PhoneAuthCredential phoneAuthCredential) { MultiFactorSignInActivity.this.mPhoneAuthCredential = phoneAuthCredential; mBinding.finishMfaSignIn.performClick(); Toast.makeText( MultiFactorSignInActivity.this, "Verification complete!", Toast.LENGTH_SHORT) .show(); } @Override public void onCodeSent(@NonNull String verificationId, @NonNull PhoneAuthProvider.ForceResendingToken token) { MultiFactorSignInActivity.this.mVerificationId = verificationId; mBinding.finishMfaSignIn.setClickable(true); } @Override public void onVerificationFailed(@NonNull FirebaseException e) { Toast.makeText( MultiFactorSignInActivity.this, "Error: " + e.getMessage(), Toast.LENGTH_SHORT) .show(); } }; }
Example #6
Source File: PhoneNumberVerificationHandler.java From FirebaseUI-Android with Apache License 2.0 | 5 votes |
public void verifyPhoneNumber(final String number, boolean force) { setResult(Resource.<PhoneVerification>forLoading()); getPhoneAuth().verifyPhoneNumber( number, AUTO_RETRIEVAL_TIMEOUT_SECONDS, TimeUnit.SECONDS, TaskExecutors.MAIN_THREAD, new PhoneAuthProvider.OnVerificationStateChangedCallbacks() { @Override public void onVerificationCompleted(@NonNull PhoneAuthCredential credential) { setResult(Resource.forSuccess(new PhoneVerification( number, credential, true))); } @Override public void onVerificationFailed(FirebaseException e) { setResult(Resource.<PhoneVerification>forFailure(e)); } @Override public void onCodeSent(@NonNull String verificationId, @NonNull PhoneAuthProvider.ForceResendingToken token) { mVerificationId = verificationId; mForceResendingToken = token; setResult(Resource.<PhoneVerification>forFailure( new PhoneNumberVerificationRequiredException(number))); } }, force ? mForceResendingToken : null); }
Example #7
Source File: PhoneVerification.java From FirebaseUI-Android with Apache License 2.0 | 5 votes |
public PhoneVerification(@NonNull String number, @NonNull PhoneAuthCredential credential, boolean verified) { mNumber = number; mCredential = credential; mIsAutoVerified = verified; }
Example #8
Source File: PhoneProviderResponseHandler.java From FirebaseUI-Android with Apache License 2.0 | 5 votes |
public void startSignIn(@NonNull PhoneAuthCredential credential, @NonNull final IdpResponse response) { if (!response.isSuccessful()) { setResult(Resource.<IdpResponse>forFailure(response.getError())); return; } if (!response.getProviderType().equals(PhoneAuthProvider.PROVIDER_ID)) { throw new IllegalStateException( "This handler cannot be used without a phone response."); } setResult(Resource.<IdpResponse>forLoading()); AuthOperationManager.getInstance() .signInAndLinkWithCredential(getAuth(), getArguments(), credential) .addOnSuccessListener(new OnSuccessListener<AuthResult>() { @Override public void onSuccess(AuthResult result) { handleSuccess(response, result); } }) .addOnFailureListener(new OnFailureListener() { @Override public void onFailure(@NonNull Exception e) { if (e instanceof FirebaseAuthUserCollisionException) { // With phone auth, this only happens if we are trying to upgrade // an anonymous account using a phone number that is already registered // on another account handleMergeFailure(((FirebaseAuthUserCollisionException) e).getUpdatedCredential()); } else { setResult(Resource.<IdpResponse>forFailure(e)); } } }); }
Example #9
Source File: MultiFactorEnrollActivity.java From quickstart-android with Apache License 2.0 | 5 votes |
private void enrollWithPhoneAuthCredential(PhoneAuthCredential credential) { FirebaseAuth.getInstance() .getCurrentUser() .getMultiFactor() .enroll(PhoneMultiFactorGenerator.getAssertion(credential), /* displayName= */ null) .addOnSuccessListener(new OnSuccessListener<Void>() { @Override public void onSuccess(Void aVoid) { Toast.makeText( MultiFactorEnrollActivity.this, "MFA enrollment was successful", Toast.LENGTH_LONG) .show(); finish(); } }) .addOnFailureListener(new OnFailureListener() { @Override public void onFailure(@NonNull Exception e) { Log.d(TAG, "MFA failure", e); Toast.makeText( MultiFactorEnrollActivity.this, "MFA enrollment was unsuccessful. " + e, Toast.LENGTH_LONG) .show(); } }); }
Example #10
Source File: MultiFactorEnrollActivity.java From quickstart-android with Apache License 2.0 | 5 votes |
private void onClickSignInWithPhoneNumber() { String smsCode = mBinding.fieldVerificationCode.getText().toString(); if (TextUtils.isEmpty(smsCode)) { return; } PhoneAuthCredential credential = PhoneAuthProvider.getCredential(mCodeVerificationId, smsCode); enrollWithPhoneAuthCredential(credential); }
Example #11
Source File: PhoneAuthActivity.java From quickstart-android with Apache License 2.0 | 5 votes |
private void signInWithPhoneAuthCredential(PhoneAuthCredential credential) { mAuth.signInWithCredential(credential) .addOnCompleteListener(this, new OnCompleteListener<AuthResult>() { @Override public void onComplete(@NonNull Task<AuthResult> task) { if (task.isSuccessful()) { // Sign in success, update UI with the signed-in user's information Log.d(TAG, "signInWithCredential:success"); FirebaseUser user = task.getResult().getUser(); // [START_EXCLUDE] updateUI(STATE_SIGNIN_SUCCESS, user); // [END_EXCLUDE] } else { // Sign in failed, display a message and update the UI Log.w(TAG, "signInWithCredential:failure", task.getException()); if (task.getException() instanceof FirebaseAuthInvalidCredentialsException) { // The verification code entered was invalid // [START_EXCLUDE silent] mBinding.fieldVerificationCode.setError("Invalid code."); // [END_EXCLUDE] } // [START_EXCLUDE silent] // Update UI updateUI(STATE_SIGNIN_FAILED); // [END_EXCLUDE] } } }); }
Example #12
Source File: MainActivity.java From snippets-android with Apache License 2.0 | 5 votes |
public void testPhoneAutoRetrieve() { // [START auth_test_phone_auto] // The test phone number and code should be whitelisted in the console. String phoneNumber = "+16505554567"; String smsCode = "123456"; FirebaseAuth firebaseAuth = FirebaseAuth.getInstance(); FirebaseAuthSettings firebaseAuthSettings = firebaseAuth.getFirebaseAuthSettings(); // Configure faking the auto-retrieval with the whitelisted numbers. firebaseAuthSettings.setAutoRetrievedSmsCodeForPhoneNumber(phoneNumber, smsCode); PhoneAuthProvider phoneAuthProvider = PhoneAuthProvider.getInstance(); phoneAuthProvider.verifyPhoneNumber( phoneNumber, 60L, TimeUnit.SECONDS, this, /* activity */ new PhoneAuthProvider.OnVerificationStateChangedCallbacks() { @Override public void onVerificationCompleted(PhoneAuthCredential credential) { // Instant verification is applied and a credential is directly returned. // ... } // [START_EXCLUDE] @Override public void onVerificationFailed(FirebaseException e) { } // [END_EXCLUDE] }); // [END auth_test_phone_auto] }
Example #13
Source File: MainActivity.java From snippets-android with Apache License 2.0 | 5 votes |
public void testPhoneVerify() { // [START auth_test_phone_verify] String phoneNum = "+16505554567"; String testVerificationCode = "123456"; // Whenever verification is triggered with the whitelisted number, // provided it is not set for auto-retrieval, onCodeSent will be triggered. PhoneAuthProvider.getInstance().verifyPhoneNumber( phoneNum, 30L /*timeout*/, TimeUnit.SECONDS, this, new PhoneAuthProvider.OnVerificationStateChangedCallbacks() { @Override public void onCodeSent(String verificationId, PhoneAuthProvider.ForceResendingToken forceResendingToken) { // Save the verification id somewhere // ... // The corresponding whitelisted code above should be used to complete sign-in. MainActivity.this.enableUserManuallyInputCode(); } @Override public void onVerificationCompleted(PhoneAuthCredential phoneAuthCredential) { // Sign in with the credential // ... } @Override public void onVerificationFailed(FirebaseException e) { // ... } }); // [END auth_test_phone_verify] }
Example #14
Source File: PhoneAuthActivity.java From quickstart-android with Apache License 2.0 | 4 votes |
private void updateUI(int uiState, PhoneAuthCredential cred) { updateUI(uiState, null, cred); }
Example #15
Source File: PhoneAuthActivity.java From quickstart-android with Apache License 2.0 | 4 votes |
private void updateUI(int uiState, FirebaseUser user, PhoneAuthCredential cred) { switch (uiState) { case STATE_INITIALIZED: // Initialized state, show only the phone number field and start button enableViews(mBinding.buttonStartVerification, mBinding.fieldPhoneNumber); disableViews(mBinding.buttonVerifyPhone, mBinding.buttonResend, mBinding.fieldVerificationCode); mBinding.detail.setText(null); break; case STATE_CODE_SENT: // Code sent state, show the verification field, the enableViews(mBinding.buttonVerifyPhone, mBinding.buttonResend, mBinding.fieldPhoneNumber, mBinding.fieldVerificationCode); disableViews(mBinding.buttonStartVerification); mBinding.detail.setText(R.string.status_code_sent); break; case STATE_VERIFY_FAILED: // Verification has failed, show all options enableViews(mBinding.buttonStartVerification, mBinding.buttonVerifyPhone, mBinding.buttonResend, mBinding.fieldPhoneNumber, mBinding.fieldVerificationCode); mBinding.detail.setText(R.string.status_verification_failed); break; case STATE_VERIFY_SUCCESS: // Verification has succeeded, proceed to firebase sign in disableViews(mBinding.buttonStartVerification, mBinding.buttonVerifyPhone, mBinding.buttonResend, mBinding.fieldPhoneNumber, mBinding.fieldVerificationCode); mBinding.detail.setText(R.string.status_verification_succeeded); // Set the verification text based on the credential if (cred != null) { if (cred.getSmsCode() != null) { mBinding.fieldVerificationCode.setText(cred.getSmsCode()); } else { mBinding.fieldVerificationCode.setText(R.string.instant_validation); } } break; case STATE_SIGNIN_FAILED: // No-op, handled by sign-in check mBinding.detail.setText(R.string.status_sign_in_failed); break; case STATE_SIGNIN_SUCCESS: // Np-op, handled by sign-in check break; } if (user == null) { // Signed out mBinding.phoneAuthFields.setVisibility(View.VISIBLE); mBinding.signedInButtons.setVisibility(View.GONE); mBinding.status.setText(R.string.signed_out); } else { // Signed in mBinding.phoneAuthFields.setVisibility(View.GONE); mBinding.signedInButtons.setVisibility(View.VISIBLE); enableViews(mBinding.fieldPhoneNumber, mBinding.fieldVerificationCode); mBinding.fieldPhoneNumber.setText(null); mBinding.fieldVerificationCode.setText(null); mBinding.status.setText(R.string.signed_in); mBinding.detail.setText(getString(R.string.firebase_status_fmt, user.getUid())); } }
Example #16
Source File: MultiFactorEnrollActivity.java From quickstart-android with Apache License 2.0 | 4 votes |
private void onClickVerifyPhoneNumber() { String phoneNumber = mBinding.fieldPhoneNumber.getText().toString(); OnVerificationStateChangedCallbacks callbacks = new OnVerificationStateChangedCallbacks() { @Override public void onVerificationCompleted(PhoneAuthCredential credential) { // Instant-validation has been disabled (see requireSmsValidation below). // Auto-retrieval has also been disabled (timeout is set to 0). // This should never be triggered. throw new RuntimeException( "onVerificationCompleted() triggered with instant-validation and auto-retrieval disabled."); } @Override public void onCodeSent( final String verificationId, PhoneAuthProvider.ForceResendingToken token) { Log.d(TAG, "onCodeSent:" + verificationId); Toast.makeText( MultiFactorEnrollActivity.this, "SMS code has been sent", Toast.LENGTH_SHORT) .show(); mCodeVerificationId = verificationId; } @Override public void onVerificationFailed(FirebaseException e) { Log.w(TAG, "onVerificationFailed ", e); Toast.makeText( MultiFactorEnrollActivity.this, "Verification failed: " + e.getMessage(), Toast.LENGTH_SHORT) .show(); } }; FirebaseAuth.getInstance() .getCurrentUser() .getMultiFactor() .getSession() .addOnCompleteListener( new OnCompleteListener<MultiFactorSession>() { @Override public void onComplete(@NonNull Task<MultiFactorSession> task) { if (task.isSuccessful()) { PhoneAuthOptions phoneAuthOptions = PhoneAuthOptions.newBuilder() .setPhoneNumber(phoneNumber) // A timeout of 0 disables SMS-auto-retrieval. .setTimeout(0L, TimeUnit.SECONDS) .setMultiFactorSession(task.getResult()) .setCallbacks(callbacks) // Disable instant-validation. .requireSmsValidation(true) .build(); PhoneAuthProvider.verifyPhoneNumber(phoneAuthOptions); } else { Toast.makeText( MultiFactorEnrollActivity.this, "Failed to get session: " + task.getException(), Toast.LENGTH_SHORT) .show(); } } }); }
Example #17
Source File: PhoneAuthActivity.java From quickstart-android with Apache License 2.0 | 4 votes |
private void verifyPhoneNumberWithCode(String verificationId, String code) { // [START verify_with_code] PhoneAuthCredential credential = PhoneAuthProvider.getCredential(verificationId, code); // [END verify_with_code] signInWithPhoneAuthCredential(credential); }
Example #18
Source File: PhoneAuthActivity.java From quickstart-android with Apache License 2.0 | 4 votes |
@Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); mBinding = ActivityPhoneAuthBinding.inflate(getLayoutInflater()); setContentView(mBinding.getRoot()); // Restore instance state if (savedInstanceState != null) { onRestoreInstanceState(savedInstanceState); } // Assign click listeners mBinding.buttonStartVerification.setOnClickListener(this); mBinding.buttonVerifyPhone.setOnClickListener(this); mBinding.buttonResend.setOnClickListener(this); mBinding.signOutButton.setOnClickListener(this); // [START initialize_auth] // Initialize Firebase Auth mAuth = FirebaseAuth.getInstance(); // [END initialize_auth] // Initialize phone auth callbacks // [START phone_auth_callbacks] mCallbacks = new PhoneAuthProvider.OnVerificationStateChangedCallbacks() { @Override public void onVerificationCompleted(PhoneAuthCredential credential) { // This callback will be invoked in two situations: // 1 - Instant verification. In some cases the phone number can be instantly // verified without needing to send or enter a verification code. // 2 - Auto-retrieval. On some devices Google Play services can automatically // detect the incoming verification SMS and perform verification without // user action. Log.d(TAG, "onVerificationCompleted:" + credential); // [START_EXCLUDE silent] mVerificationInProgress = false; // [END_EXCLUDE] // [START_EXCLUDE silent] // Update the UI and attempt sign in with the phone credential updateUI(STATE_VERIFY_SUCCESS, credential); // [END_EXCLUDE] signInWithPhoneAuthCredential(credential); } @Override public void onVerificationFailed(FirebaseException e) { // This callback is invoked in an invalid request for verification is made, // for instance if the the phone number format is not valid. Log.w(TAG, "onVerificationFailed", e); // [START_EXCLUDE silent] mVerificationInProgress = false; // [END_EXCLUDE] if (e instanceof FirebaseAuthInvalidCredentialsException) { // Invalid request // [START_EXCLUDE] mBinding.fieldPhoneNumber.setError("Invalid phone number."); // [END_EXCLUDE] } else if (e instanceof FirebaseTooManyRequestsException) { // The SMS quota for the project has been exceeded // [START_EXCLUDE] Snackbar.make(findViewById(android.R.id.content), "Quota exceeded.", Snackbar.LENGTH_SHORT).show(); // [END_EXCLUDE] } // Show a message and update the UI // [START_EXCLUDE] updateUI(STATE_VERIFY_FAILED); // [END_EXCLUDE] } @Override public void onCodeSent(@NonNull String verificationId, @NonNull PhoneAuthProvider.ForceResendingToken token) { // The SMS verification code has been sent to the provided phone number, we // now need to ask the user to enter the code and then construct a credential // by combining the code with a verification ID. Log.d(TAG, "onCodeSent:" + verificationId); // Save verification ID and resending token so we can use them later mVerificationId = verificationId; mResendToken = token; // [START_EXCLUDE] // Update UI updateUI(STATE_CODE_SENT); // [END_EXCLUDE] } }; // [END phone_auth_callbacks] }
Example #19
Source File: verifyPatient2.java From Doctorave with MIT License | 4 votes |
@Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_verify_patient2); if (doctorPreference.getPhoneNumberFromSP(this) != null){ Intent intent = new Intent(this, hospitalActivity.class); startActivity(intent); } codeReceived = findViewById(R.id.codeReceived); verifyCode = findViewById(R.id.verifyCode); progressDialog=new ProgressDialog(this,R.style.AppTheme_Dark_Dialog); progressDialog.setMessage("Please Wait.."); progressDialog.setCancelable(false); phone = getIntent().getStringExtra("phone"); verificationId = getIntent().getStringExtra("verificationId"); smsVerifyCatcher = new SmsVerifyCatcher(this, new OnSmsCatchListener<String>() { @Override public void onSmsCatch(String message) { code = message.substring(0, 6); /*code = message.split(":")[1].trim().substring(1);*/ codeReceived.setText(code); PhoneAuthCredential credential = PhoneAuthProvider.getCredential(verificationId, codeReceived.getText().toString().trim()); signInWithPhoneAuthCredential(credential); } }); firebaseAuth = FirebaseAuth.getInstance(); verifyCode.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { progressDialog.show(); PhoneAuthCredential credential = PhoneAuthProvider.getCredential(verificationId, codeReceived.getText().toString().trim()); signInWithPhoneAuthCredential(credential); } }); }
Example #20
Source File: PhoneVerification.java From FirebaseUI-Android with Apache License 2.0 | 4 votes |
@NonNull public PhoneAuthCredential getCredential() { return mCredential; }
Example #21
Source File: PhoneProviderHandler.java From capacitor-firebase-auth with MIT License | 4 votes |
@Override public void init(final CapacitorFirebaseAuth plugin) { this.plugin = plugin; this.mCallbacks = new PhoneAuthProvider.OnVerificationStateChangedCallbacks() { @Override public void onVerificationCompleted(PhoneAuthCredential credential) { Log.d(PHONE_TAG, "PhoneAuth:onVerificationCompleted:" + credential); mVerificationCode = credential.getSmsCode(); PluginCall call = plugin.getSavedCall(); // Notify listeners of Code Received event. JSObject jsEvent = new JSObject(); jsEvent.put("verificationId", mVerificationId); jsEvent.put("verificationCode", mVerificationCode); plugin.notifyListeners("cfaSignInPhoneOnCodeReceived", jsEvent); JSObject jsUser = new JSObject(); jsUser.put("callbackId", call.getCallbackId()); jsUser.put("providerId", credential.getProvider()); jsUser.put("verificationId", mVerificationId); jsUser.put("verificationCode", mVerificationCode); call.success(jsUser); } @Override public void onVerificationFailed(FirebaseException error) { Log.w(PHONE_TAG, "PhoneAuth:onVerificationFailed:" + error); if (error instanceof FirebaseAuthInvalidCredentialsException) { plugin.handleFailure("Invalid phone number.", error); } else if (error instanceof FirebaseTooManyRequestsException) { plugin.handleFailure("Quota exceeded.", error); } else { plugin.handleFailure("PhoneAuth Sign In failure.", error); } } public void onCodeSent(String verificationId, PhoneAuthProvider.ForceResendingToken token) { // The SMS verification code has been sent to the provided phone number, we // now need to ask the user to enter the code and then construct a credential // by combining the code with a verification ID. Log.d(PHONE_TAG, "onCodeSent:" + verificationId); // Save verification ID and resending token so we can use them later mVerificationId = verificationId; mResendToken = token; // Notify listeners of Code Sent event. JSObject jsEvent = new JSObject(); jsEvent.put("verificationId", mVerificationId); plugin.notifyListeners("cfaSignInPhoneOnCodeSent", jsEvent); } }; }