Paginate users in a SPA

According to the docs (Pagination), pagination should be performed using the “link” header in the response.
However, when requesting all users (/api/v1/users) from a SPA using a fetch CORS request, the “links” header can not be accessed from the headers object:

fetch('.../api/v1/users', requestOptions)
    .then(response => {
       response.headers.get('link') === null
       ...
    })
...

How should the pagination of all users be performed in this case?

@fes Can you please take a look at the note here?
This should help.

@Lijia Thanks for the reply! :slight_smile: Unfortunately, I cannot access the link property of the response header in JavaScript on the client. So do I have to manually build the “after” URL for the next page and see if the response is an empty array or is there a better way to find out if I still have to fetch more users?

How are you authorizing this request? I hope you’re not exposing an API key on your front end to make the call to the /users endpoint!

@andrea No, I’m using the Authorization code with PKCE flow to get an access token which is then stored in the session storage on the client. The request to /users is then authorized by adding the access token to the request header.

Hm… I’m also having trouble getting the “link” header back when making a Fetch request to /users with an access token, but I can clearly see it in the network events in the browser. I’m checking to see if this is expected or not.

@andrea Thanks for trying it out! It seems that Okta would have to add the link header to the Access-Control-Expose-Headers response header, so that it can be accessed in CORS requests!?

Yup, @fes, you appear to be correct. We’ve filed a ticket internally (OKTA-357710) to get this header accessible in CORS requests.

Let me know if there are any other headers returned by Okta that you would want available when making AJAX requests so I can add it to the list of headers we should return to support this use case.

1 Like

Okay thanks, that sounds great! So far, the link header was the only one I needed.
However, please make sure that the link header will not only be available for the /api/v1/users endpoint but also for other endpoints that support pagination, such as /api/v1/groups/${groupId}/users.
Can you already estimate when this ticket will be done?

Hi @fes,

If you are asking about the ticket to add link headers for pagination for the cors enabled endpoints, we do not have an estimation yet.

If you need it to be prioritized, please create a Support case to Okta.

1 Like

This topic was automatically closed 24 hours after the last reply. New replies are no longer allowed.