B4A Class [B4X] Firebase Auth REST Helper - manage the access tokens

The advantage of the rest API of Firebase auth is that it can be used cross platform. The downside is, we have to take care of the user access tokens ourselves. That's why I wrote this class, it works with the "FirebaseAuthREST" class.

The class was inspired by @Erel oauth2 class, adapted to my class
For the attached example you need the FirebaseAuthREST class.
The class were tested in B4A.

FirebaseAuthRESTHelper
Author: Alexander Stolte
Version: 1.0

  • FirebaseAuthRESTHelper
    • Events:
      • AccessTokenAvailable (Success As Boolean, Token As String)
      • Authenticate
      • RefreshToken (RefreshToken As String)
    • Functions:
      • Class_Globals As String
      • GetAccessToken As String
      • Initialize (Target As Object, EventName As String) As String
        Initializes the object. You can add parameters to this method if needed.
      • IsInitialized As Boolean
        Tests whether the object has been initialized.
      • ResetToken As String
      • TokenInformationFromResponse (m As Map) As String
  • TokenInformations
    • Fields:
      • AccessExpiry As Long
      • AccessToken As String
      • IsInitialized As Boolean
        Tests whether the object has been initialized.
      • RefreshToken As String
      • Valid As Boolean
    • Functions:
      • Initialize
        Initializes the fields to their default value.

Usage Example:
Dim farh As FirebaseAuthRESTHelper
Dim far As FirebaseAuthREST

farh.Initialize(Me,"farh")
far.Initialize

farh.GetAccessToken

Private Sub farh_AccessTokenAvailable (Success As Boolean, Token As String)

    Log("farh_AccessTokenAvailable")
    Log("Success: " & Success)
    Log("Token: " & Token)

End Sub

Private Sub farh_Authenticate

    Log("farh_Authenticate")

    Wait For (far.signInEmailPW("[email protected]","Test123",True)) complete (root As Map)
    If root.Get("success") = True Then

        farh.TokenInformationFromResponse(root)

    End If

End Sub

Private Sub farh_RefreshToken (RefreshToken As String)

    Log("farh_RefreshToken")
    Log("RefreshToken: " & RefreshToken)

    Wait For (far.refreshToken("refresh_token",RefreshToken)) complete (root As Map)
    If root.Get("success") = True Then

        farh.TokenInformationFromResponse(root)

    End If

End Sub

Changelog
  • 1.00
    • Release
  • 1.01
    • Add GetEmail - gets the email-address from the user
  • 1.02
    • Add tag to TokenInformations
Have Fun :)
 

Attachments

  • B4A Example.zip
    2.6 KB · Views: 572
  • FirebaseAuthRESTHelper.bas
    3 KB · Views: 562
Last edited:

Alexander Stolte

Expert
Licensed User
Longtime User
Update
  • 1.02
    • Add tag to TokenInformations
Usage Example:
Usage Example:
'for example we have this type:
Type UserInfos (username As String)

Public Sub CreateUserInfos (username As String) As UserInfos
    Dim t1 As UserInfos
    t1.Initialize
    t1.username = username
    Return t1
End Sub

'save infos
Dim parser As JSONParser
parser.Initialize(j.GetString)
Dim tmp_map As Map = parser.NextObject
tmp_map.Put("tag",cm_functions.CreateUserInfos("myusername")) 'put a new value named "tag" to the map with your custom type values
farh_auth.TokenInformationFromResponse(tmp_map) 'updates the saved informations

'use infos
If farh_auth.GetTag <> Null Then
    Dim tmp_ui As UserInfos = farh_auth.GetTag
    xlbl_username.Text = tmp_ui.username
Else
    xlbl_username.Text = ""
End If
 

Mashiane

Expert
Licensed User
Longtime User
Hi, you don't happen to perhaps have a firebase messaging & notifications rest api helper class? This is what I need.

1. Get a token (wait for it until you get it)
2. Subscribe to a topic (wait for it until a subscription is done)

I'm using B4A. With the current implementation of the firebase service, I've seen some people say add sleep(0), use a do loop to wait for the token to be provided etc etc. This does not sound good. With the power of Wait For, there should be a better way unless the matter is really unsolvable.

Thanks
 

Alexander Stolte

Expert
Licensed User
Longtime User
helper class?
this helper class is meant to better handle the complexity of the token, since with the REST api you have to manage the token yourself at the client.
Get a token (wait for it until you get it)
do you mean the user login token/Access Token?
My FirebaseAuthREST class have a"refreshToken" function, who you need the refresh token of your user, of course it works with wait for.

Subscribe to a topic (wait for it until a subscription is done)
i have updated the code here with the wait for feature
 

Mashiane

Expert
Licensed User
Longtime User
Thanks

The thorny issue for me is rather..

B4X:
Public fm As FirebaseMessaging
fm.Token

because the token is seldom returned (on first app run) and thus one cannot use it. In most cases I have to restart the app. I am not using the authorization helper but just firebase messaging and notifications. Im not sure if there is an event that gets fired when the token is available, besides tokenRefresh.
 
Top