Android Question firebase authentication problem !

Discussion in 'Android Questions' started by Waldemar Lima, Apr 16, 2019.

  1. Waldemar Lima

    Waldemar Lima Active 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 :
    Code:
    #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 = TrueThen
            Startup = 
    True
            Starter.Startup = 
    True
            
            
    Activity.LoadLayout("splash")
            
            
    Do While (Starter.ServerIsConnected = False)
                
                
    Log(timerM)
                
                Sleep(
    1000)
                timerM = timerM  + 
    1
                
                
    If (timerM = 15Then
                    
    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 = FalseThen
                    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 = 15Then
                    
    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 = FalseThen
                    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 = 15Then
                
    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 = TrueThen
                Sleep(
    5000)
            
    End If
            auth_SignedIn(Starter.auth.CurrentUser)
        
    Else
            LogColor(
    "Starter = "&Starter.Startup ,Colors.Green)
            
            
    If (Starter.RequestedAuth = TrueThen
                LogColor(
    "AuthID = "&Starter.auth.CurrentUser,Colors.Blue)
            
    End If
            
            
    If (Starter.RequestedAuth = FalseThen
                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?
     
  2. DonManfred

    DonManfred Expert Licensed User

    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.

    [​IMG]

    NOTE that firebase auth is part of the ACTIVITY where i want to use it, not the starter service!!
     
    Erel likes this.
  3. Waldemar Lima

    Waldemar Lima Active Member Licensed User

    Thanks by you help @DonManfred
    But i have a ask : If i set Firebase auth part of current activity , can i ' logout ' from Google initializing other 'auth' in other activity ?
     
  4. Erel

    Erel Administrator Staff 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.
     
  5. Waldemar Lima

    Waldemar Lima Active Member Licensed User

    but I'm destroying the activity that contains the FirebaseAuth object ... even so I can access?
     
  6. DonManfred

    DonManfred Expert Licensed User

    When Erel is saying do so. Process global variables are available as long as the process is not killed.

    WHY you are not TRYING IT first?
     
    Last edited: Apr 17, 2019
Loading...
  1. This site uses cookies to help personalise content, tailor your experience and to keep you logged in if you register.
    By continuing to use this site, you are consenting to our use of cookies.
    Dismiss Notice