Hi all
Based on this tutorial, I tried to extract only the verification code via SMS, the first part succeeds and sends a Google verification message, but the second part of the test verification code does not work, I got this error message (The sms code has expired):
this is my code:
Based on this tutorial, I tried to extract only the verification code via SMS, the first part succeeds and sends a Google verification message, but the second part of the test verification code does not work, I got this error message (The sms code has expired):
** Activity (main) Pause, UserClosed = true ** *** Service (starter) Create *** ** Service (starter) Start ** ** Activity (main) Create, isFirst = true ** ** Activity (main) Resume ** Method: phoneLogin Phone: --------- (My phone number) default sign in: com.google.firebase.auth.internal.zzp@420a920 onCodeSent: AOO2nWU4KeIkcUEnj9H5dECDXNJ828Y_E9x0EIFBlN_0_t4L5u3VUCCr5f_igrqlI2N5F4RhAmlVUyjAi0gQZeBW-xDn1f4QK6b4Fvd4nKKFgXUgo12e7eVybJwp2tujqDEIsUHKdjhnHy3bpng13okliW9htbu6vzkP2cwujKFT8mQjkou5R6IHHK1jdsZxaXo6EKoJmm-8JNcmLpndJkEA3jmu8WC7NCLnvl5WmM_XhL2WThfAMH1iO1Hspx6BgnrSg4iLWQ1H3E7HAePy2zYlkQ0XqpINpA onVerificationCompleted: end signInWithPhoneAuthCredential _phonesignedin: true AOO2nWU4KeIkcUEnj9H5dECDXNJ828Y_E9x0EIFBlN_0_t4L5u3VUCCr5f_igrqlI2N5F4RhAmlVUyjAi0gQZeBW-xDn1f4QK6b4Fvd4nKKFgXUgo12e7eVybJwp2tujqDEIsUHKdjhnHy3bpng13okliW9htbu6vzkP2cwujKFT8mQjkou5R6IHHK1jdsZxaXo6EKoJmm-8JNcmLpndJkEA3jmu8WC7NCLnvl5WmM_XhL2WThfAMH1iO1Hspx6BgnrSg4iLWQ1H3E7HAePy2zYlkQ0XqpINpA signInWithPhoneAuthCredential _phonesignedin: false com.google.android.gms.tasks.RuntimeExecutionException: com.google.firebase.auth.FirebaseAuthInvalidCredentialsException: The sms code has expired. Please re-send the verification code to try again. at com.google.android.gms.tasks.zzw.getResult(com.google.android.gmslay-services-tasks@@17.2.1:3) at anywheresoftware.b4a.FirebaseAuthPhone.FirebaseAuthPhoneWrapper$4.onComplete(FirebaseAuthPhoneWrapper.java:401) at com.google.android.gms.tasks.zzi.run(com.google.android.gmslay-services-tasks@@17.2.1:1) at android.os.Handler.handleCallback(Handler.java:739) at android.os.Handler.dispatchMessage(Handler.java:95) at android.os.Looper.loop(Looper.java:148) at android.app.ActivityThread.main(ActivityThread.java:7406) at java.lang.reflect.Method.invoke(Native Method) at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1230) at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1120) Caused by: com.google.firebase.auth.FirebaseAuthInvalidCredentialsException: The sms code has expired. Please re-send the verification code to try again. at com.google.firebase.auth.api.internal.zzej.zza(com.google.firebase:firebase-auth@@19.4.0:30) at com.google.firebase.auth.api.internal.zzfm.zza(com.google.firebase:firebase-auth@@19.4.0:21) at com.google.firebase.auth.api.internal.zzff.zza(com.google.firebase:firebase-auth@@19.4.0:33) at com.google.firebase.auth.api.internal.zzfh.zza(com.google.firebase:firebase-auth@@19.4.0:74) at com.google.firebase.auth.api.internal.zzep.zza(com.google.firebase:firebase-auth@@19.4.0:18) at com.google.android.gms.internal.firebase_auth.zza.onTransact(com.google.firebase:firebase-auth@@19.4.0:13) at android.os.Binder.execTransact(Binder.java:453) |
this is my code:
B4X:
#Region Project Attributes
#ApplicationLabel: B4A Example
#VersionCode: 1
#VersionName:
'SupportedOrientations possible values: unspecified, landscape or portrait.
#SupportedOrientations: unspecified
#CanInstallToExternalStorage: False
#End Region
#Region Activity Attributes
#FullScreen: False
#IncludeTitle: True
#End Region
#AdditionalJar: com.google.android.gms:play-services-base
#MultiDex: True
Sub Process_Globals
'These global variables will be declared once when the application starts.
'These variables can be accessed from all modules.
Private xui As XUI
Dim gs_verificationID, gs_phoneNumber As String
Private authphone As FirebaseAuthPhoneWrapper
Dim gb_phoneVerificationSuccess As Boolean
Private auth As FirebaseAuth
End Sub
Sub Globals
'These global variables will be redeclared each time the activity is created.
Private fet_setphonenumber As EditText
Private setPhoneNumber As Button
Private rp As RuntimePermissions
Dim gs_signInMethod As String
Dim ime As IME
Private EditText1 As EditText
End Sub
Sub Activity_Create(FirstTime As Boolean)
Activity.LoadLayout("er")
Try
If FirstTime Then
ime.Initialize("e_ime")
gb_phoneVerificationSuccess = False
If CheckForGooglePlayServices = True Then
ToastMessageShow("Google play services active!",True)
End If
rp.CheckAndRequest(rp.PERMISSION_READ_PHONE_STATE)
Wait For Activity_PermissionResult (Permission As String, Result As Boolean)
If Result = False Then
MsgboxAsync("Phone access permission not allowed!", "Alert...")
ExitApplication
End If
read_signInMethod ' get preferred method from firebase.ini
End If
authphone.Initialize("authphone")
Dim ls_phoneNumber As String = getPhoneNumber
If ls_phoneNumber <> "" Then gs_phoneNumber = ls_phoneNumber
Catch
Log(LastException)
End Try
End Sub
Sub Activity_Resume
End Sub
Sub Activity_Pause (UserClosed As Boolean)
End Sub
Sub authphone_oncodesent(success As Boolean, verificationid As String)
'--------------------------------
Log("q1")
If success = True And gb_phoneVerificationSuccess = False Then
gs_verificationID = verificationid
Log("q2: " & gs_verificationID)
End If
'''''Log("authphone_oncodesent success" & success)
End Sub
Sub authphone_phonesignedin(success As Boolean, User As FirebaseAuthPhoneUser, info As String)
'--------------------------------
If success = True Then
authphone.currentToken()
gs_signInMethod = "phoneLogin"
End If
End Sub
Private Sub setPhoneNumber_Click
Dim ls_phoneNr As String = fet_setphonenumber.Text.Trim
If ls_phoneNr.StartsWith("+") = False Then
MsgboxAsync("Please digit complete Phone number using +xx prefix!", "Alert...")
Return
else If fet_setphonenumber.Text.Length <= 9 Then
MsgboxAsync("Please digit complete Phone number using +xx prefix!", "Alert...")
Return
Else
gs_phoneNumber = fet_setphonenumber.text
authphone.startPhoneNumberVerification(gs_phoneNumber)
save_signInMethod
End If
End Sub
Sub save_signInMethod
'-----------------------
Dim MapConfig As Map
MapConfig.Initialize
MapConfig.Clear
MapConfig.Put("authSignInMethod", gs_signInMethod)
If gs_phoneNumber <> "" Then
MapConfig.Put("authPhoneNumber", gs_phoneNumber)
End If
File.WriteMap(File.DirInternal, "firebase.ini", MapConfig)
End Sub
Sub getPhoneNumber As String
'--------------------------------
Dim pID As PhoneId
Dim phonenumber As String
phonenumber = pID.GetLine1Number
Return phonenumber
End Sub
Sub CheckForGooglePlayServices As Boolean
'---------------------------------------------
Try
Dim GoogleApiAvailablity As JavaObject
GoogleApiAvailablity = GoogleApiAvailablity.InitializeStatic("com.google.android.gms.common.GoogleApiAvailability").RunMethod("getInstance", Null)
Dim context As JavaObject
context.InitializeContext
Dim li_googlePlayServices As Int = GoogleApiAvailablity.RunMethod("isGooglePlayServicesAvailable", Array(context))
If li_googlePlayServices <> 0 Then
GoogleApiAvailablity.RunMethod("makeGooglePlayServicesAvailable", Array(context))
Return False
End If
Return True
Catch
Log(LastException)
End Try
End Sub
Sub read_signInMethod
'-----------------------
Dim ls_tempKey As String
Dim MapConfig As Map
MapConfig.Initialize
If File.Exists(File.DirInternal,"firebase.ini") = False Then
File.WriteMap(File.DirInternal, "firebase.ini", MapConfig)
End If
MapConfig.Clear
MapConfig = File.ReadMap(File.DirInternal,"firebase.ini")
ls_tempKey = ""
For i = 0 To MapConfig.Size - 1
ls_tempKey = MapConfig.GetKeyAt(i)
Select ls_tempKey
Case "authSignInMethod"
gs_signInMethod = MapConfig.GetValueAt(i)
Case "authPhoneNumber"
gs_phoneNumber = MapConfig.GetValueAt(i)
End Select
Next
Log ("Method: " & gs_signInMethod & CRLF & " Phone: " & gs_phoneNumber)
End Sub
Sub Auth_SignedIn(User As FirebaseUser)
'---------------------------------------------
If User.DisplayName <> Null Then
Log("google SignedIn: " & User.email)
Log("UID: " & User.Uid)
Log("User name: " & User.DisplayName)
ToastMessageShow("Welcome: " & User.email , True)
gs_signInMethod = "googleLogin"
save_signInMethod
''''' Activity.RemoveAllViews
''''' Activity.LoadLayout("main2")
''''' labelLogInMethod.Text = "Logged in with Google"
''''' LabelUser.Text = "user:" &User.DisplayName & CRLF & "email: " & User.Email &CRLF & "UID/Token" & CRLF & User.Uid
End If
End Sub
Private Sub Button1_Click
Try
Log(gs_verificationID)
authphone.verifyPhoneNumberWithCode(gs_verificationID, EditText1.text)
Catch
Log(LastException)
End Try
End Sub
Sub gb_phoneverification(success As Boolean, info As String)
Log(success)
End Sub
Sub authphone_phoneverification(success As Boolean, info As String)
'--------------------------------
If success = True Then
ToastMessageShow("Phone number successfully verified!",True)
gb_phoneVerificationSuccess = True
Else
MsgboxAsync("Phone number verification failed!"& CRLF & "info: "& info, "Alert...")
gs_phoneNumber = ""
gs_signInMethod = ""
save_signInMethod
End If
End Sub
Firebase phone number authentication
Here is my second lib, a wrap to integrate firebase auth phone number authentication. It is working nicely on my app. for reference visit: Authenticate with Firebase on Android using a Phone Number FirebaseAuthPhone Version: 1.9 Events: phoneverification (success As Boolean, info As...
www.b4x.com