Android Question firebase authentication problem !

Waldemar Lima

Well-Known Member
Licensed User
hello everyone !

I am using the firebase for user authentication of my application, however, in order for the firebase to return the information to the sub: Sub auth_SignedIn (User As FirebaseUser), it is necessary to call the function: Starter.auth.SignInWithGoogle several times until the function: Sub auth_SignedIn (User As FirebaseUser), is accessed ...


this is the activity code in question :
B4X:
#Region  Project Attributes
    #ApplicationLabel: MyApp
    #VersionCode: 1
    #VersionName:
    'SupportedOrientations possible values: unspecified, landscape or portrait.
    #SupportedOrientations: portrait
    #CanInstallToExternalStorage: True

#End Region

#Region  Activity Attributes
    #FullScreen: True   
    #IncludeTitle: False
#End Region
#BridgeLogger: true


Sub Process_Globals
    Public ApplicationVersion As Int = 1
    Public ServerUrl As String = "192.168.0.1:51063"
    Public UserEmail As String = ""
    Public UserName As String = ""
    Private timerM As Int = 0
    Public Startup As Boolean = True
    'Private auth As FirebaseAuth
    Private RequestsNum As Int = 0
End Sub

Sub Globals

    Private auth As FirebaseAuth
    
End Sub

Sub Activity_Create(FirstTime As Boolean)
    'Do not forget to load the layout file created with the visual designer. For example:
    'Startup = FirstTime
    UserEmail = ""
    UserName = ""
    RequestsNum = 0
    Startup = Starter.Startup
'    auth.Initialize("auth")
    
    If (Startup = True) Then
        Startup = True
        Starter.Startup = True
        
        Activity.LoadLayout("splash")
        
        Do While (Starter.ServerIsConnected = False)
            
            Log(timerM)
            
            Sleep(1000)
            timerM = timerM  + 1
            
            If (timerM = 15) Then
                Msgbox("Não foi possível se conectar ao servidor !","Houve um problema ...")
                ExitApplication
            End If
            
        Loop
        
        If Starter.auth.CurrentUser.IsInitialized Then
            Log("FirstTime")
            Sleep(5000)
            auth_SignedIn(Starter.auth.CurrentUser)
        Else   
            LogColor("Starter = "&Starter.Startup ,Colors.Green)
            If (Starter.RequestedAuth = False) Then
                Starter.auth.SignInWithGoogle
            Else
                LogColor("Tentativa de Request",Colors.Red)
            End If
        End If
        
    Else
        Startup = False
        Starter.Startup = False
        Do While (Starter.ServerIsConnected = False)
            
            Log(timerM)
            
            Sleep(1000)
            timerM = timerM  + 1
            
            If (timerM = 15) Then
                Msgbox("Não foi possível se conectar ao servidor !","Houve um problema ...")
                ExitApplication
            End If
            
        Loop
        
        If Starter.auth.CurrentUser.IsInitialized Then
            Log("NoFirstTime")
            'Sleep(5000)
            auth_SignedIn(Starter.auth.CurrentUser)
        Else
            LogColor("Starter = "&Starter.Startup ,Colors.Green)
            If (Starter.RequestedAuth = False) Then
                Starter.auth.SignInWithGoogle
            Else
                LogColor("Tentativa de Request",Colors.Red)
            End If
        End If
        
    End If   
    
    
End Sub

Sub auth_TokenAvailable (User As FirebaseUser, Success As Boolean, TokenId As String)
    
    LogColor("returnFB = "&Success,Colors.Blue)
    Log("tokenID = "&TokenId)
    
    
End Sub

Sub auth_SignedIn (User As FirebaseUser)

    Starter.RequestedAuth = True
    Log("SignedIn: " & User.DisplayName)
    Log(User.Email)
    Log(User.PhotoUrl)
    
    UserEmail = User.Email
    UserName = User.DisplayName
    Starter.LoggedMail = UserEmail
    Starter.LoggedNome = UserName
    Starter.LoggedPhoto = User.PhotoUrl
    
    
    
End Sub


