Android Code Snippet [Supabase] Findings

Hello,

I am doing my baby steps with Supabase and i will post here my findings in case someone needs them.

1) If you JOIN tables and filtering, then you will get results even it is equal with filter even it is not. So, in order to INNER JOIN tables and get only results that corresponds with filters you have to do this:
B4X:
    Dim Query As Supabase_DatabaseSelect = xSupabase.Database.SelectData
    Query.Columns("*,workoutcalendar!inner(wctime),users!inner(name)").From("appointments").Filter_Equal(CreateMap("workoutcalendar.wcdate":DateTime.Date(FirstDate))).OrderBy("workoutcalendar(wctime).asc")
    Wait For (Query.Execute) Complete (DatabaseResult As SupabaseDatabaseResult)
    xSupabase.Database.PrintTable(DatabaseResult)

So here, we will get every column (*) from appointments table, wctime from workoutcalendar table and name from users table that corresponds to a specific date ordered by wctime of workoutcalendar table.

You have to put: !inner before column name. More info here.
 
Last edited:

yiankos1

Well-Known Member
Licensed User
Longtime User
2) If you want to get total rows of a table COUNT(*) you can do it by using this:
B4X:
    Dim Query As Supabase_DatabaseSelect = xSupabase.Database.SelectData
    Query.Columns("count").From("users")
    Wait For (Query.Execute) Complete (DatabaseResult As SupabaseDatabaseResult)
    For Each Row As Map In DatabaseResult.Rows
        Dim numusers As Int = Row.Get("count")
        Log("Total Users: " & numusers)
    Next
More info here.
 
Last edited:

yiankos1

Well-Known Member
Licensed User
Longtime User
3) If you encounter an error: supabase auth: access token could not be renewed or invalid refresh token: already used, you can fix it by log in and log out with your user's credentials. Then, you can CRUD your database again

If i understand correctly, in order to avoid this error, don't forget to logout the user.

It took me 2 hours searching this.
 
Last edited:

yiankos1

Well-Known Member
Licensed User
Longtime User
4) Invite a user by email -> user verify email -> user creates a new password

-Put your site at URL
1.jpg


-Edit your email template that corresponds to the correct page where a user can enter his new password (in my case /updatepassword)
2.jpg


-You can invite an email using this:
B4X:
xSupabase.Auth.LogIn_MagicLink("test@gmail.com")

-Then user recieves this mail

3.jpg


-When you click this link, it verifies the email address and then it redirects you at webapp that you created in order this user create a password. At redirected URL there are couple of parameters that you have to extract them and use them in order to save the new password of that user using this code
B4X:
Sub btnUpdate_Clicked(Target As String)

    Private url As String = ws.EvalWithResult("return location.href",Null).Value
 
    If url.ToLowerCase.Contains("#") Then
        Dim hash As String
        hash = url.SubString(url.IndexOf("#") + 1)
   
        Dim result1 As Map
        result1.Initialize
   
        Dim params() As String
        params = Regex.Split("&", hash)
   
        For Each param As String In params
            Dim parts() As String
            parts = Regex.Split("=", param)
       
            If parts.Length = 2 Then
                result1.Put(parts(0), parts(1))
            End If
        Next
   
        ' Now 'result' contains the key-value pairs from the hash
        Log(result1)
   
       'Put all the extracted parameters from URL to Supabase's token iformation in order to allow us to update the new password
        xSupabase.Auth.TokenInformations.AccessToken=result1.Get("access_token")
        xSupabase.Auth.TokenInformations.RefreshToken=result1.Get("refresh_token")
        xSupabase.Auth.TokenInformations.AccessExpiry=result1.Get("expires_at")
        xSupabase.Auth.TokenInformations.TokenType=result1.Get("token_type")
        xSupabase.Auth.TokenInformations.Valid=True
   
        'Repeat Password edit text. We will use it order to get the new password
        Dim etPassRepeat As ABMInput = page.Component("etPass")
 
        Wait For (xSupabase.Auth.UpdateUser("test@gmail.com",etPassRepeat.Text)) Complete (result As SupabaseError)
        If result.Success Then
            Log("User data successfully changed")
        Else
            Log("Error: " & result.ErrorMessage)
        End If
    End If
 
End Sub

Of course this procedure is some kind of a workaround if you are an admin in admin panel. I don't know if Auth Admin is the correct way
 
Last edited:
Top