-
Notifications
You must be signed in to change notification settings - Fork 46
docs(auth): standardise auth endpoint examples on /api/auth/token #3397
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
base: main
Are you sure you want to change the base?
Changes from 2 commits
34fe6eb
1a09319
0932bd4
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -125,16 +125,16 @@ final tokenRequest = rest.auth.createTokenRequest(tokenParams: tokenParams); | |
| ``` | ||
| </Code> | ||
|
|
||
| ### Client usage | ||
| ### Client usage <a id="client-usage"/> | ||
|
|
||
| The client SDK automatically handles TokenRequests returned from your auth endpoint: | ||
| The client SDK automatically handles TokenRequests returned from your auth endpoint. These examples use `authCallback`, but `authUrl` is also valid; see [choosing how the client fetches tokens](/docs/auth/token#auth-mechanism) for guidance on which to use. | ||
|
|
||
| <Code> | ||
| ```realtime_javascript | ||
| const realtime = new Ably.Realtime({ | ||
| authCallback: async (tokenParams, callback) => { | ||
| try { | ||
| const response = await fetch('/api/ably-token'); | ||
| const response = await fetch('/api/auth/token'); | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
|
||
| const tokenRequest = await response.json(); | ||
| callback(null, tokenRequest); | ||
| } catch (error) { | ||
|
|
@@ -148,7 +148,7 @@ const realtime = new Ably.Realtime({ | |
| const realtime = new Ably.Realtime({ | ||
| authCallback: async (tokenParams, callback) => { | ||
| try { | ||
| const response = await fetch('/api/ably-token'); | ||
| const response = await fetch('/api/auth/token'); | ||
| const tokenRequest = await response.json(); | ||
| callback(null, tokenRequest); | ||
| } catch (error) { | ||
|
|
@@ -163,7 +163,7 @@ import aiohttp | |
|
|
||
| async def get_token_request(*args, **kwargs): | ||
| async with aiohttp.ClientSession() as session: | ||
| async with session.get('/api/ably-token') as response: | ||
| async with session.get('/api/auth/token') as response: | ||
| if response.status != 200: | ||
| raise Exception(f"Auth failed: {response.status}") | ||
| return await response.json() | ||
|
|
@@ -340,7 +340,7 @@ The client code for Ably Token direct is the same as for TokenRequest — the SD | |
| const realtime = new Ably.Realtime({ | ||
| authCallback: async (tokenParams, callback) => { | ||
| try { | ||
| const response = await fetch('/api/ably-token'); | ||
| const response = await fetch('/api/auth/token'); | ||
| const tokenDetails = await response.json(); | ||
| callback(null, tokenDetails); | ||
| } catch (error) { | ||
|
|
@@ -354,7 +354,7 @@ const realtime = new Ably.Realtime({ | |
| const realtime = new Ably.Realtime({ | ||
| authCallback: async (tokenParams, callback) => { | ||
| try { | ||
| const response = await fetch('/api/ably-token'); | ||
| const response = await fetch('/api/auth/token'); | ||
| const tokenDetails = await response.json(); | ||
| callback(null, tokenDetails); | ||
| } catch (error) { | ||
|
|
@@ -369,7 +369,7 @@ import aiohttp | |
|
|
||
| async def get_ably_token(*args, **kwargs): | ||
| async with aiohttp.ClientSession() as session: | ||
| async with session.get('/api/ably-token') as response: | ||
| async with session.get('/api/auth/token') as response: | ||
| if response.status != 200: | ||
| raise Exception(f"Auth failed: {response.status}") | ||
| return await response.json() | ||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -16,7 +16,7 @@ Token authentication is the recommended authentication method to use client-side | |
| SDKs for [Chat](/docs/chat) and [Spaces](/docs/spaces) products rely on the underlying Pub/Sub library for their connection and authentication, so there is no need to configure auth separately in those SDKs. | ||
| </Aside> | ||
|
|
||
| ## Choosing a token mechanism <a id="choosing"/> | ||
| ## Choosing a token format <a id="choosing"/> | ||
|
|
||
| Ably supports two token formats: JWT, the primary and recommended format, and Ably tokens, a legacy format. Each format is associated with a different flow for issuing tokens to clients. | ||
|
|
||
|
|
@@ -68,6 +68,21 @@ Ably enforces maximum TTL (time-to-live) limits: | |
| Attempting to create a token with a TTL that exceeds these limits will result in an error code [40003](/docs/platform/errors/codes#40003). | ||
| </Aside> | ||
|
|
||
| ## Choosing how the client fetches tokens <a id="auth-mechanism"/> | ||
|
|
||
| `authUrl` and `authCallback` are the two ways a client fetches a token from your server. Both apply to any token format, both are current, and both refresh tokens automatically before expiry. Choose based on the client environment and how much control you need over the request. | ||
|
umair-ably marked this conversation as resolved.
Outdated
|
||
|
|
||
| | | `authUrl` | `authCallback` | | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. There's also the |
||
| | --- | --- | --- | | ||
| | How it works | The SDK makes an HTTP request to a URL you specify and uses the response as the token. | The SDK calls a function you provide, which returns the token. | | ||
| | Best for | Web clients that can pass cookies automatically to your auth endpoint. | Native and non-web clients, or any case where you need more control. | | ||
|
umair-ably marked this conversation as resolved.
Outdated
|
||
| | Control over the request | Limited to `authMethod`, `authHeaders`, and `authParams` [auth options](/docs/api/realtime-sdk/types#auth-options). | Full: run any logic to obtain the token, set custom headers, and handle errors. | | ||
| | Setup | A single client option pointing at your endpoint. | A function in your client code. | | ||
|
|
||
| Use `authUrl` when a simple HTTP request to your server returns the token, particularly for web clients where the browser sends session cookies automatically. Reach for `authCallback` when the token fetch is non-trivial, for example retrieving the token from native storage, adding custom authorization headers, or handling errors yourself. `authCallback` is also the option for non-web clients, though native SDKs support `authUrl` as well. | ||
|
umair-ably marked this conversation as resolved.
Outdated
|
||
|
|
||
| See [JWTs](/docs/auth/token/jwt#client-setup) and [Ably Tokens](/docs/auth/token/ably-tokens#client-usage) for per-format client setup examples using each mechanism. | ||
|
|
||
| ## Dynamic channel access control <a id="dynamic-access"/> | ||
|
|
||
| Token authentication allows you to dynamically change a client's channel access permissions without disconnecting. Use the [`authorize()`](/docs/api/realtime-sdk/authentication#authorize) method to re-authenticate with updated [capabilities](/docs/auth/capabilities). | ||
|
|
||
Uh oh!
There was an error while loading. Please reload this page.