After logging in to app, timer based script to stay logged in via background session

I'm building an internal app for our client success team that requires them to login (in order to get a JWT token that will be used for the remainder of the session). Behind the scenes the API will log the user out / expire the session if there is inactivity but I don't want the users to keep having to login in if their retool app remains open.

My question / requirements / use case are as follows:

  1. User logs into the internal app at the start of their session (The login form only shows when they are logged out)
  2. After the user logs in there is a trigger / script that acts like a ping and once evert couple of minutes makes a rest request to the API so that the token stays active. I guess it could reference one of the existing resources and just make the request.
  3. As long as the user is logged in it would hide the login form.

Hello @Josh_Nisenson,

Welcome to the community!

To implement a ping script to keep a Retool user logged in even if the API session expires, you can follow these steps:

  1. Create a new JavaScript query that will:

    • Make a REST request to the API endpoint that refreshes the user's session.
    • Check the response from the API endpoint to make sure that the user's session was refreshed successfully.
  2. Create a new recurring event that triggers the JavaScript query every couple of minutes.

  3. In your app's login form, check to see if the user is already logged in. If the user is already logged in, hide the login form.

Here is an example of a JavaScript query that you can use:

// Make a REST request to the API endpoint that refreshes the user's session.
const response = await api.trigger({
  name: "RefreshToken",
});

// Check the response from the API endpoint to make sure that the user's session was refreshed successfully.
if (response.status === 200) {
  // The user's session was refreshed successfully.
} else {
  // The user's session was not refreshed successfully.
  // You may want to log an error message or take other action.
}

Here is an example of how to create a recurring event in Retool:

  1. Go to the Events page in your Retool app.
  2. Click the New Event button.
  3. Select the Recurring event type.
  4. Set the Frequency to the desired interval, such as every 2 minutes.
  5. Set the Query to the JavaScript query that you created in step 1.
  6. Click the Create Event button.

Once you have created the recurring event, it will trigger the JavaScript query every couple of minutes, which will keep the user's session active.

To check to see if the user is already logged in, you can use the following JavaScript code:

const user = await api.trigger({
  name: "GetCurrentUser",
});

if (user) {
  // The user is already logged in.
} else {
  // The user is not logged in.
  // You may want to show the login form.
}

You can add this code to the login form's onSubmit event handler. If the user is already logged in, the login form will not be displayed.

By following these steps, you can implement a ping script to keep a Retool user logged in even if the API session expires.

Hope this helps.

:grinning:

Patrick

Thanks so much @PatrickMast that is super helpful! I'll give this a whirl and come back with any follow up questions if I have them.

2 Likes