Skip to content

Request Finishes

Request finishes action

The request finishes action allows you to execute an after-action after a request.

The most common use cases for this action are:

  • Access control - if a get_user request fails, redirect the user to the login page
  • Chaining requests - run a request after another request
  • Setting cookies - save an auth token in a cookie
  • Setting variables - store user preferences in a theme variable
  • Run function - initiate an external library with request data

We're going to look at each of these use cases in more depth.

Access control

After-action - Navigate to

To control access to pages, we can check whether our get_user request was successful or not, and then redirect the user to the login page, if they're not logged in.

All we have to do is tick the Conditional checkbox in our Navigate to action, and check for the following:

js
(c, f, i, n, r, v, e, t) => {
  return !r.get_user.ok; //[!code focus]
};

The same access control logic can be created using a Custom condition. Both approaches are equally good.

Chaining requests with Perform request

Orchestrating requests

Sometimes, you want to get a request-response before triggering another request.

There can be a log of reasons for that, but the most common are:

  • The data from your first request is needed to perform your second request
  • You want to re-run a get request after an item has been edited or deleted
  • You don't want to make unnecessary requests if a user is not logged in

With the Request finishes action, you can set up a Perform request action after the first request.

Set cookie

A widely used approach among authentication providers is to store the authentication token within a cookie.

The flow looks something like this:

  • The user submits a login form
  • The form submission triggers a login or signup request
  • If the request is successful, we save the token in a cookie and redirect the user to the dashboard.

To save the auth token in a cookie, all we need to do is set up the Set cookie after-action, and return the token from the request:

js
(c, f, i, n, r, v, e, t) => {
  return r.create_login_session.data.authToken; //![code focus]
};

Here's the whole action with a redirect: Set cookie and redirect user - Action

Setting a variable

Set variable

There can be many cases when you'd want to set or change a variable after a request is finished, including:

  • Filtering data
  • Sorting data
  • Personalization settings

In the example above, we allow users to change the theme of the app.

But, when the get_user request finishes, we get the user's current theme:

js
(c, f, i, n, r, v, e, t) => {
  return r.get_user.data.theme; //[!code focus]
};

Now the value of the theme is set to the current theme, but if the user changes it, we will update the theme variable.

Run function

Initialize charts.js

In a Run function, you can perform any javascript logic. This makes it the perfect candidate for initializing third-party libraries in our Wized app.

For example, let's say we want to initialize our charting library after we get the data for our chart from our backend.

This can be easily achieved with a Run function, like in the example above.

If you prefer, you can also achieve the same functionality using the Wized Javascript API instead of Run functions.