Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

ListMySessions API pagination returns duplicate results #3883

Open
3 of 5 tasks
aryaniyaps opened this issue Apr 18, 2024 · 0 comments
Open
3 of 5 tasks

ListMySessions API pagination returns duplicate results #3883

aryaniyaps opened this issue Apr 18, 2024 · 0 comments
Labels
bug Something is not working.

Comments

@aryaniyaps
Copy link

aryaniyaps commented Apr 18, 2024

Preflight checklist

Ory Network Project

No response

Describe the bug

I have been trying to use the Ory Kratos Frontend API to list sessions and manage them in my Next.JS frontend application.

I wanted to utilize the newly added token based pagination API, but found out that the frontend listMySessions API returned duplicate results, even when the pageToken value changed.

kratos frontend API client initialization:

import { Configuration, FrontendApi } from "@ory/kratos-client";
import { env } from "./env";

const kratos = new FrontendApi(
  new Configuration({
    basePath: env.NEXT_PUBLIC_KRATOS_PUBLIC_URL,
    baseOptions: { withCredentials: true },
  })
);

Im using react-query for data management on the client side

import { useSuspenseInfiniteQuery } from "@tanstack/react-query";
import parseLinkHeader from "parse-link-header";
import { DEFAULT_PAGE_SIZE } from "../constants";
import kratos from "../kratos";

export default function useSessions() {
  return useSuspenseInfiniteQuery({
    queryKey: ["/sessions"],
    queryFn: async ({ pageParam }) => {
      const { data, headers } = await kratos.listMySessions({
        pageSize: DEFAULT_PAGE_SIZE,
        pageToken: pageParam,
      });

      const linkHeader = headers.link;

      const links = parseLinkHeader(linkHeader);

      return {
        sessions: data,
        nextPageToken: links?.next?.page_token
      };
    },
    initialPageParam: undefined as string | undefined,
    getNextPageParam: (lastPage) => lastPage.nextPageToken,
  });
}

essentially, what is happening is when I call

      const { data, headers } = await kratos.listMySessions({
        pageSize: DEFAULT_PAGE_SIZE,
        pageToken: undefined, // page token defaults to "1" as per the API
      });

and when I'm calling

      const { data, headers } = await kratos.listMySessions({
        pageSize: DEFAULT_PAGE_SIZE,
        pageToken: "XYZ", // assuming that `XYZ` was the next page token
      });

I get the same results i.e. the same list of sessions when using both calls.

Reproducing the bug

  1. Run The ory kratos quickstart using docker compose:
    https://github.com/ory/kratos/blob/master/quickstart-latest.yml
  2. create a number of sessions (say 6) for a particular identity.
  3. fetch the first page of sessions for the identity using the following curl command:
    curl -X GET "http://localhost:4433/sessions?per_page=5"
    
  4. retrieve the link header from the response and get the next page token, XYZ
  5. fetch the next page of sessions for the identity using the following curl command:
    curl -X GET "http://localhost:4433/sessions?per_page=5&page_token=XYZ"
    
  6. We can infer that both the commands return the same set of results

Relevant log output

No response

Relevant configuration

No response

Version

v1.1.0

On which operating system are you observing this issue?

Windows

In which environment are you deploying?

Docker Compose

Additional Context

No response

@aryaniyaps aryaniyaps added the bug Something is not working. label Apr 18, 2024
@aryaniyaps aryaniyaps changed the title ListMySessions API pagination is broken ListMySessions API pagination returns duplicate results Apr 18, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something is not working.
Projects
None yet
Development

No branches or pull requests

1 participant