1

I am trying to implement a firebase auth method using phone number but when I click the button to receive my sms code, the progress bar just loads and I don t get any sms. Nor does any account get created on the Firebase Console since I can't register my account if I don't enter my verification code.

RegisterLoginActivity

package com.example.madistrezsieu

import android.content.Intent

import android.os.Bundle
import android.view.View
import android.widget.Toast
import androidx.appcompat.app.AppCompatActivity
import com.google.android.gms.tasks.Task
import com.google.firebase.FirebaseException
import com.google.firebase.auth.AuthResult
import com.google.firebase.auth.FirebaseAuth
import com.google.firebase.auth.PhoneAuthCredential
import com.google.firebase.auth.PhoneAuthProvider
import kotlinx.android.synthetic.main.activity_register_login.*
import java.util.concurrent.TimeUnit


class RegisterLoginActivity : AppCompatActivity() {

    lateinit var mCallbacks: PhoneAuthProvider.OnVerificationStateChangedCallbacks
    lateinit var mAuth: FirebaseAuth
    var verificationId = ""


    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_register_login)
        mAuth = FirebaseAuth.getInstance()
        veriBtn.setOnClickListener {
                view: View? -> progress.visibility = View.VISIBLE
            verify ()
        }
        authBtn.setOnClickListener {
                view: View? -> progress.visibility = View.VISIBLE
            authenticate()
        }
    }


    private fun verificationCallbacks () {
        mCallbacks = object: PhoneAuthProvider.OnVerificationStateChangedCallbacks() {
            override fun onVerificationCompleted(credential: PhoneAuthCredential) {
                progress.visibility = View.INVISIBLE
                signIn(credential)
            }

            override fun onVerificationFailed(p0: FirebaseException?) {
                //TODO("not implemented") //To change body of created functions use File | Settings | File Templates.
            }

            override fun onCodeSent(verfication: String?, p1: PhoneAuthProvider.ForceResendingToken?) {
                super.onCodeSent(verfication, p1)
                verificationId = verfication.toString()
                progress.visibility = View.INVISIBLE
            }

        }
    }

    private fun verify () {

        verificationCallbacks()

        val phnNo = phnNoTxt.text.toString()

        PhoneAuthProvider.getInstance().verifyPhoneNumber(
            phnNo,
            60,
            TimeUnit.SECONDS,
            this,
            mCallbacks
        )
    }

    private fun signIn (credential: PhoneAuthCredential) {
        mAuth.signInWithCredential(credential)
            .addOnCompleteListener {
                    task: Task<AuthResult> ->
                if (task.isSuccessful) {
                    toast("Logged in Successfully :)")
                    startActivity(Intent(this, LatestMessagesActivity::class.java))
                }
            }
    }

    private fun authenticate () {

        val verifiNo = verifiTxt.text.toString()

        val credential: PhoneAuthCredential = PhoneAuthProvider.getCredential(verificationId, verifiNo)

        signIn(credential)

    }

    private fun toast (msg: String) {
        Toast.makeText(this, msg, Toast.LENGTH_LONG).show()
    }


}

activity_register_login.xml

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
                xmlns:tools="http://schemas.android.com/tools"
                android:layout_width="match_parent"
                android:layout_height="match_parent"
                tools:context="com.example.madistrezsieu.RegisterLoginActivity">

    <EditText
            android:id="@+id/phnNoTxt"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_alignParentTop="true"
            android:layout_centerHorizontal="true"
            android:layout_marginTop="88dp"
            android:hint="Phone Number"
            android:inputType="number"
            android:textAlignment="center"
            android:textSize="24sp" />

    <EditText
            android:id="@+id/verifiTxt"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_alignParentStart="true"
            android:textAlignment="center"
            android:inputType="number"
            android:layout_below="@+id/phnNoTxt"
            android:layout_marginTop="43dp"
            android:hint="Verification Code" android:layout_alignParentLeft="true"/>

    <Button
            android:id="@+id/veriBtn"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_below="@+id/verifiTxt"
            android:layout_centerHorizontal="true"
            android:layout_marginTop="35dp"
            android:text="Verify" />

    <Button
            android:id="@+id/authBtn"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_below="@+id/veriBtn"
            android:layout_centerHorizontal="true"
            android:layout_marginTop="26dp"
            android:text="Authenticate" />

    <ProgressBar
            android:id="@+id/progress"
            style="?android:attr/progressBarStyle"
            android:layout_width="150dp"
            android:layout_height="150dp"
            android:layout_alignTop="@+id/phnNoTxt"
            android:layout_centerHorizontal="true"
            android:layout_marginTop="28dp"
            android:visibility="invisible" />
</RelativeLayout>
Doug Stevenson
  • 297,357
  • 32
  • 422
  • 441

1 Answers1

0
  1. Make sure you enabled phone authentication on firebase console,
  2. Make sure your project on android studio connected to firebase because phone authentication need SHA-1 certificate to send sms. Your android studio will handle SHA-1 automatically.
  3. sometimes there is a problem when connecting firebase to android project through android studio, in this case you should create manually your own SHA-1 for your project. follow this link to do it SHA-1 fingerprint of keystore certificate
flx_h
  • 101
  • 3