没有从Firebase电话身份验证获得短信OTP代码

时间:2020-06-25 20:30:42

标签: android firebase firebase-authentication one-time-password

请帮助我,我尝试使用带Firebase的电话身份验证的应用程序。我已激活电话登录方法firebase

这是我的应用gradle

apply plugin: 'com.android.application'
apply plugin: 'kotlin-android'
apply plugin: 'kotlin-android-extensions'
apply plugin: 'com.google.gms.google-services'

android {
    compileSdkVersion 29
    buildToolsVersion "29.0.3"

    defaultConfig {
        applicationId "com.lumbung_inovasi.policehealthcare"
        minSdkVersion 22
        targetSdkVersion 29
        versionCode 1
        versionName "1.0"
        buildConfigField("String", "BASE_API", '"http://ludes.in:5001/"')
        testInstrumentationRunner "androidx.test.runner.AndroidJUnitRunner"
    }

    buildTypes {
        release {
            minifyEnabled false
            proguardFiles getDefaultProguardFile('proguard-android-optimize.txt'), 'proguard-rules.pro'
        }
    }

// To inline the bytecode built with JVM target 1.8 into
// bytecode that is being built with JVM target 1.6. (e.g. navArgs)


    compileOptions {
        sourceCompatibility JavaVersion.VERSION_1_8
        targetCompatibility JavaVersion.VERSION_1_8
    }
    kotlinOptions {
        jvmTarget = "1.8"
    }

}

dependencies {
    implementation fileTree(dir: 'libs', include: ['*.jar'])
    implementation "org.jetbrains.kotlin:kotlin-stdlib-jdk7:$kotlin_version"
    implementation 'androidx.appcompat:appcompat:1.1.0'
    implementation 'androidx.core:core-ktx:1.3.0'
    implementation 'com.google.android.material:material:1.1.0'
    implementation 'androidx.constraintlayout:constraintlayout:1.1.3'
    implementation "androidx.fragment:fragment:1.2.5"
    implementation 'androidx.navigation:navigation-fragment:2.2.2'
    implementation 'androidx.navigation:navigation-ui:2.2.2'
    implementation 'androidx.lifecycle:lifecycle-extensions:2.2.0'
    implementation 'androidx.navigation:navigation-fragment-ktx:2.2.2'
    implementation 'androidx.navigation:navigation-ui-ktx:2.2.2'
    implementation 'androidx.legacy:legacy-support-v4:1.0.0'
    implementation 'com.google.firebase:firebase-core:17.4.3'
    implementation 'com.google.firebase:firebase-auth:19.3.1'
    testImplementation 'junit:junit:4.13'
    androidTestImplementation 'androidx.test.ext:junit:1.1.1'
    androidTestImplementation 'androidx.test.espresso:espresso-core:3.2.0'
    implementation 'de.hdodenhof:circleimageview:2.1.0'
    implementation 'com.tbuonomo.andrui:viewpagerdotsindicator:4.1.2'
    implementation 'com.github.bumptech.glide:glide:4.11.0'
    annotationProcessor 'com.github.bumptech.glide:compiler:4.11.0'
    implementation 'com.github.alahammad:android-OTP-View:1.0.2'
    // Retrofit & OkHttp
    implementation 'com.squareup.retrofit2:retrofit:2.6.1'
    implementation 'com.squareup.retrofit2:converter-gson:2.6.1'
    implementation('com.facebook.stetho:stetho-okhttp3:1.5.1') {
        exclude group: 'com.facebook.stetho'
    }
}

我试图用片段创建OTP布局,并且我使用sms catch库来读取带有OTP代码的新消息。这是我的OTP片段代码

class OtpFragment : Fragment(), OTPListener, OnSmsCatchListener<String> {
    private lateinit var smsCatcher: SmsVerifyCatcher
    private lateinit var phoneAuth: FirebaseAuth
    private var mResendToken: ForceResendingToken? = null
    private var mVerificationId: String = ""

    override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
        super.onViewCreated(view, savedInstanceState)
        otp.setOnOtpFinished(this)
        phoneAuth = FirebaseAuth.getInstance()
        smsCatcher = SmsVerifyCatcher(activity, this)
        val phoneNumber = "+62 85157233868"
        requestOTP(phoneNumber)
        resend_button.setOnClickListener {
            resendOTPCode(phoneNumber)
        }
    }

    private fun requestOTP(phoneNumber: String){
        PhoneAuthProvider.getInstance().verifyPhoneNumber(phoneNumber, 60, TimeUnit.SECONDS, this.requireActivity(), callbacks())
    }

    private fun callbacks(): OnVerificationStateChangedCallbacks {
        return object : OnVerificationStateChangedCallbacks() {
            override fun onVerificationCompleted(phoneAuthCredential: PhoneAuthCredential) {
                Log.e("onVerificationCompleted", "${phoneAuthCredential.smsCode}")
                phoneAuth.signInWithCredential(phoneAuthCredential).addOnCompleteListener {
                    if (it.isSuccessful){
                        Log.e("sign-in", "berhasil")
                    } else{
                        Log.e("sign-in", "${it.result}")
                    }
                }
            }

            override fun onVerificationFailed(e: FirebaseException) {
                if (e is FirebaseAuthInvalidCredentialsException) {
                    Log.e("invalidCredential", e.toString())
                } else if (e is FirebaseTooManyRequestsException) {
                    Log.e("out of quota", e.toString())
                }
            }

            override fun onCodeAutoRetrievalTimeOut(s: String) {
                super.onCodeAutoRetrievalTimeOut(s)
                Log.e("", s)
            }

            override fun onCodeSent(
                verificationId: String,
                forceResendingToken: ForceResendingToken
            ) {
                super.onCodeSent(verificationId, forceResendingToken)
                Log.e("code-sent", "onCodeSent:$verificationId")
                mVerificationId = verificationId
                mResendToken = forceResendingToken
            }
        }
    }

    private fun signInWithCredential(credential: PhoneAuthCredential){
        phoneAuth.signInWithCredential(credential)
            .addOnCompleteListener {
                val intent = Intent(activity, MainActivity::class.java)
                startActivity(intent)
                activity?.overridePendingTransition(0,0)
            }
            .addOnFailureListener {
                Log.e("login-fail", "${it.message}")
            }
    }

    private fun resendOTPCode(phoneNumber: String){
        if(mResendToken != null) {
            PhoneAuthProvider.getInstance().verifyPhoneNumber(
                phoneNumber,
                120,
                TimeUnit.SECONDS,
                this.requireActivity(),
                callbacks(),
                mResendToken
            )
            Toast.makeText(activity, "code otp dikirim ulang", Toast.LENGTH_SHORT).show()
        }
    }

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
    }

    override fun onCreateView(
        inflater: LayoutInflater, container: ViewGroup?,
        savedInstanceState: Bundle?
    ): View? {
        return inflater.inflate(R.layout.fragment_otp, container, false)
    }

    override fun otpFinished(p0: String?) {
        Toast.makeText(activity, "otp finish $p0", Toast.LENGTH_SHORT).show()
        p0?.let {
            val credential = PhoneAuthProvider.getCredential(mVerificationId, it)
            signInWithCredential(credential)
        }
    }

    override fun onSmsCatch(p0: String?) {
        Toast.makeText(activity, "sms catch $p0", Toast.LENGTH_SHORT).show()
    }

}

有关信息,我将小米redmi note 7与互联网连接使用,并且sim卡仍处于活动状态

0 个答案:

没有答案