@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 +115 -10
- package/account-ui/assets/index-5oloT5kb.js +17 -0
- package/account-ui/assets/index-DEedXv4r.css +1 -0
- package/account-ui/index.js +2 -2
- package/native-client.js +4 -1
- package/package.json +1 -1
- package/account-ui/assets/index-DM1YaOig.css +0 -1
- package/account-ui/assets/index-DdKBGKhQ.js +0 -17
package/README.md
CHANGED
|
@@ -1,30 +1,135 @@
|
|
|
1
|
-
#
|
|
1
|
+
# @authlor/browser
|
|
2
2
|
|
|
3
|
-
The
|
|
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
|
|
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
|
-
|
|
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
|
-
|
|
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
|
-
|
|
120
|
+
## Troubleshooting
|
|
23
121
|
|
|
24
|
-
|
|
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
|
-
|
|
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
|
-
|
|
133
|
+
## License
|
|
29
134
|
|
|
30
|
-
|
|
135
|
+
[MIT](./LICENSE) © Vyslor. Bundled dependencies and fonts retain their own licenses; see `account-ui/THIRD_PARTY_NOTICES.txt`.
|