B4J Question FirebaseServer Question

yiankos1

Active Member
Licensed User
Hello,
I try to implement Firebase Server backend auth. I have a jRDC server up and running correctly. I want to add this verification too. I followed all steps as @Erel mentions. Can i add tutorial's code at seperate server handler in order to call this verification before every post/get call, or i have to add it at main module of jRDC. If i have to add this app start(server start) how it can verify tokens from all users? I think that i am really comfused :confused:

My code:
B4X:
'Handler class
Sub Class_Globals
    Private fs As FirebaseServer
End Sub

Public Sub Initialize
    fs.Initialize("fs", File.OpenInput(File.DirAssets, "se-eida-*****.json"))
End Sub

Sub Handle(req As ServletRequest, resp As ServletResponse)
  
    If req.Method <> "POST" Then
        resp.SendError(500, "Method not supported!")
        Return
    End If
  
    Dim reqName As String = req.GetParameter("token")
  
    If reqName = "" Then
        resp.SendError(500,"Missing parameter!")
        Return
    End If
      
       fs.VerifyToken(reqName)
  
    Wait For fs_TokenVerified (TokenId As String, Success As Boolean, Token As FirebaseToken)
    If Success Then
        resp.Write(Token.DisplayName)
        resp.Write(Token.Email)
        resp.Write(Token.Uid)
    Else
        resp.SendError(500,LastException.Message)
    End If
End Sub
If i try to reach this verify handler with Postman with a random token(not from firebase, just random numbers), first time i get nothing as response and if i try to send this call again i get this as response:
B4X:
<html>
    <head>
        <meta http-equiv="Content-Type" content="text/html;charset=utf-8"/>
        <title>Error 500 java.lang.reflect.InvocationTargetException</title>
    </head>
    <body>
        <h2>HTTP ERROR 500</h2>
        <p>Problem accessing /verify. Reason:

            <pre>    java.lang.reflect.InvocationTargetException</pre>
        </p>
        <hr>
        <a href="http://eclipse.org/jetty">Powered by Jetty:// 9.4.z-SNAPSHOT</a>
        <hr/>
    </body>
</html>
In jRDC log file i find this:
B4X:
java.lang.reflect.InvocationTargetException
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
    at java.lang.reflect.Method.invoke(Method.java:497)
    at anywheresoftware.b4j.object.JServlet.createInstance(JServlet.java:62)
    at anywheresoftware.b4j.object.JServlet$Handle.run(JServlet.java:128)
    at anywheresoftware.b4j.object.JServlet.Handle(JServlet.java:110)
    at anywheresoftware.b4j.object.JServlet.doPost(JServlet.java:80)
    at javax.servlet.http.HttpServlet.service(HttpServlet.java:707)
    at javax.servlet.http.HttpServlet.service(HttpServlet.java:790)
    at org.eclipse.jetty.servlet.ServletHolder.handle(ServletHolder.java:865)
    at org.eclipse.jetty.servlet.ServletHandler.doHandle(ServletHandler.java:535)
    at org.eclipse.jetty.server.handler.ScopedHandler.nextHandle(ScopedHandler.java:255)
    at org.eclipse.jetty.server.session.SessionHandler.doHandle(SessionHandler.java:1595)
    at org.eclipse.jetty.server.handler.ScopedHandler.nextHandle(ScopedHandler.java:255)
    at org.eclipse.jetty.server.handler.ContextHandler.doHandle(ContextHandler.java:1253)
    at org.eclipse.jetty.server.handler.ScopedHandler.nextScope(ScopedHandler.java:203)
    at org.eclipse.jetty.servlet.ServletHandler.doScope(ServletHandler.java:473)
    at org.eclipse.jetty.server.session.SessionHandler.doScope(SessionHandler.java:1564)
    at org.eclipse.jetty.server.handler.ScopedHandler.nextScope(ScopedHandler.java:201)
    at org.eclipse.jetty.server.handler.ContextHandler.doScope(ContextHandler.java:1155)
    at org.eclipse.jetty.server.handler.ScopedHandler.handle(ScopedHandler.java:144)
    at org.eclipse.jetty.server.handler.HandlerCollection.handle(HandlerCollection.java:126)
    at org.eclipse.jetty.server.handler.HandlerWrapper.handle(HandlerWrapper.java:132)
    at org.eclipse.jetty.server.Server.handle(Server.java:531)
    at org.eclipse.jetty.server.HttpChannel.handle(HttpChannel.java:352)
    at org.eclipse.jetty.server.HttpConnection.onFillable(HttpConnection.java:260)
    at org.eclipse.jetty.io.AbstractConnection$ReadCallback.succeeded(AbstractConnection.java:281)
    at org.eclipse.jetty.io.FillInterest.fillable(FillInterest.java:102)
    at org.eclipse.jetty.io.ChannelEndPoint$2.run(ChannelEndPoint.java:118)
    at org.eclipse.jetty.util.thread.strategy.EatWhatYouKill.runTask(EatWhatYouKill.java:333)
    at org.eclipse.jetty.util.thread.strategy.EatWhatYouKill.doProduce(EatWhatYouKill.java:310)
    at org.eclipse.jetty.util.thread.strategy.EatWhatYouKill.tryProduce(EatWhatYouKill.java:168)
    at org.eclipse.jetty.util.thread.strategy.EatWhatYouKill.run(EatWhatYouKill.java:126)
    at org.eclipse.jetty.util.thread.ReservedThreadExecutor$ReservedThread.run(ReservedThreadExecutor.java:366)
    at org.eclipse.jetty.util.thread.QueuedThreadPool.runJob(QueuedThreadPool.java:760)
    at org.eclipse.jetty.util.thread.QueuedThreadPool$2.run(QueuedThreadPool.java:678)
    at java.lang.Thread.run(Thread.java:745)
