Skip to content

Commit

Permalink
add a section on testing flow for getting valid session tokens
Browse files Browse the repository at this point in the history
  • Loading branch information
jescalan committed Dec 19, 2024
1 parent d43786b commit c456dc4
Showing 1 changed file with 11 additions and 0 deletions.
11 changes: 11 additions & 0 deletions docs/testing/overview.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -24,4 +24,15 @@ Once retrieved, include the token value in the `__clerk_testing_token` query par
POST https://happy-hippo-1.clerk.accounts.dev/v1/client/sign_ups?__clerk_testing_token=1713877200-c_2J2MvPu9PnXcuhbPZNao0LOXqK9A7YrnBn0HmIWxy
```

## Test flow for getting a valid session token

When writing tests using Clerk, if you need to get a valid session token, you can use the following flow:

1. If you have not already, [create a new user](https://clerk.com/docs/reference/backend-api/tag/Users#operation/CreateUser).
2. [Create a new session](https://clerk.com/docs/reference/backend-api/tag/Sessions#operation/createSession) for the user.
3. [Create a session token](https://clerk.com/docs/reference/backend-api/tag/Sessions#operation/CreateSessionToken) using the session ID returned in the previous step.
4. Pass the returned session token as the value of an Authorization header to any other requests you're making, as such: `Authorization: Bearer <session_token>`.

Note that Clerk's session tokens are short-lived and are valid only for 60 seconds (learn more about why this is the case in [our "how clerk works" guide](/docs/how-clerk-works-overview)), if the session token expires, you will need to refresh it with the same [create session token endpoint](https://clerk.com/docs/reference/backend-api/tag/Sessions#operation/CreateSessionToken). The most common ways to do this are to either hit this endpoint before every test to ensure you have a valid session token, or to run an interval timer that refreshes the token before it expires.

For more information, feedback or issues, visit the [`@clerk/testing`](https://github.com/clerk/javascript/tree/main/packages/testing) package.

0 comments on commit c456dc4

Please sign in to comment.