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

Add "How Clerk works" and "Client handshake" to initial concepts section #577

Draft
wants to merge 45 commits into
base: main
Choose a base branch
from

Conversation

rachelnabors
Copy link
Contributor

@rachelnabors rachelnabors commented Dec 19, 2023

We have an internal copy of this document we have talked about moving externally for some time. This is how that could look:

https://docs-preview-577.clerkpreview.com/docs/concepts/how-clerk-works
https://docs-preview-577.clerkpreview.com/docs/concepts/client-handshake

Related to PR-867 in the marketing repo which adds a lightbulb icon for the "Concepts" section in the sidenav.

Copy link

github-actions bot commented Dec 19, 2023

Hey @alexisintech, your docs preview is available.

Status Preview Updated (UTC)
🍪 Updated Visit preview Feb 05, 2024 04:10 PM

@alexisintech
Copy link
Member

I'll provide my review once the information is reviewed by an engineer more familiar with this information and is deemed technically correct :)

@rachelnabors
Copy link
Contributor Author

Updated based on @SokratisVidros's feedback in the deploy preview. @alexisintech please do an editorial sweep and merge.

docs/concepts/client-handshake.mdx Outdated Show resolved Hide resolved
docs/concepts/client-handshake.mdx Show resolved Hide resolved
docs/concepts/client-handshake.mdx Outdated Show resolved Hide resolved
docs/concepts/client-handshake.mdx Outdated Show resolved Hide resolved
docs/concepts/client-handshake.mdx Outdated Show resolved Hide resolved
docs/concepts/client-handshake.mdx Outdated Show resolved Hide resolved
docs/concepts/client-handshake.mdx Outdated Show resolved Hide resolved
rachelnabors and others added 4 commits December 22, 2023 14:50
Co-authored-by: Sokratis Vidros <SokratisVidros@users.noreply.github.com>
Co-authored-by: Sokratis Vidros <SokratisVidros@users.noreply.github.com>
docs/concepts/client-handshake.mdx Outdated Show resolved Hide resolved
docs/concepts/client-handshake.mdx Outdated Show resolved Hide resolved
docs/concepts/client-handshake.mdx Outdated Show resolved Hide resolved
docs/concepts/client-handshake.mdx Outdated Show resolved Hide resolved
docs/concepts/client-handshake.mdx Outdated Show resolved Hide resolved
docs/concepts/how-clerk-works.mdx Outdated Show resolved Hide resolved
docs/concepts/how-clerk-works.mdx Outdated Show resolved Hide resolved
docs/concepts/how-clerk-works.mdx Outdated Show resolved Hide resolved
docs/manifest.json Outdated Show resolved Hide resolved
docs/manifest.json Outdated Show resolved Hide resolved
docs/concepts/client-handshake.mdx Outdated Show resolved Hide resolved
docs/concepts/client-handshake.mdx Outdated Show resolved Hide resolved
docs/concepts/how-clerk-works.mdx Outdated Show resolved Hide resolved
docs/concepts/how-clerk-works.mdx Show resolved Hide resolved
docs/concepts/client-handshake.mdx Show resolved Hide resolved
docs/concepts/client-handshake.mdx Outdated Show resolved Hide resolved
docs/concepts/client-handshake.mdx Outdated Show resolved Hide resolved
docs/concepts/client-handshake.mdx Outdated Show resolved Hide resolved
docs/concepts/client-handshake.mdx Outdated Show resolved Hide resolved
docs/concepts/client-handshake.mdx Outdated Show resolved Hide resolved
### Handshake payload

