Android Question Back4app Parse server authentication

Discussion in 'Android Questions' started by Alvsky, Apr 23, 2019.

  1. Alvsky

    Alvsky Member Licensed User

    Hi
    I decided to use Back4app Parse server for my project as a backend.

    I wonder did someone try to communicate with parse server and how?
    Since the only Parse library I found here is outdated, and I am not very skilled programmer :), I decided that CURL is the best (i.e. easiest ;) ) way to connect with back4app server (using API and REST keys).

    So far, I managed to implement all CRUD operations on parse objects.
    the "only" thing I have problems with is the authentication.
    I can make Register and Login with no problems.

    Here are my questions:

    When I sign in (or register) a session token is created on back4app server.
    What exactly I do with it? Do I have to store it in a variable and check it every time I do some CRUD operations, or something else.

    How do I Logout?
    I couldn't find it in documentation (https://dashboard.back4app.com/apidocs#User-custom-class)

    ... and in the parseproject.org documentation it is explained, but I can not get it to work
    (https://docs.parseplatform.org/rest/guide/#deleting-sessions)

    I hope someone did this and can help me.

    A.
     
  2. DonManfred

    DonManfred Expert Licensed User

    The Location header will contain the endpoint of the newly-created object.

    So you probably get it in the headers of the job request-result.

    What is the code you are using and what exactly is the error message you got?
     
  3. Alvsky

    Alvsky Member Licensed User

    Hi
    thanks for the tip...
    I did a little bit of digging (or a lot :)) and managed to implement a registration/login/logout process (for now)

    I did in in a way that I have a DB table with parseUser, parseObjectID and session_token columns alongside all others usual columns needed for a user.

    My app tracks crop harvest on the farm and main user is actually a company so I have one predefined company in my DB in table 'company'.

    When new company (user) registers I update parseUser, parseObjectID and session_token with actual data.
    On logout, I delete 'session_token' (i.e. I update it with blank string ("") )

    When application starts first the check is done if parseUser field is empty or not. If it is empty Register activity is shown.

    for login, the check is done if session_token is empty or not and according to result SingIn activity is shown or not.

    Here is the code:

    Code:
    'MAIN activity
    Sub Process_Globals
        
        
    Dim AppID = "xxxxxxxx" As String
        
    Dim RESTid = "xxxxxxxx" As String
        
    Dim parseServerLink = "https://parseapi.back4app.com/" As String

        
    '...

    End Sub

    '...

    Sub checkCompanyIdInternal 
       
    Log("checkCompanyIdInternal")
       dbC = SQL1.ExecQuery(
    "SELECT * FROM 'company' WHERE id = '1'")
       dbC.Position=
    0

       
    If dbC.GetString("parse_objectID") <> "" Then
           company_id = dbC.GetString(
    "id")
           company_name = dbC.GetString(
    "name")
           parse_objectID = dbC.GetString(
    "parse_objectID")
           sessionToken = dbC.GetString(
    "session_token")
           
    If sessionToken = "" Then
               lblCompanyName.Text = company_name&
    " (Odjavljeni ste)"
               
    Else
               lblCompanyName.Text = company_name
           
    End If
         
           
    Else
               
    Log("idi na Register")
               
    StartActivity(actRegister)
               
    Activity.Finish
               
       
    End If
       
       dbC.Close
    End Sub

    Sub logout

           
    Dim curl As HttpJob
           curl.Initialize(
    "", Me)
           curl.PostString(parseServerLink&
    "logout","")
       
           curl.GetRequest.SetHeader(
    "X-Parse-Application-Id",AppID)
           curl.GetRequest.SetHeader(
    "X-Parse-REST-API-Key",RESTid)
           curl.GetRequest.SetHeader(
    "X-Parse-Session-Token",sessionToken)
           SQL1.ExecNonQuery2(
    "UPDATE company SET session_token=? WHERE id= ?",Array As String(""1))
           curl.Release
           
    Activity.Finish
           
    ExitApplication

    End Sub


    'REGISTER activity

    Sub btnswRegister_Click
       kbrd.HideKeyboard
       
       
    Dim j As HttpJob
       j.Initialize(
    "log-in", Me)
       
    Dim user As Map
       user.Initialize
       user.Put(
    "username", fldRegEmail.Text)
       user.Put(
    "password", fldRegPassword.Text)
       user.Put(
    "email", fldRegEmail.Text)
       user.Put(
    "company_name", fldRegCompany.Text)
       user.Put(
    "name", fldRegName.Text)
       user.Put(
    "last_name", fldRegLastName.Text)
       user.Put(
    "oib", fldRegOIB.Text)
       
    Dim gen As JSONGenerator
       gen.Initialize(user)
       j.PostString(Main.parseServerLink&
    "users", gen.ToString)
       j.GetRequest.SetHeader(
    "X-Parse-Application-Id", Main.AppID)
       j.GetRequest.SetHeader(
    "X-Parse-REST-API-Key", Main.RESTid)
       j.GetRequest.SetHeader(
    "X-Parse-Revocable-Session""1")
       j.GetRequest.SetContentType(
    "application/json")
       
       j.Release

    End Sub

    Sub JobDone(job As HttpJob)
       
    If job.Success Then
           
    Log(job.GetString)
           
           
    Dim parser As JSONParser
           parser.Initialize(job.GetString)
           
    Dim root As Map = parser.NextObject
           
    Dim objectId As String = root.Get("objectId")
           
    Dim sessionToken As String = root.Get("sessionToken")
              
       
    If fldRegOIB.Text = "" Then fldRegOIB.Text = "0"
       
           Main.SQL1.ExecNonQuery2(
    "UPDATE company SET parse_objectID=?, session_token=?, name=?, oib=?, email=?, username=?",Array As String(objectId, sessionToken, fldRegCompany.Text, fldRegOIB.Text, fldRegEmail.Text, fldRegEmail.Text))

           
    StartActivity(Main)
           
    Activity.Finish

       
    Else
           
    Log(job.ErrorMessage)
           lblError.Text = job.ErrorMessage
           dialogAlert.ShowCustom(pnlErr, 
    "OK""""")
               
       
    End If
       job.Release
    End Sub


    'SIGN IN activity

    Sub btnswSignIn_Click
       
    Dim j As HttpJob
       j.Initialize(
    "log-in", Me)
       j.Download(Main.parseServerLink&
    "login?username="&fldRegEmail.Text&"&password="&fldRegPassword.Text&"")
       j.GetRequest.SetHeader(
    "accept","application/json")
       j.GetRequest.SetHeader(
    "X-Parse-Application-Id", Main.AppID)
       j.GetRequest.SetHeader(
    "X-Parse-REST-API-Key", Main.RESTid)
       j.GetRequest.SetHeader(
    "X-Parse-Revocable-Session""1")
       
    End Sub

    Sub JobDone(job As HttpJob)
       
    If job.Success Then
           
    Log(job.GetString)
           
           
    Dim parser As JSONParser
           parser.Initialize(job.GetString)
           
    Dim root As Map = parser.NextObject
           
    Dim objectId As String = root.Get("objectId")
           
    Dim sessionToken As String = root.Get("sessionToken")
           
    Log(objectId &" -- "&sessionToken)
       
           Main.SQL1.ExecNonQuery2(
    "UPDATE company SET session_token=? WHERE id= ?",Array As String(sessionToken, 1))


           
    StartActivity(Main)
           
    Activity.Finish

       
    Else
           
    Log(job.ErrorMessage)
               
       
    End If
       job.Release
    End Sub

    I hope this will help someone. At least to start with something.

    And of course, if I did something wrong and someone notices I would appreciate a correction :)
     
    José J. Aguilar likes this.
Loading...
  1. This site uses cookies to help personalise content, tailor your experience and to keep you logged in if you register.
    By continuing to use this site, you are consenting to our use of cookies.
    Dismiss Notice