Post server connect user/pass values to an API action and save JSON response value/token

I’m struggling to find the right way to do this from the docs/videos - wondering if someone could steer me in the right direction?

Trying to work out how to get/store a value from an API JSON response, under the following scenario…

  • Web server with user info in SQL database
  • Odoo ERP server (same users/passwords stored) with REST API connection
    (The authentication of the Odoo server using the “get token” operation, documented at the top of the following features/documentation: https://apps.odoo.com/apps/modules/12.0/rest_api_drc/)

What I am trying to do is:

  • authenticate a user on the WEB server/SQL database and get the user information with security provider, (which is successful)
  • If the login is successful, pass the same username/password entered in the login action to the Odoo REST API, (which if also successful, returns JSON with a “token” value)
  • Next, store that “token” value with the security provider session on web server so it can be re-used for future API calls
  • On logout of the web server/security provider, delete the token from the Odoo server using the “delete token” operation

Can you simply define session variables in globals to store the token, etc… with ‘set session’ in your server connect and API calls as and then retrieve them as and when required?

or store in cookies or local browser storage

I’m trying to save the values using session/state, but do I put the API action in the SC login action or do I put it as a separate app connect api datasource component?

When the API connection is in the server connect login action there is no option to cache the api data.
When the API connection is an app connect component, the username/password post variables for the server connect action are not available to use in the API query parameters.

Do I need to save the username and password (perhaps hashed) in session/cookies first, then use them for the API datasource/caching, or can the server connect username/password post variables still be referenced without needing to store them in session?

Assuming your login process is a web form with two variable for username and password, then I would do as follows.

For good practice, create an auth folder in your API structure, and add a ‘login’ action that takes the two login form variables and calls the security login action. Add a security restrict after that to act as a gateway to the next step in the process, which is your ODOO login. This can either be an inline API call or a separate reusable server action in your library.

See the following example:

The variables are available to use directly (they’re in the $POST object. You can also choose to store them in your SESSION object for re-use later .

If none of this makes sense, I’m happy to jump on a Zoom and walk through it with you.

Thanks scalaris.
This is basically what I had for the login action - i forgot the Security restrict step though :slight_smile:
I have the GET API action working ok with url, and login/password query parameters using the SC POST variables and it’s returning the JSON with token ok…
The main issue I’m having is getting the returned JSON “token” value from the API call, which comes back in the (sample) format: {“token”: “24e635ff9cc74429bed3d420243f5aa6”}

How do use the returned JSON and save just the value (24e635ff9cc74429bed3d420243f5aa6) into a session value so I can make further API calls using this stored token id?

Ok, so first is to define a session variable in Globals to persist this value. Give it a suitable name

After you call the login action, the GET API action should have the token available in the output parameters (assuming you’ve defined the schema in the action).

Add a set session step in the server action, manually adding the name of the session variable you’ve created and selecting the binding from the API call

The token should be visible in the server bindings list

You can now explicitly retrieve the value in the session by adding it to a variable using ‘set value’

Awesome! Thanks so much, I’m only just starting with Wappler so I still have a lot to learn :slight_smile:

My API data schema wasn’t setup properly (at all), so that’s why I couldn’t find it after the API action step.

I thought I had everything setup right and I’m trying to simply output the value stored in the global variable on the second (home) page after logging in… but everything is blank.

Going to keep playing around with it… but you have steered me in the right direction

Good stuff. The define schema is often missed in the early days. I tend to use something like insomnia to verify the API action then copy it over to Wappler.

Wappler is a pain to learn frankly, But if you persevere, you’ will figure it out and I promise you will be as productive as a team of developers within 6 months.