Created
January 3, 2024 07:18
-
-
Save 1AhsanSyed/ef3fd5c07eac2a06cacc2e3be60406dc to your computer and use it in GitHub Desktop.
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.teamx.devad.fragments.auth; | |
public class OTPFragment extends BaseFragment { | |
FragmentOTPBinding fragmentOTPBinding; | |
String firstname; | |
String lastName; | |
String userEmail; | |
String userPassword; | |
String userPhoneNumber; | |
String otpid; | |
FirebaseAuth mAuth; | |
private Bundle bundle; | |
String email; | |
@Override | |
public View onCreateView(LayoutInflater inflater, ViewGroup container, | |
Bundle savedInstanceState) { | |
fragmentOTPBinding = FragmentOTPBinding.inflate(getLayoutInflater(),container,false); | |
mAuth = FirebaseAuth.getInstance(); | |
// mAuth.getFirebaseAuthSettings().setAppVerificationDisabledForTesting(false); | |
bundle = getArguments(); | |
if(bundle != null){ | |
email = bundle.getString("email"); | |
userPhoneNumber = bundle.getString("phone" ); | |
} | |
else{ | |
getUserDetails(); | |
} | |
sentOTPcode(); | |
fragmentOTPBinding.resentOtp.setOnClickListener(new View.OnClickListener() { | |
@Override | |
public void onClick(View v) { | |
showToast("An OTP has been sent to your number"); | |
sentOTPcode(); | |
} | |
}); | |
fragmentOTPBinding.btnVerifyOtp.setOnClickListener( | |
view -> { | |
if (InternetConnection.checkConnection(requireContext())){ | |
if (!fragmentOTPBinding.pinView.getText().toString().isEmpty()){ | |
verifyCode(fragmentOTPBinding.pinView.getText().toString()); | |
}else { | |
UtilsKt.snackbar(requireView(),"Please enter your OTP"); | |
} | |
}else { | |
showInternetToast(); | |
} | |
} | |
); | |
return fragmentOTPBinding.getRoot(); | |
} | |
private void sentOTPcode() | |
{ | |
PhoneAuthProvider.getInstance().verifyPhoneNumber( | |
userPhoneNumber, | |
60, | |
TimeUnit.SECONDS, | |
requireActivity(), | |
new PhoneAuthProvider.OnVerificationStateChangedCallbacks() { | |
@Override | |
public void onCodeSent(@NonNull @NotNull String s, @NonNull @NotNull PhoneAuthProvider.ForceResendingToken forceResendingToken) { | |
otpid =s; | |
} | |
@Override | |
public void onVerificationCompleted(@NonNull @NotNull PhoneAuthCredential phoneAuthCredential) { | |
String code = phoneAuthCredential.getSmsCode(); | |
if(code != null){ | |
fragmentOTPBinding.pinView.setText(code); | |
verifyCode(code); | |
} | |
} | |
@Override | |
public void onVerificationFailed(@NonNull @NotNull FirebaseException e) { | |
Toast.makeText(requireContext(),e.getMessage(),Toast.LENGTH_LONG).show(); | |
} | |
} | |
); | |
} | |
private void verifyCode(String code) | |
{ | |
PhoneAuthCredential credential = PhoneAuthProvider.getCredential(otpid,code); | |
signInWithPhoneAuthCredential(credential); | |
} | |
private void signInWithPhoneAuthCredential(PhoneAuthCredential credential) { | |
mAuth.signInWithCredential(credential).addOnCompleteListener(new OnCompleteListener<AuthResult>() { | |
@Override | |
public void onComplete(@NonNull @NotNull Task<AuthResult> task) { | |
if (task.isSuccessful()) { | |
if(bundle != null){ | |
popUpStack(); | |
navController = Navigation.findNavController(requireActivity(), R.id.nav_host_Fragmnet); | |
navController.navigate(R.id.updatePasswordFragment, null); | |
} | |
else{ | |
signUp(); | |
} | |
} | |
else { | |
Toast.makeText(requireContext(),"Code Error",Toast.LENGTH_LONG).show(); | |
} | |
} | |
}); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment