Private Sub Authenticate
#if B4J
PrepareServer
#End If
Dim link As String = BuildLink("https://xxx.supabase.co/auth/v1/authorize?provider=google", _
CreateMap("client_id": mClientId, _
"redirect_uri": "https://xxx.supabase.co/auth/v1/callback","response_type":"code","scope":"openid profile email"))
#if B4A
Dim pi As PhoneIntents
StartActivity(pi.OpenBrowser(link))
#else if B4i
Main.App.OpenURL(link)
#else if B4J
fx.ShowExternalDocument(link)
#end if
End Sub
Private Sub PrepareServer
If server.IsInitialized Then server.Close
If astream.IsInitialized Then astream.Close
Do While True
Try
server.Initialize(port, "server")
server.Listen
Exit
Catch
port = port + 1
Log(LastException)
End Try
Loop
Wait For server_NewConnection (Successful As Boolean, NewSocket As Socket)
If Successful Then
astream.Initialize(NewSocket.InputStream, NewSocket.OutputStream, "astream")
Dim Response As StringBuilder
Response.Initialize
Log(Response.ToString)
Do While Response.ToString.Contains("Host:") = False
Wait For AStream_NewData (Buffer() As Byte)
Response.Append(BytesToString(Buffer, 0, Buffer.Length, "UTF8"))
Loop
astream.Write(("HTTP/1.0 200" & Chr(13) & Chr(10)).GetBytes("UTF8"))
Sleep(50)
astream.Close
server.Close
Log(Response.ToString) 'GET / HTTP/1.1
ParseBrowserUrl(Regex.Split2("$",Regex.MULTILINE, Response.ToString)(0))
End If
End Sub
Private Sub ParseBrowserUrl(Response As String)
Log(Response)
Dim m As Matcher = Regex.Matcher("code=([^&\s]+)", Response)
If m.Find Then
Dim code As String = m.Group(1)
GetTokenFromAuthorizationCode(code)
Else
Log("Error parsing server response: " & Response)
m_Supabase.Auth.Logout
RaiseEvent(False)
End If
End Sub