I am trying to achieve an elegant Firebase authentication module that transparently re-authenticates and renews the Firebase Token after the initial authentication. If the re-authentication or the token renewal fails, then the module should display the login button and allow a fresh login.
Here is my module code:
Thanks for taking a look, I imagine this function would be helpful to other Firebase users.
Gentry
Here is my module code:
B4X:
#Region Activity Attributes
#FullScreen: False
#IncludeTitle: True
#End Region
Sub Process_Globals
End Sub
Sub Globals
Private lblName As Label
End Sub
Sub Activity_Create(FirstTime As Boolean)
'Do not forget to load the layout file created with the visual designer. For example:
'Activity.LoadLayout("Layout1")
If FirstTime Then
Starter.fAuth.Initialize("fbAuth")
Wait For fbAuth_SignedIn (User As FirebaseUser)
Log("fbAuth_SignedIn - User = "&User)
Starter.fAuth.GetUserTokenId(User,False)
Wait For fbAuth_TokenAvailable (User As FirebaseUser, Success As Boolean, TokenId As String)
Log("fbAuth_TokenAvailable - Token = "&TokenId)
End If
End Sub
Sub btnSignIn_Click
Starter.fAuth.SignInWithGoogle
End Sub
Sub btnSignOut_Click
Starter.fAuth.SignOutFromGoogle
lblName.Text = "Goodbye!"
End Sub
Sub fbAuth_SignedIn (User As FirebaseUser)
' Starter.fAuth.GetUserTokenId(User,False)
Starter.fbuser = User
Log("SignedIn: " & User.DisplayName)
Return
End Sub
Sub fbAuth_TokenAvailable (User As FirebaseUser, Success As Boolean, TokenId As String)
If Success Then
Dim fbexpiretime As Period
fbexpiretime.Minutes = 55
Starter.fbtokenexpires = DateUtils.AddPeriod(DateTime.Now, fbexpiretime)
Log("FirebaseJWT "&TokenId)
Starter.fbtoken = TokenId
Activity.Finish
Else
Log("No Token :(")
Activity.LoadLayout("fbAuth")
End If
Return
End Sub
Sub Activity_Resume
If Starter.fAuth.CurrentUser.IsInitialized Then
fbAuth_SignedIn(Starter.fAuth.CurrentUser)
Else
Activity.LoadLayout("fbAuth")
End If
End Sub
Sub Activity_Pause (UserClosed As Boolean)
End Sub
Sub btnContinue_Click
If Starter.fbtoken <> "" Then
StartActivity(Main)
End If
End Sub
Thanks for taking a look, I imagine this function would be helpful to other Firebase users.
Gentry