Calling Wix-Users-Backend code from mobile device

UPDATE 2:
While the update below works, it is still not ideal for programmatically logging in the user. I still must access from a web page that uses the front-end API applySessionToken(), using the token returned from the back-end login() API. I have gone around and around on this, and have come up with a “hack” solution which I have documented in this post .

Wix, please consider updating your API architecture to allow true programmatic access to the Corvid APIs from mobile apps (i.e. being able to authenticate and call Corvid API without having to process web pages)!

Check out the Dropbox Java 2 API (for Android - they have equivalent on iOS) - this approach works well.

UPDATE:

I decided to edit the post rather than create a comment. After reading through multiple posts, and particularly finding this article , I changed my Wix back-end code that is called by my mobile app to this:

export async function get_utils(request) {
    console.log("utils function called");

 let response = { "headers": { "Content-Type": "application/json"}};
 const operation = request.path[0]; // "login"
 switch (request.path[0]) {
 case 'login': 
 {
     const username = request.query["username"];
     const password = request.query["password"];

     // body has json which is username and password
     try {
       await wixUsers.login(username, password)
        .then( (sessionToken) => {
           response.body = {"result":"login succeeded", "token":sessionToken};
           console.log("Returning response: " + response.body.token);
         },
         (error) => {
          response.body = {"result": "login failed", "errormsg": error};
          console.log("Returning response: " + response.body.result);
         } )     
       .catch( (err) => {
         console.log("Got error: " + err);
         response.body = {"result": "login failed","errormsg" : err.message};
        } );
     } finally {
        return ok(response);
     }                          
  }
 
  default: {
    response.body = {"result": "login failed"};
    return ok(response);        
  }

}

This works well for what I’m trying to do, which is essentially cause a synchronous attempt to login from my mobile app. There is a note of caution however, that this approach really should only be done for back-end work that will not delay long. A DB query for example would not be appropriate, because using this approach I am essentially blocking the UI in my mobile app until the return completes.

What I ended up doing in the front-end app that calls this function is to implement a queue that causes this function to be called and the front-end updated in an asynchronous manner. However I still would like to understand if making the server-side code synchronous will cause some sort of bottleneck as the access scales to (hopefully) thousands of front-end requests.

Absent some way to update asynchronously in real time to the caller of the back-end function when that caller isn’t a web page also hosted on Wix (and can thus deal with a Promise that is returned from a back-end function), this is what I’ve come up with so far.

I would love to see best practices for a way to subsequently “notify” a HTTPS-based caller of this API when the Promise is fulfilled, can anyone point me to some good examples? In other words, when my function gets called via HTTPS and the back-end API returning a Promise gets called, what can I do in the .then() clause that can reach back out to the caller:

wixUsers.login(username, password)       
      .then( (sessionToken) => {            
           response.body = {"result":"login succeeded", "token":sessionToken};
           WHAT TO DO HERE WITH: ok(response);
         },
             (error) => {
           response.body = {"result": "login failed", "errormsg": error};
           WHAT TO DO HERE WITH: ok(response);
         }
       ) 

BTW - I still don’t understand what I’m supposed to do about the deprecation warning.

END UPDATE

ORIGINAL POST:

I want my mobile app to communicate with the Wix backend functions in order to register, login, etc. There are no “front-end web pages” envisioned here - instead I am using Volley in the Android mobile app to perform https GET/PUT. This part is working fine. The function gets called. On a simple test of a GET with two numeric arguments, I can return a response that multiplies the arguments together for an answer.

When I move to doing something with wix-users-backend, I am running into all kinds of problems, including Server 500 errors. Here is the backend function I am calling from my mobile app:

import {ok, badRequest} from 'wix-http-functions';
import wixUsers from 'wix-users-backend';
export function get_utils(request) {
    console.log("utils function called");

 let response = {
 "headers": {
 "Content-Type": "application/json"
        }
    };

 const operation = request.path[0]; // "login"
 switch (request.path[0]) {
 case 'login': {
     const username = request.query["username"];
     const password = request.query["password"];
     console.log("Got operation: " + operation + " and username: " + username 
     + " and password: " + password);
     // body has json which is username and password
 
     wixUsers.login(username, password)
      .then( (sessionToken) => {
           response.body = {"result":"login succeeded", "token":sessionToken};
           console.log("Returning response: " + response.body);
           return ok(response);
           },
           (error) => {
            response.body = {"result": "login failed", "errormsg": error};
            console.log("Returning response: " + response.body);
            return ok(response);
            } 
         )
      .catch( (err) => {
           console.log(err);
           response.body = {"result": "login failed",
                            "errormsg" : err.message};
           return ok(response);
      } );                            
    }
    break;
 default:
     response.body = {"result": "login failed"};
     return ok(response);
}

What is causing the 500 Server error?

Also, I am getting a deprecation warning in the console:

 (node:1) [DEP0097] DeprecationWarning: Using a domain property in MakeCallback is deprecated. Use the async_context variant of MakeCallback or the AsyncResource class instead. 

I have no idea what this means or how to fix it - I am structuring my code straight out of the Wix API documentation.

Help!

I am having trouble import wix user does not seem to work on Android … (Samsung browswer).