@authlor/browser 0.3.0 → 0.4.1

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 CHANGED
@@ -1,30 +1,135 @@
1
- # Authlor browser SDK by Vyslor
1
+ # @authlor/browser
2
2
 
3
- The prepared version 0.3.0 targets public npm distribution as `@authlor/browser` under the MIT license. It is not yet published; namespace ownership must be finalized first. Once published, installation needs no registry token. Bundled dependencies retain their original licenses and notices.
3
+ The official Authlor browser SDK. Add sign-in, session management, and an account interface to your application.
4
+
5
+ ## Installation
4
6
 
5
7
  ```sh
6
- npm install @authlor/browser@0.3.0
8
+ npm install @authlor/browser
7
9
  ```
8
10
 
11
+ Use [`@authlor/node`](https://www.npmjs.com/package/@authlor/node) on your Express backend to register the application and verify authenticated requests. Use matching browser and Node SDK versions.
12
+
13
+ For Next.js App Router, install [`@authlor/nextjs`](https://www.npmjs.com/package/@authlor/nextjs) for the React provider, hooks, and server integration.
14
+
15
+ ## Quick start
16
+
17
+ In the Authlor console, select your instance, add your frontend origin under **Domains → Allowed Origins**, and copy its publishable key from **API keys**.
18
+
9
19
  ```js
10
20
  import { createAuthlorClient } from '@authlor/browser';
11
21
 
12
22
  const auth = createAuthlorClient({ publishableKey: 'pk_test_...' });
13
23
  await auth.load();
24
+
25
+ const unsubscribe = auth.subscribe(({ signedIn, user }) => {
26
+ document.querySelector('#account-name').textContent =
27
+ signedIn ? (user?.name ?? 'Account') : 'Guest';
28
+ });
29
+
14
30
  document.querySelector('#sign-in').onclick = () => auth.redirectToSignIn();
15
31
  document.querySelector('#account').onclick = () => auth.openUserProfile();
32
+ document.querySelector('#sign-out').onclick = () => auth.signOut();
16
33
  ```
17
34
 
18
- Use `getToken()` before authenticated requests, `subscribe()` for session changes and `signOut()` to end the session. `mountUserProfile(element)` uses your own container. Close with `closeUserProfile()` or `unmountUserProfile()`; call `destroy()` when disposing the client. The UI code, styles and fonts are pinned inside this package and load on first opening. No hosted manifest selects executable code. Shadow DOM provides style isolation, not a security sandbox.
35
+ Call `load()` before using the client. It completes authentication callbacks and restores the current session. Keep the secret API key on your backend; the browser client uses only the publishable key.
36
+
37
+ ## Configuration
38
+
39
+ | Option | Description |
40
+ | --- | --- |
41
+ | `publishableKey` | Required. The publishable key for your Authlor instance. |
42
+ | `baseUrl` | Self-hosted Authlor service origin. Omit to use hosted Authlor. |
43
+ | `hosted` | Enables the hosted authentication form client. Defaults to `false`; application integrations should use the default. |
44
+
45
+ Development supports HTTP on loopback origins such as `http://localhost:3000`. Staging and Production require HTTPS. Production also requires a verified accounts domain on the same site as the application, such as `accounts.example.com` for `app.example.com`.
46
+
47
+ The SDK discovers account pages from the publishable key. For self-hosted Authlor, configure the same `baseUrl` in both browser and Node SDKs.
48
+
49
+ ## Authenticated requests
50
+
51
+ Call `getToken()` before sending a request to a protected backend route:
52
+
53
+ ```js
54
+ const token = await auth.getToken();
55
+ if (!token) {
56
+ await auth.redirectToSignIn();
57
+ } else {
58
+ const response = await fetch('/api/me', {
59
+ headers: { Authorization: `Bearer ${token}` },
60
+ });
61
+ }
62
+ ```
63
+
64
+ The client refreshes session tokens automatically and coalesces simultaneous refresh requests. Session recovery runs after page reloads, tab suspension, and network reconnection. Your backend must verify tokens and enforce application permissions.
65
+
66
+ For separate frontend and backend origins, use bearer tokens and configure the backend's CORS policy for the exact frontend origin.
67
+
68
+ ## Account interface
69
+
70
+ Open an account modal over the current page:
71
+
72
+ ```js
73
+ await auth.openUserProfile();
74
+ // Close it when needed:
75
+ auth.closeUserProfile();
76
+ ```
77
+
78
+ To display the profile in your own container:
79
+
80
+ ```js
81
+ await auth.mountUserProfile(document.querySelector('#profile'));
82
+ // Remove it when the container is disposed:
83
+ auth.unmountUserProfile();
84
+ ```
85
+
86
+ The interface loads on first use and includes profile editing, pictures, usernames, phone numbers, passwords, connected accounts, MFA, backup codes, passkeys, devices, and account deletion, subject to instance settings. Account switching is available when multiple sessions are enabled.
87
+
88
+ UI scripts, styles, and fonts are included in the package. Shadow DOM scopes styles to the account interface. Preserve the package's lazy imports and asset URLs in your frontend build.
89
+
90
+ Social linking, passkey enrollment, and reverification may open a popup on the accounts domain. Allow popups for these actions. Opening the profile without an active session starts sign-in and reopens the profile on return.
91
+
92
+ ## API reference
93
+
94
+ These methods apply to the default application client (`hosted: false`):
95
+
96
+ | Member | Purpose |
97
+ | --- | --- |
98
+ | `load()` | Initialize the client, process a callback, and restore the session. |
99
+ | `user` | Current user (`id`, `name`, and optional email value), or `null`. |
100
+ | `getToken()` | Return a current session token, or `null` when signed out. |
101
+ | `subscribe(listener)` | Receive session changes. Returns an unsubscribe function. |
102
+ | `redirectToSignIn(options?)` | Open hosted sign-in. Supports `returnTo` and `addAccount`. |
103
+ | `signOut()` | Sign out of the current session. |
104
+ | `openUserProfile()` | Load and open the account modal. |
105
+ | `closeUserProfile()` | Close the account modal. |
106
+ | `mountUserProfile(element)` | Render the account interface in a container. |
107
+ | `unmountUserProfile()` | Remove the mounted account interface. |
108
+ | `destroy()` | Release client timers, event listeners, and subscriptions. |
109
+
110
+ Call the unsubscribe function when a component stops listening. Call `destroy()` when disposing the client, such as when its application root unmounts.
111
+
112
+ For custom authentication forms, the client also provides sign-in, signup, email and SMS verification, password recovery, and MFA methods. Available methods depend on the instance's authentication policy. See the Authlor console Docs for flow examples and the package's TypeScript declarations for method signatures.
113
+
114
+ ## Session behavior
115
+
116
+ Development and Staging use an origin-bound browser credential in session storage. Production uses a Secure, host-only, HttpOnly cookie on the accounts domain. Keep session storage available in non-production environments.
19
117
 
20
- Pair this version with `@authlor/node@0.3.0`. HTTPS session cookies use the protected `__Host-` prefix; upgrade both packages together. The unversioned hosted module remains available for existing integrations; new applications use `/sdk/v0.3/authlor.js` or this package.
118
+ Account changes and signout synchronize across tabs. A tab with an invalidated browser credential may require a new sign-in handshake. Session renewal alone does not extend the instance's inactivity timeout.
21
119
 
22
- Native account switching requires both the account capability and the current renewal credential. The client supplies the HttpOnly cookie in Production, or the non-production renewal header only on the switch request, then saves the replacement before renewal. A copied account capability alone cannot obtain a long-lived credential.
120
+ ## Troubleshooting
23
121
 
24
- Authlor owns sessions centrally. The backend SDK automatically registers the callback and never accesses your application's database. Configure **Allowed Origins** in Authlor. Publishable keys are public identifiers; only the server receives the secret key. Credentials and account changes go directly to Authlor.
122
+ | Problem | Check |
123
+ | --- | --- |
124
+ | Instance discovery fails | Check the publishable key, network access, and self-hosted `baseUrl`, if configured. |
125
+ | Sign-in cannot return to the application | Check Allowed Origins and the Node SDK's canonical frontend and backend URLs. Use `localhost` or `127.0.0.1` consistently. |
126
+ | A handshake expires | Offer an explicit sign-in retry. Avoid automatic redirect loops. |
127
+ | The account interface does not load | Preserve packaged assets and lazy imports. Check the application's Content Security Policy for required scripts, styles, fonts, and Authlor API and image connections. |
128
+ | A popup does not open | Allow popups for account linking, passkey enrollment, and reverification. |
129
+ | The accounts hostname changes | Restart the Node SDK backend, reload the page, and sign in again. Existing passkeys remain bound to their enrollment hostname. |
25
130
 
26
- Development and Staging discover their managed accounts domains automatically and use a session-storage client credential. It rotates on login and account selection, is revoked on signout and is cleared after definitive invalidation. Account switching saves the replacement before renewal. Other tabs sharing the replaced client clear stale state and perform a fresh handshake; secrets are not broadcast. Production discards stored credentials, ignores any redemption credential field and sends no client-credential header; renewal and account switching use the Authlor HttpOnly cookie. Local Authlor uses a loopback `baseUrl` in both SDKs. Staging and Production require HTTPS; Production also requires a verified same-site accounts domain. After changing a hostname, restart the SDK backend, reload clients and sign in again. Existing passkeys keep their original RP ID.
131
+ Session tokens are accessible to application JavaScript. Protect the host page against XSS and never log tokens, cookies, verification codes, or account credentials.
27
132
 
28
- Social navigation is limited to this release's supported provider authorization URLs. Completion permits registered callbacks, configured application destinations and the specific Authlor flow routes. Reject unexpected destinations rather than following them. The server also enforces redirect policy for top-level form flows.
133
+ ## License
29
134
 
30
- Public source is not an authorization boundary. Your backend must verify every protected request and enforce its own resource permissions. Keep a restrictive host-page CSP and protect against XSS; browser tokens are accessible to application JavaScript. Never log credentials, account capabilities or authorization headers. See the Authlor console Docs for the full installation and security guide.
135
+ [MIT](./LICENSE) © Vyslor. Bundled dependencies and fonts retain their own licenses; see `account-ui/THIRD_PARTY_NOTICES.txt`.