Sub Activity_Resume
    For Each v As View In Activity.GetAllViewsRecursive
        If v.Tag Is B4XLoadingIndicator And v.Visible = True Then
            Dim x As B4XLoadingIndicator = v.Tag
            x.Show
        End If
    Next
    Log("Resumed run")
    
    Do While (Starter.ServerIsConnected = False)
            
        Log(timerM)
            
        Sleep(1000)
        timerM = timerM  + 1
            
        If (timerM = 15) Then
            Msgbox("Não foi possível se conectar ao servidor !","Houve um problema ...")
            ExitApplication
        End If
            
    Loop
    
    If Starter.auth.CurrentUser.IsInitialized Then
        Log("entrou aqui porra")
        If (Startup = True) Then
            Sleep(5000)
        End If
        auth_SignedIn(Starter.auth.CurrentUser)
    Else
        LogColor("Starter = "&Starter.Startup ,Colors.Green)
        
        If (Starter.RequestedAuth = True) Then
            LogColor("AuthID = "&Starter.auth.CurrentUser,Colors.Blue)
        End If
        
        If (Starter.RequestedAuth = False) Then
            Starter.auth.SignInWithGoogle
        Else
            LogColor("Tentativa de Request",Colors.Red)
        End If
    End If
    
    If (UserEmail <> "") Then
        Log("logado !")
        
        Dim data As Map
        data.Initialize
        data.Put("message", UserEmail)
        Starter.wsh.SendEventToServer("Device_UserAuthenticate", data)
        data.Clear
        
    End If
    
End Sub

Sub ToRegistro()
    
    StartActivity(Registro)
    SetAnimation("right_left1","right_left")
    Activity.Finish
    
End Sub

Sub ToApp()
    
    StartActivity(App)
    SetAnimation("right_left1","right_left")
    Activity.Finish
    
End Sub

Sub Blocked()
    
    Msgbox("Esta conta está permanentemente bloqueada !","Encontramos um problema !")
    ExitApplication
    
End Sub

Sub Error0()
    
    Msgbox("Houve um erro interno , não foi possível concluir a autenticação !","Erro na autenticação")
    ExitApplication
    
End Sub

Sub Activity_Pause (UserClosed As Boolean)

End Sub

Sub SetAnimation(InAnimation As String, OutAnimation As String)
    Dim r As Reflector
    Dim package As String
    Dim In, out As Int
    package = r.GetStaticField("anywheresoftware.b4a.BA", "packageName")
    In = r.GetStaticField(package & ".R$anim", InAnimation)
    out = r.GetStaticField(package & ".R$anim", OutAnimation)
    r.Target = r.GetActivity
    r.RunMethod4("overridePendingTransition", Array As Object(In, out), Array As String("java.lang.int", "java.lang.int"))
End Sub

this is the debug screenshots :

*(this is the screenshot of the numerous requisitions before entering the function: ' Sub auth_SignedIn (User As FirebaseUser) '

Screenshot_1.png

*(this is the screenshot of when I add a 'Limiter' so that the function: ' Starter.auth.SignInWithGoogle ', be called only once ) the application stays stuck forever ...
Screenshot_2.png
the firebase never enters the function: Sub auth_SignedIn (User As FirebaseUser), so that the information of this due user is stored .


Does anyone have a solution to do away with the many requests?
 

DonManfred

Expert
Licensed User
Longtime User
it is necessary to call the function: Starter.auth.SignInWithGoogle several times
Not for me. I click ONE time and then i get the event and a toast telling me that i am logged

I create a new project using the FirebaseAuth example. Gave the app a new packagename, added a new app to my firebase project, downloaded the json and use it in the example code.

*** Service (starter) Create ***
** Service (starter) Start **
** Activity (main) Create, isFirst = true **
** Activity (main) Resume **
*** Service (firebasemessaging) Create ***
** Service (firebasemessaging) Start **
onAuthStateChanged: com.google.firebase.auth.internal.zzi@e262b14
** Activity (main) Pause, UserClosed = false **
** Activity (main) Resume **
SignInWithGoogle called
** Activity (main) Pause, UserClosed = false **
sending message to waiting queue (OnActivityResult)
running waiting messages (1)
SignInWithGoogle.ResultArrived
ResultArrived Success
** Activity (main) Resume **
onAuthStateChanged: com.google.firebase.auth.internal.zzi@e262b14
SignedIn: Manfred Ssykor
firebaseAuthWithGoogle success: true
result: com.google.firebase.auth.internal.zze@d945241

fbauth033.png


NOTE that firebase auth is part of the ACTIVITY where i want to use it, not the starter service!!
 
Upvote 0

Waldemar Lima

Well-Known Member
Licensed User
FirebaseAuth should not be a global object. It should be a process global object.

This will also allow you to access it from other modules.

but I'm destroying the activity that contains the FirebaseAuth object ... even so I can access?
 
Upvote 0
Top