Android Question [Solved] Pocketbase authentication, store token?

kgf

Member
Licensed User
Longtime User
Hi all

I am looking at using pocketbase to send data to. The app will be offline most of the time (could be days or weeks ) but occasionally would like to connect and send or receive data from pocketbase.

I would the users to be able to enter their login details on first use while online and then have the app save their credentials somehow for when they are connected and would like to send data.

Does anyone know if the pocketbase library stores tokens and for how long? or is this best done through KVS or similar. Is there a best practice for this?

Thanks
 
Solution
That is why there is a library that creates a standardized system and before anything is executed, it is checked whether the token is valid and if not, it is automatically renewed.

Alexander Stolte

Expert
Licensed User
Longtime User
Does anyone know if the pocketbase library stores tokens and for how long?
The token is stored for as long as your app is installed, it just expires at some point. In the Pocketbase Dashboard you can define how long a token is active:
1741708986021.png
 
Upvote 0

kgf

Member
Licensed User
Longtime User
Thanks, So the token would be retrieved from Auth.TokenInformations, is this the right property?
 
Upvote 0

Alexander Stolte

Expert
Licensed User
Longtime User
Token management is done by the library itself. If the token has expired, an auth event is triggered.
B4X:
Private Sub PocketBase_AuthStateChange(StateType As String)
    Select StateType
        Case "passwordRecovery"
            Log("Reset password was requested")
        Case "signedIn"
            Log("The user has logged in")
        Case "signedOut"
            Log("The user has logged out")
        Case "tokenRefreshed"
            Log("The Auth Token was updated")
        Case "userUpdated"
            Log("The email address and or password has been changed")
        Case Else
            Log("Unknown State Type")
    End Select
End Sub
 
Upvote 0

kgf

Member
Licensed User
Longtime User
so any functions such as a below the library will manage the auth etc once a user has signed in, I was was assuming that I would need to provide it from somewhere, very new to this so thanks very much.
B4X:
    Dim Insert As Pocketbase_DatabaseInsert = xPocketbase.Database.InsertData.Collection("dt_Task")
    Insert.Parameter_Fields("Task_Name,Task_CompletedAt")
    Dim InsertMap As Map = CreateMap("Task_UserId":xPocketbase.Auth.TokenInformations.Id,"Task_Name":"Task 06","Task_CompletedAt":Pocketbase_Functions.GetISO8601UTC(DateTime.Now))
    Wait For (Insert.Insert(InsertMap).Execute) Complete (DatabaseResult As PocketbaseDatabaseResult)
    xPocketbase.Database.PrintTable(DatabaseResult)
 
Upvote 0

Alexander Stolte

Expert
Licensed User
Longtime User
That is why there is a library that creates a standardized system and before anything is executed, it is checked whether the token is valid and if not, it is automatically renewed.
 
Upvote 0
Solution
Top