B4J Question [ABMaterial] log in with php api

jayel

Active Member
Licensed User
Longtime User
Hello,

I set my ABMaterial project up to log in against a php api that run on another server.
I followed this example :
https://www.b4x.com/android/forum/threads/mymaterial-show-free4life.70259/page-5#post-454226

But when I want to log in a have to push the login button twice???

Strange.
B4X:
Sub msbtn1_Clicked(Target As String)

    Dim mymodal As ABMModalSheet = AppPage.ModalSheet("login")
    Dim inp1 As ABMInput = mymodal.Content.Component("inp1")
    Dim inp2 As ABMInput = mymodal.Content.Component("inp2")
    ' here check the login a page against your login database
'    Dim SQL As SQL = DBMusers.GetSQL
'
'    Dim users As List = DBMusers.SQLSelect(SQL, "SELECT * FROM tblusersnew WHERE webusername='" & inp1.Text & "' AND paswoord='" & inp2.Text & "'")
'    DBMusers.CloseSQL(SQL)
'
'    If users.Size = 0 Then    
'        inp2.Text = ""
'        mymodal.Refresh        
'        AppPage.ShowModalSheet("wronginput")
'    
'        Return
'    End If
'    Dim user As Map = users.Get(0)
'
'    ABM.SaveLogin(AppPage, AppName, inp1.Text, inp2.Text)
'    ws.Session.SetAttribute("authType", "local")
'    ws.Session.SetAttribute("authName", inp1.Text)
'    ws.Session.SetAttribute("IsAuthorized", "true")
'    'ws.Session.SetAttribute("UserType", "" & user.Get("usertype") )    ' lowercase!            
'    ws.Session.SetAttribute("UserID", "" & user.Get("idnummer") ) ' lowercase!
'    ABMShared.NavigateToPage(ws, "./" & InitialPage)
    
    dbAction.Initialize
    dbAction.Put("tag", "loginnormal")
    dbAction.Put("username", inp1.Text)
    dbAction.Put("password", inp2.Text)
    ABMShared.ExecutePHP(Me,dbAction, "loginnormal", "user.php", "loginnormal")
End Sub

Sub GetUser(s_result As String)
    Dim mymodal As ABMModalSheet = AppPage.ModalSheet("login")
    Dim inp1 As ABMInput = mymodal.Content.Component("inp1")
    Dim inp2 As ABMInput = mymodal.Content.Component("inp2")
    Dim parser As JSONParser
    parser.Initialize(s_result)
    Dim root As Map = parser.Nextobject
    Dim error As String = root.Get("error")
    Dim tag As String = root.Get("tag")
    Log("tag " & tag)
    If error = "false" Then
        Dim user As Map = root.Get("user")
        Dim naam As String = user.Get("naam")
        Dim voornaam As String = user.Get("voornaam")
        Dim kortenaam As String = user.Get("kortenaam")
        Dim userid As String = user.Get("idnummer")
        Log("login : " & kortenaam)
    
        ABM.SaveLogin(AppPage, AppName, inp1.Text, inp2.Text)
        ws.Session.SetAttribute("authType", "local")
        ws.Session.SetAttribute("authName", kortenaam)
        ws.Session.SetAttribute("IsAuthorized", "true")
        'ws.Session.SetAttribute("UserType", "" & user.Get("usertype") )    ' lowercase!            
        ws.Session.SetAttribute("UserID", "" & userid.ToLowerCase ) ' lowercase!
        ABMShared.NavigateToPage(ws, "./" & InitialPage)
    Else
        inp2.Text = ""
        mymodal.Refresh        
        AppPage.ShowModalSheet("wronginput")

        Return
    End If
End Sub
Sub JobDone(job As HttpJob)
    If job.Success = True Then
        Select job.JobName
        Case "loginnormal"
            Log(job.GetString)
            'JobList = ABMShared.Json2List(job.GetString)
            JobTag = job.Tag
        
        
            Dim mystr As String = job.GetString
            job.Release
            Log("mystr = " &  mystr)
            If mystr.Length > 0 Then
                GetUser(mystr)
            End If
        End Select
    Else
        'myToastId = myToastId + 1
        'page.ShowToast("toast" & myToastId, "toastred", job.errormessage, 5000)
        'job.release
    End If

    'job.release
