@auth0/auth0-server-js 1.15.0 → 1.16.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +38 -0
- package/dist/index.cjs +501 -22
- package/dist/index.cjs.map +1 -1
- package/dist/index.d.cts +519 -59
- package/dist/index.d.ts +519 -59
- package/dist/index.js +479 -7
- package/dist/index.js.map +1 -1
- package/package.json +2 -2
package/README.md
CHANGED
|
@@ -22,6 +22,8 @@ Using this SDK as-is in your application may not be trivial, as it is designed t
|
|
|
22
22
|
- [Stateful Store](#stateful-store)
|
|
23
23
|
- [4. Add login to your Application (interactive)](#4-add-login-to-your-application-interactive)
|
|
24
24
|
- [5. Add logout to your application](#5-add-logout-to-your-application)
|
|
25
|
+
- [6. Database Connections (Sign-up & Change Password)](#6-database-connections-sign-up--change-password)
|
|
26
|
+
- [7. Anonymous Sessions](#7-anonymous-sessions)
|
|
25
27
|
|
|
26
28
|
### 1. Install the SDK
|
|
27
29
|
|
|
@@ -298,6 +300,42 @@ try {
|
|
|
298
300
|
|
|
299
301
|
For full options and error handling, see the [auth0-auth-js Database Connections examples](https://github.com/auth0/auth0-auth-js/blob/main/packages/auth0-auth-js/examples/database-connections.md) (the underlying database client is identical) and the runnable [`examples/database-conns`](https://github.com/auth0/auth0-auth-js/tree/main/examples/database-conns) sample.
|
|
300
302
|
|
|
303
|
+
### 7. Anonymous Sessions
|
|
304
|
+
|
|
305
|
+
The `ServerClient` exposes an `anonymous` sub-client that gives a visitor an identity, and an access token for your API, before they log in. Configure an `anonymousStore` to switch it on:
|
|
306
|
+
|
|
307
|
+
```ts
|
|
308
|
+
import { StatelessAnonymousStore, ServerClient } from '@auth0/auth0-server-js';
|
|
309
|
+
|
|
310
|
+
const auth0 = new ServerClient<StoreOptions>({
|
|
311
|
+
// ...
|
|
312
|
+
anonymousStore: new StatelessAnonymousStore({ secret: options.secret }, new FastifyCookieHandler()),
|
|
313
|
+
});
|
|
314
|
+
|
|
315
|
+
// Give the visitor an anonymous identity and a token for your API.
|
|
316
|
+
const tokenSet = await auth0.anonymous.createSession(
|
|
317
|
+
{ audience: '<AUTH0_AUDIENCE>', metadata: { campaign: 'summer-sale' } },
|
|
318
|
+
{ request, reply }
|
|
319
|
+
);
|
|
320
|
+
|
|
321
|
+
// Later requests reuse the cached token, or mint a new one when it expired.
|
|
322
|
+
const { accessToken } = await auth0.anonymous.getAccessToken({ audience: '<AUTH0_AUDIENCE>' }, { request, reply });
|
|
323
|
+
```
|
|
324
|
+
|
|
325
|
+
The anonymous session is kept in its own store, so `getSession()` and `getUser()` keep returning `undefined` until the visitor really logs in.
|
|
326
|
+
|
|
327
|
+
Call `createSession()` only once `auth0.anonymous.getSession()` shows the visitor has no session yet. `getAccessToken()` serves cached tokens and only calls Auth0 when there is no cached token for the requested audience and scope, or when the cached one has expired.
|
|
328
|
+
|
|
329
|
+
Logging in and logging out both end the anonymous session for you. Every login method drops it once the user session is written, and `auth0.logout()` clears it as well. Read `auth0.anonymous.getSession()` **before** you complete the login when you need the anonymous `sub` to merge data into the user's account, or set `clearAnonymousSessionOnLogin: false` to keep the session and clear it yourself.
|
|
330
|
+
|
|
331
|
+
> [!NOTE]
|
|
332
|
+
> Anonymous Sessions are in Early Access and have to be enabled on your tenant.
|
|
333
|
+
|
|
334
|
+
> [!NOTE]
|
|
335
|
+
> `startInteractiveLogin()` automatically links an active anonymous session to the user at login via a Session Transfer Ticket. For login methods that do not go through `/authorize` (`passkey.getToken()`, `completePasswordless()`, `loginBackchannel()`, and similar), the link is unavailable — do the merge in your own application with the anonymous `sub`.
|
|
336
|
+
|
|
337
|
+
For the full API, the store options, the clearing behaviour and the details on linking an anonymous session to the user created at login, see the [Anonymous Sessions section in EXAMPLES.md](https://github.com/auth0/auth0-auth-js/blob/main/packages/auth0-server-js/EXAMPLES.md#anonymous-sessions).
|
|
338
|
+
|
|
301
339
|
## Feedback
|
|
302
340
|
|
|
303
341
|
### Contributing
|