Caused by: java.lang.IllegalStateException: FirebaseApp name [DEFAULT] already exists!
    at com.google.firebase.internal.Preconditions.checkState(Preconditions.java:173)
    at com.google.firebase.FirebaseApp.initializeApp(FirebaseApp.java:180)
    at com.google.firebase.FirebaseApp.initializeApp(FirebaseApp.java:160)
    at anywheresoftware.b4j.objects.FirebaseServer.Initialize(FirebaseServer.java:29)
    at b4j.example.verify._initialize(verify.java:173)
    ... 38 more
 
Last edited:

yiankos1

Active Member
Licensed User
UPDATE:
As i read this i initiliazed at main module and above error disappeard. Now my code is like this:
Main module:
B4X:
'Non-UI application (console / server application)
#Region  Project Attributes
    #CommandLineArgs:
    #MergeLibraries: True
#End Region

'change based on the jdbc jar file
#AdditionalJar: mariadb-java-client-2.2.1
#AdditionalJar: firebase-server-sdk.jar
'#AdditionalJar: postgresql-9.4.1207

Sub Process_Globals
    Public srvr As Server
    Public rdcConnector1 As RDCConnector
    Public const VERSION As Float = 2.1
    Type DBCommand (Name As String, Parameters() As Object)
    Type DBResult (Tag As Object, Columns As Map, Rows As List)
    Public filesFolder As String = "uploaded"
    Public fs As FirebaseServer
End Sub

Sub AppStart (Args() As String)
    fs.Initialize("fs", File.OpenInput(File.DirAssets, "se-***********.json"))
   
    srvr.Initialize("")
    rdcConnector1.Initialize
    srvr.Port = rdcConnector1.serverPort
    srvr.AddHandler("/test", "TestHandler", False)
    srvr.AddHandler("/rdc", "RDCHandler", False)
    srvr.AddHandler("/upload", "Upload", False)
    srvr.AddHandler("/download", "Download", False)
    srvr.AddHandler("/verify", "Verify", False)
    'ConfigureSSL(443)
    srvr.Start
   
    Log($"jRDC is running (version = $1.2{VERSION})"$)
    StartMessageLoop
End Sub
And verify handler:
B4X:
'Handler class
Sub Class_Globals
   
End Sub

Public Sub Initialize
   
End Sub

Sub Handle(req As ServletRequest, resp As ServletResponse)
   
    If req.Method <> "POST" Then
        resp.SendError(500, "Method not supported!")
        Return
    End If
   
    Dim reqName As String = req.GetParameter("token")
   
    If reqName = "" Then
        resp.SendError(500,"Missing parameter!")
        Return
    End If
       
       Main.fs.VerifyToken(reqName)

    Wait For fs_TokenVerified (TokenId As String, Success As Boolean, Token As FirebaseToken)
    If Success Then
        LogError("Fired event")
        resp.Write(Token.DisplayName)
        resp.Write(Token.Email)
        resp.Write(Token.Uid)
    Else
        LogError("Fired event")
        resp.SendError(500,LastException.Message)
    End If
    LogError("NOT fired event")
End Sub
fs sub NEVER fired. Nothing logs at error log when i check it.
 
Last edited:

yiankos1

Active Member
Licensed User
Yes indeed! That post fixes almost everything. One more question, before every call (post/get) at app, token needs to be fristly verified with this handler? or prcedure is completely different?
 

yiankos1

Active Member
Licensed User
Thank you for answering me, but i have searched at forum about server sessions but I can't figure it out which fits me. Where should I store user state?

P.s. user state is, if a user logged in?
 
Top