The handshake payload is a signed JWT that contains an array of `set-cookie` header directives. This allows us to transfer cookies from the FAPI domain to the application’s domain across environments without additional cookie setting logic embedded into our SDKs. In development, the payload is transported in the URL in a `__clerk_handshake` query parameter. In production, the payload is transported in a `__clerk_handshake` cookie. On returning from the handshake endpoint, the handshake payload is parsed and the cookie directives are set on the final response.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@BRKalow Please add an example of a Handshake payload.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@alexisintech Please add the following example of the handshake payload:

  "handshake": [
    "__client_uat=; Path=/; Expires=Thu, 01 Jan 1970 00:00:00 GMT",
    "__clerk_handshake=; Path=/; Domain=example.com; Expires=Thu, 01 Jan 1970 00:00:00 GMT",
    "__client_uat=1706822359; Path=/; Domain=example.com; Max-Age=315360000; Secure; SameSite=Lax",
    "__session=<...CLERK_SESSION_JWT...>; Path=/; Expires=Wed, 05 Feb 2025 09:08:53 GMT; Secure"
  ]
}```

The `__session` cookie is set on the host application that is secured by Clerk (for example `https://dashboard.<example.com>`). It’s a JS cookie, it’s secure, and it contains a short-lived session JWT that lasts 60 seconds. This JWT contains the current session, user, and organization identifiers and must be sent to the application API to authenticate API requests. In SSR, the `__session` cookie travels automatically to the server. ClerkJS ensures that the `__session` cookie is automatically refreshed in Client Side Rendering (CSR) and Server Side Rendering (SSR), ensuring a fresh session is always available.

However, if a `__session` cookie is expired on a request to an application’s backend, the SDK doesn’t know if the session has ended, or if a new short-lived JWT needs to be issued. When an SDK gets into this state, it triggers Clerk's client handshake.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The __client_uat cookie

This __client_uat cookie is set on the host application that is secured by Clerk and hints Clerk SDKs about the timestamp of the latest authentication update of the current client.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@alexisintech Please add this ☝️

docs/concepts/how-clerk-works.mdx Show resolved Hide resolved
docs/concepts/how-clerk-works.mdx Outdated Show resolved Hide resolved
docs/concepts/client-handshake.mdx Outdated Show resolved Hide resolved
### Handshake payload

The handshake payload is a signed JWT that contains an array of `set-cookie` header directives. This allows us to transfer cookies from the FAPI domain to the application’s domain across environments without additional cookie setting logic embedded into our SDKs. In development, the payload is transported in the URL in a `__clerk_handshake` query parameter. In production, the payload is transported in a `__clerk_handshake` cookie. On returning from the handshake endpoint, the handshake payload is parsed and the cookie directives are set on the final response.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@alexisintech Please add the following example of the handshake payload:

  "handshake": [
    "__client_uat=; Path=/; Expires=Thu, 01 Jan 1970 00:00:00 GMT",
    "__clerk_handshake=; Path=/; Domain=example.com; Expires=Thu, 01 Jan 1970 00:00:00 GMT",
    "__client_uat=1706822359; Path=/; Domain=example.com; Max-Age=315360000; Secure; SameSite=Lax",
    "__session=<...CLERK_SESSION_JWT...>; Path=/; Expires=Wed, 05 Feb 2025 09:08:53 GMT; Secure"
  ]
}```

The `__session` cookie is set on the host application that is secured by Clerk (for example `https://dashboard.<example.com>`). It’s a JS cookie, it’s secure, and it contains a short-lived session JWT that lasts 60 seconds. This JWT contains the current session, user, and organization identifiers and must be sent to the application API to authenticate API requests. In SSR, the `__session` cookie travels automatically to the server. ClerkJS ensures that the `__session` cookie is automatically refreshed in Client Side Rendering (CSR) and Server Side Rendering (SSR), ensuring a fresh session is always available.

However, if a `__session` cookie is expired on a request to an application’s backend, the SDK doesn’t know if the session has ended, or if a new short-lived JWT needs to be issued. When an SDK gets into this state, it triggers Clerk's client handshake.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@alexisintech Please add this ☝️

docs/concepts/how-clerk-works.mdx Outdated Show resolved Hide resolved
@alexisintech alexisintech marked this pull request as draft February 12, 2024 16:25
@alexisintech
Copy link
Member

this PR is waiting on @kylemac and the decisions surrounding the new docs IA - converting back to draft for now.

@LekoArts LekoArts mentioned this pull request Aug 9, 2024
@alexisintech alexisintech changed the title Adding "How Clerk works" to initial concepts section Add "How Clerk works" and "Client handshake" to initial concepts section Sep 12, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

6 participants