End Sub

Abshared executePHP :
B4X:
Sub Json2List(jsonTxt As String) As List
    Dim root As List
    root.Initialize
    Try
        Dim parser As JSONParser
        parser.Initialize(jsonTxt)
        root = parser.NextArray
        Return root
    Catch
        Return root
        'Log(LastException)
    End Try
End Sub' runs a php scripts at the server
Sub ExecutePHP(page As Object, pQuery As Map, JobName As String, phpFile As String, phpTag As String)
    Dim job As HttpJob
    Dim scommand As String
    Dim json As String
    job.Initialize(JobName, page)
    job.Tag = phpTag
    json = Map2QueryString(pQuery)

    If json.Length = 0 Then
        scommand = URLstring & phpFile
    Else
        scommand = URLstring & phpFile & "?" & json
    End If
    Log(scommand)
    job.Download(scommand)
End Sub
Sub Map2QueryString(sm As Map) As String
    ' convert a map to a querystring string
    Dim SU As StringUtils
    Dim iCnt As Int
    Dim iTot As Int
    Dim sb As StringBuilder
    Dim mValue As String
    sb.Initialize
 
    ' get size of map
    iTot = sm.Size - 1
    iCnt = 0
    For Each mKey As String In sm.Keys
        mValue = sm.Get(mKey)
        mValue = SU.EncodeUrl(mValue, "UTF8")
        mKey = mKey.Trim
        If mKey.EndsWith("=") = False Then mKey = mKey & "="
        sb.Append(mKey).Append(mValue)
        If iCnt < iTot Then sb.Append("&")
        iCnt = iCnt + 1
    Next
    Return sb.ToString
End Sub

myresult in log window :
2
Waiting for value (93 ms)
12/12/2016 13:39:01 Send : 129
12/12/2016 13:39:03 Kraan : EX-1/01 timed out
12/12/2016 13:39:03 Send : 130
12/12/2016 13:39:06 Kraan : EX-1/02 timed out
12/12/2016 13:39:16 Send : 129
12/12/2016 13:39:18 Kraan : EX-1/01 timed out
12/12/2016 13:39:19 Send : 130
12/12/2016 13:39:21 Kraan : EX-1/02 timed out
Waiting for value (3 ms)
12/12/2016 13:39:23 Connected
Waiting for value (19 ms)
[Ljava.lang.String;@7f509561
12/12/2016 13:39:31 Send : 129
Waiting for value (9 ms)
http://x.x.x.x/kraanserver/user.php?tag=loginnormal&username=john&password=xxxxx
{"tag":"loginnormal","error":false,"user":{"idnummer":"2","naam":"Lauwers","voornaam":"John","kortenaam":"John"}}
mystr = {"tag":"loginnormal","error":false,"user":{"idnummer":"2","naam":"Lauwers","voornaam":"John","kortenaam":"John"}}
tag loginnormal
login : John
Waiting for value (5 ms)
12/12/2016 13:39:33 Kraan : EX-1/01 timed out
12/12/2016 13:39:34 Send : 130
12/12/2016 13:39:36 Kraan : EX-1/02 timed out
Waiting for value (3 ms)
http://x.x.x.x/kraanserver/user.php?tag=loginnormal&username=john&password=xxxxx
{"tag":"loginnormal","error":false,"user":{"idnummer":"2","naam":"Lauwers","voornaam":"John","kortenaam":"John"}}
mystr = {"tag":"loginnormal","error":false,"user":{"idnummer":"2","naam":"Lauwers","voornaam":"John","kortenaam":"John"}}
tag loginnormal
login : John
Waiting for value (4 ms)
12/12/2016 13:39:38 Disconnected
Waiting for value (55 ms)
Waiting for value (37 ms)
2
Waiting for value (44 ms)
12/12/2016 13:39:46 Send : 129
 

Mashiane

Expert
Licensed User
Longtime User
Hi @jayel

I see you made reference to a post I wrote. Did you ever manage to get your issue sorted here?

Whilst it might be prudent to execute php scripts from another server, should you need to. I was also advised against that approach when using the ABMaterial framework. I would suggest that you establish a connection to the other server and then just run sql commands against that for your login.

Should you need advise, dont hesitate to pm me within the forum.

Ta!
 
Upvote 0
Top