@lunora/react-native 0.0.0 → 1.0.0-alpha.2
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/LICENSE.md +111 -0
- package/README.md +198 -28
- package/__assets__/package-og.svg +14 -0
- package/dist/auth.d.mts +35 -0
- package/dist/auth.d.ts +35 -0
- package/dist/auth.mjs +2 -0
- package/dist/index.d.mts +75 -0
- package/dist/index.d.ts +75 -0
- package/dist/index.mjs +2 -0
- package/dist/packem_shared/createLunoraClient-D9i-IKbc.mjs +36 -0
- package/dist/packem_shared/expoBearerToken-B0Zorz--.mjs +7 -0
- package/package.json +70 -7
package/LICENSE.md
ADDED
|
@@ -0,0 +1,111 @@
|
|
|
1
|
+
# Functional Source License, Version 1.1, Apache 2.0 Future License
|
|
2
|
+
|
|
3
|
+
## Abbreviation
|
|
4
|
+
|
|
5
|
+
FSL-1.1-Apache-2.0
|
|
6
|
+
|
|
7
|
+
## Notice
|
|
8
|
+
|
|
9
|
+
Copyright 2026 anolilab and contributors
|
|
10
|
+
|
|
11
|
+
## Terms and Conditions
|
|
12
|
+
|
|
13
|
+
### Licensor ("We")
|
|
14
|
+
|
|
15
|
+
The party offering the Software under these Terms and Conditions.
|
|
16
|
+
|
|
17
|
+
### The Software
|
|
18
|
+
|
|
19
|
+
The "Software" is each version of the software that we make available under
|
|
20
|
+
these Terms and Conditions, as indicated by our inclusion of these Terms and
|
|
21
|
+
Conditions with the Software.
|
|
22
|
+
|
|
23
|
+
### License Grant
|
|
24
|
+
|
|
25
|
+
Subject to your compliance with this License Grant and the Patents,
|
|
26
|
+
Redistribution and Trademark clauses below, we hereby grant you the right to
|
|
27
|
+
use, copy, modify, create derivative works, publicly perform, publicly display
|
|
28
|
+
and redistribute the Software for any Permitted Purpose identified below.
|
|
29
|
+
|
|
30
|
+
### Permitted Purpose
|
|
31
|
+
|
|
32
|
+
A Permitted Purpose is any purpose other than a Competing Use. A Competing Use
|
|
33
|
+
means making the Software available to others in a commercial product or service
|
|
34
|
+
that:
|
|
35
|
+
|
|
36
|
+
1. substitutes for the Software;
|
|
37
|
+
|
|
38
|
+
2. substitutes for any other product or service we offer using the Software that
|
|
39
|
+
exists as of the date we make the Software available; or
|
|
40
|
+
|
|
41
|
+
3. offers the same or substantially similar functionality as the Software.
|
|
42
|
+
|
|
43
|
+
Permitted Purposes specifically include using the Software:
|
|
44
|
+
|
|
45
|
+
1. for your internal use and access;
|
|
46
|
+
|
|
47
|
+
2. for non-commercial education;
|
|
48
|
+
|
|
49
|
+
3. for non-commercial research; and
|
|
50
|
+
|
|
51
|
+
4. in connection with professional services that you provide to a licensee using
|
|
52
|
+
the Software in accordance with these Terms and Conditions.
|
|
53
|
+
|
|
54
|
+
### Patents
|
|
55
|
+
|
|
56
|
+
To the extent your use for a Permitted Purpose would necessarily infringe our
|
|
57
|
+
patents, the license grant above includes a license under our patents. If you
|
|
58
|
+
make a claim against any party that the Software infringes or contributes to the
|
|
59
|
+
infringement of any patent, then your patent license to the Software ends
|
|
60
|
+
immediately.
|
|
61
|
+
|
|
62
|
+
### Redistribution
|
|
63
|
+
|
|
64
|
+
The Terms and Conditions apply to all copies, modifications and derivatives of
|
|
65
|
+
the Software.
|
|
66
|
+
|
|
67
|
+
If you redistribute any copies, modifications or derivatives of the Software,
|
|
68
|
+
you must include a copy of or a link to these Terms and Conditions and not
|
|
69
|
+
remove any copyright notices provided in or with the Software.
|
|
70
|
+
|
|
71
|
+
### Disclaimer
|
|
72
|
+
|
|
73
|
+
THE SOFTWARE IS PROVIDED "AS IS" AND WITHOUT WARRANTIES OF ANY KIND, EXPRESS OR
|
|
74
|
+
IMPLIED, INCLUDING WITHOUT LIMITATION WARRANTIES OF FITNESS FOR A PARTICULAR
|
|
75
|
+
PURPOSE, MERCHANTABILITY, TITLE OR NON-INFRINGEMENT.
|
|
76
|
+
|
|
77
|
+
IN NO EVENT WILL WE HAVE ANY LIABILITY TO YOU ARISING OUT OF OR RELATED TO THE
|
|
78
|
+
SOFTWARE, INCLUDING INDIRECT, SPECIAL, INCIDENTAL OR CONSEQUENTIAL DAMAGES, EVEN
|
|
79
|
+
IF WE HAVE BEEN INFORMED OF THEIR POSSIBILITY IN ADVANCE.
|
|
80
|
+
|
|
81
|
+
### Trademarks
|
|
82
|
+
|
|
83
|
+
Except for displaying the License Details and identifying us as the origin of
|
|
84
|
+
the Software, you have no right under these Terms and Conditions to use our
|
|
85
|
+
trademarks, trade names, service marks or product names.
|
|
86
|
+
|
|
87
|
+
## Grant of Future License
|
|
88
|
+
|
|
89
|
+
We hereby irrevocably grant you an additional license to use the Software under
|
|
90
|
+
the Apache License, Version 2.0 that is effective on the second anniversary of
|
|
91
|
+
the date we make the Software available. On or after that date, you may use the
|
|
92
|
+
Software under the Apache License, Version 2.0, in which case the following will
|
|
93
|
+
apply:
|
|
94
|
+
|
|
95
|
+
Licensed under the Apache License, Version 2.0 (the "License"); you may not use
|
|
96
|
+
this file except in compliance with the License.
|
|
97
|
+
|
|
98
|
+
You may obtain a copy of the License at
|
|
99
|
+
|
|
100
|
+
http://www.apache.org/licenses/LICENSE-2.0
|
|
101
|
+
|
|
102
|
+
Unless required by applicable law or agreed to in writing, software distributed
|
|
103
|
+
under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR
|
|
104
|
+
CONDITIONS OF ANY KIND, either express or implied. See the License for the
|
|
105
|
+
specific language governing permissions and limitations under the License.
|
|
106
|
+
|
|
107
|
+
<!-- DEPENDENCIES -->
|
|
108
|
+
<!-- /DEPENDENCIES -->
|
|
109
|
+
|
|
110
|
+
<!-- TYPE_DEPENDENCIES -->
|
|
111
|
+
<!-- /TYPE_DEPENDENCIES -->
|
package/README.md
CHANGED
|
@@ -1,45 +1,215 @@
|
|
|
1
|
+
<div align="center">
|
|
2
|
+
|
|
1
3
|
# @lunora/react-native
|
|
2
4
|
|
|
3
|
-
|
|
5
|
+
**React Native / Expo integration for [Lunora](https://lunora.sh).**
|
|
6
|
+
|
|
7
|
+
</div>
|
|
4
8
|
|
|
5
|
-
|
|
9
|
+
---
|
|
6
10
|
|
|
7
|
-
|
|
11
|
+
The same live hooks you use on the web — `useQuery`, `useMutation`,
|
|
12
|
+
`useSubscription`, `useAuth`, `usePresence`, … — running on your phone, plus the
|
|
13
|
+
two seams a native app needs that a browser gives you for free: a durable offline
|
|
14
|
+
queue backed by `AsyncStorage`, and credentialed requests (there is no cookie jar
|
|
15
|
+
in React Native, so the session has to be attached explicitly).
|
|
16
|
+
|
|
17
|
+
This package **re-exports the entire `@lunora/react` surface**, so you import
|
|
18
|
+
your hooks and provider from here, and adds:
|
|
19
|
+
|
|
20
|
+
- `createLunoraClient(options)` — a `LunoraClient` factory tuned for React
|
|
21
|
+
Native.
|
|
22
|
+
- `@lunora/react-native/auth` — a one-import bridge to the
|
|
23
|
+
[better-auth Expo](https://www.better-auth.com/docs/integrations/expo) plugin.
|
|
24
|
+
|
|
25
|
+
## Install
|
|
26
|
+
|
|
27
|
+
```sh
|
|
28
|
+
pnpm add @lunora/react-native @lunora/react @tanstack/react-query react
|
|
29
|
+
# offline-queue persistence:
|
|
30
|
+
npx expo install @react-native-async-storage/async-storage
|
|
31
|
+
# only if you use auth:
|
|
32
|
+
npx expo install expo-secure-store expo-web-browser expo-linking expo-constants expo-network
|
|
33
|
+
pnpm add better-auth @better-auth/expo
|
|
34
|
+
```
|
|
35
|
+
|
|
36
|
+
## Quick start
|
|
37
|
+
|
|
38
|
+
```tsx
|
|
39
|
+
// lunora.ts
|
|
40
|
+
import AsyncStorage from "@react-native-async-storage/async-storage";
|
|
41
|
+
import { createLunoraClient } from "@lunora/react-native";
|
|
42
|
+
|
|
43
|
+
export const client = createLunoraClient({
|
|
44
|
+
url: process.env.EXPO_PUBLIC_LUNORA_URL!, // e.g. https://my-app.workers.dev
|
|
45
|
+
storage: AsyncStorage, // persists offline writes across app restarts
|
|
46
|
+
});
|
|
47
|
+
```
|
|
48
|
+
|
|
49
|
+
```tsx
|
|
50
|
+
// App.tsx
|
|
51
|
+
import { LunoraProvider } from "@lunora/react-native";
|
|
52
|
+
import { QueryClient, QueryClientProvider } from "@tanstack/react-query";
|
|
53
|
+
|
|
54
|
+
import { client } from "./lunora";
|
|
55
|
+
import { Chat } from "./Chat";
|
|
56
|
+
|
|
57
|
+
const queryClient = new QueryClient();
|
|
58
|
+
|
|
59
|
+
export default function App() {
|
|
60
|
+
return (
|
|
61
|
+
<QueryClientProvider client={queryClient}>
|
|
62
|
+
<LunoraProvider client={client}>
|
|
63
|
+
<Chat />
|
|
64
|
+
</LunoraProvider>
|
|
65
|
+
</QueryClientProvider>
|
|
66
|
+
);
|
|
67
|
+
}
|
|
68
|
+
```
|
|
69
|
+
|
|
70
|
+
```tsx
|
|
71
|
+
// Chat.tsx
|
|
72
|
+
import { useMutation, useQuery } from "@lunora/react-native";
|
|
73
|
+
import { FlatList, Text } from "react-native";
|
|
74
|
+
|
|
75
|
+
import { api } from "./lunora/_generated/api";
|
|
76
|
+
|
|
77
|
+
export function Chat() {
|
|
78
|
+
const messages = useQuery(api.messages.list, {});
|
|
79
|
+
const { mutate: send } = useMutation(api.messages.send);
|
|
80
|
+
|
|
81
|
+
return <FlatList data={messages ?? []} keyExtractor={(m) => m._id} renderItem={({ item }) => <Text>{item.text}</Text>} />;
|
|
82
|
+
}
|
|
83
|
+
```
|
|
84
|
+
|
|
85
|
+
`useQuery` opens a live WebSocket subscription — the list updates in place as
|
|
86
|
+
other clients write, and `send` is optimistic and offline-safe.
|
|
87
|
+
|
|
88
|
+
## Authentication (better-auth + Expo)
|
|
89
|
+
|
|
90
|
+
React Native has no cookie jar, so the session is sent as a **bearer** token: the
|
|
91
|
+
HTTP RPC carries it in the `Authorization` header and the live socket carries it
|
|
92
|
+
in the `?token=` query param. A bearer avoids the `Cookie` header the runtime's
|
|
93
|
+
CSRF guard rejects on an `Origin`-less native request (see [Why a bearer token](#why-a-bearer-token)).
|
|
94
|
+
|
|
95
|
+
```tsx
|
|
96
|
+
// auth.ts
|
|
97
|
+
import { createAuthClient } from "better-auth/react";
|
|
98
|
+
import { expoClient } from "@lunora/react-native/auth";
|
|
99
|
+
import * as SecureStore from "expo-secure-store";
|
|
100
|
+
|
|
101
|
+
export const authClient = createAuthClient({
|
|
102
|
+
baseURL: process.env.EXPO_PUBLIC_LUNORA_URL!,
|
|
103
|
+
plugins: [expoClient({ scheme: "myapp", storage: SecureStore })],
|
|
104
|
+
});
|
|
105
|
+
```
|
|
106
|
+
|
|
107
|
+
```tsx
|
|
108
|
+
// lunora.ts
|
|
109
|
+
import AsyncStorage from "@react-native-async-storage/async-storage";
|
|
110
|
+
import { createLunoraClient } from "@lunora/react-native";
|
|
111
|
+
|
|
112
|
+
export const client = createLunoraClient({
|
|
113
|
+
url: process.env.EXPO_PUBLIC_LUNORA_URL!,
|
|
114
|
+
storage: AsyncStorage,
|
|
115
|
+
});
|
|
116
|
+
```
|
|
117
|
+
|
|
118
|
+
Bridge the session into the client whenever it changes — `setAuthToken` for HTTP,
|
|
119
|
+
`setWsToken` for the socket:
|
|
120
|
+
|
|
121
|
+
```tsx
|
|
122
|
+
// App.tsx
|
|
123
|
+
import { useEffect } from "react";
|
|
124
|
+
import { expoBearerToken } from "@lunora/react-native/auth";
|
|
125
|
+
|
|
126
|
+
import { authClient } from "./auth";
|
|
127
|
+
import { client } from "./lunora";
|
|
128
|
+
|
|
129
|
+
function Root() {
|
|
130
|
+
const { data: session } = authClient.useSession();
|
|
131
|
+
|
|
132
|
+
useEffect(() => {
|
|
133
|
+
const token = expoBearerToken(authClient);
|
|
134
|
+
client.setAuthToken(token); // HTTP `Authorization: Bearer …`
|
|
135
|
+
client.setWsToken(token ?? undefined); // WS `?token=…`
|
|
136
|
+
}, [session]);
|
|
8
137
|
|
|
9
|
-
|
|
138
|
+
// …render the app
|
|
139
|
+
}
|
|
140
|
+
```
|
|
10
141
|
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
3. Establish provenance for packages published under this name
|
|
142
|
+
On the **server**, add better-auth's `expo()` and `bearer()` plugins, and fold
|
|
143
|
+
the socket's `?token=` into an `Authorization` header in `resolveIdentity` (see
|
|
144
|
+
`@lunora/auth`):
|
|
15
145
|
|
|
16
|
-
|
|
146
|
+
```ts
|
|
147
|
+
import { bearer } from "@lunora/auth/plugins";
|
|
148
|
+
import { expo } from "@better-auth/expo";
|
|
17
149
|
|
|
18
|
-
|
|
150
|
+
// authOptions.plugins: [expo(), bearer(), /* … */]
|
|
151
|
+
// authOptions.trustedOrigins: ["myapp://"]
|
|
19
152
|
|
|
20
|
-
|
|
153
|
+
// createWorker({
|
|
154
|
+
// resolveIdentity: async (request) => {
|
|
155
|
+
// const headers = new Headers(request.headers);
|
|
156
|
+
// const wsToken = new URL(request.url).searchParams.get("token");
|
|
157
|
+
// if (wsToken && !headers.has("authorization")) headers.set("authorization", `Bearer ${wsToken}`);
|
|
158
|
+
// const session = await auth.api.getSession({ headers });
|
|
159
|
+
// return session?.user?.id ? { userId: session.user.id } : null;
|
|
160
|
+
// },
|
|
161
|
+
// });
|
|
162
|
+
```
|
|
21
163
|
|
|
22
|
-
|
|
164
|
+
### Why a bearer token
|
|
23
165
|
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
166
|
+
Lunora's runtime enables a CSRF Origin-check by default — it rejects any
|
|
167
|
+
state-changing HTTP request or WebSocket upgrade that carries a `Cookie` but no
|
|
168
|
+
trusted `Origin`. React Native sends no `Origin`, so a cookie-based credential
|
|
169
|
+
would be **rejected** once signed in. A bearer token carries no `Cookie`, so it's
|
|
170
|
+
exempt — and it works identically on `react-native-web` (the browser lets you set
|
|
171
|
+
`Authorization`, and the token rides `?token=` on the socket).
|
|
28
172
|
|
|
29
|
-
|
|
173
|
+
### TanStack Query focus / online managers
|
|
30
174
|
|
|
31
|
-
|
|
32
|
-
-
|
|
33
|
-
- Provides no functionality
|
|
34
|
-
- Should not be installed as a dependency
|
|
35
|
-
- Exists only for administrative purposes
|
|
175
|
+
React Native doesn't fire the browser's `focus` / `online` events, so Query
|
|
176
|
+
won't auto-refetch on app resume unless you point its managers at Expo's signals:
|
|
36
177
|
|
|
37
|
-
|
|
178
|
+
```ts
|
|
179
|
+
import { setupExpoFocusManager, setupExpoOnlineManager } from "@lunora/react-native/auth";
|
|
38
180
|
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
181
|
+
setupExpoFocusManager();
|
|
182
|
+
setupExpoOnlineManager();
|
|
183
|
+
```
|
|
42
184
|
|
|
43
|
-
|
|
185
|
+
## API
|
|
186
|
+
|
|
187
|
+
### `createLunoraClient(options)`
|
|
188
|
+
|
|
189
|
+
Everything on `LunoraClientOptions` (`url`, `wsUrl`, `authBasePath`,
|
|
190
|
+
`persistenceVersion`, …) plus:
|
|
191
|
+
|
|
192
|
+
| Option | Description |
|
|
193
|
+
| ---------------- | ----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- |
|
|
194
|
+
| `storage` | React Native `AsyncStorage` (or any `getItem`/`setItem`/`removeItem` store). Wires `createAsyncStoragePersistence` for the offline queue. |
|
|
195
|
+
| `getAuthHeaders` | `() => Record<string, string> \| undefined`. Generic escape hatch for a **custom** credential header (API-gateway key, proxy token) on every HTTP RPC + the WebSocket upgrade. For better-auth sessions prefer a bearer token (`expoBearerToken` + `setAuthToken`/`setWsToken`) — a `Cookie` header here would trip the CSRF guard. |
|
|
196
|
+
|
|
197
|
+
An explicit `persistence`, `fetch`, or `WebSocket` always takes precedence over
|
|
198
|
+
the convenience derived from `storage` / `getAuthHeaders`.
|
|
199
|
+
|
|
200
|
+
### `@lunora/react-native/auth`
|
|
201
|
+
|
|
202
|
+
- `expoBearerToken(authClient)` — reads the better-auth Expo session token (from
|
|
203
|
+
`getCookie()`) for use with `client.setAuthToken` / `setWsToken`; `null` when
|
|
204
|
+
signed out.
|
|
205
|
+
- `expoClient`, `setupExpoFocusManager`, `setupExpoOnlineManager` — re-exported
|
|
206
|
+
from `@better-auth/expo/client`.
|
|
207
|
+
|
|
208
|
+
## Example app
|
|
209
|
+
|
|
210
|
+
A complete Expo chat app (auth + live messages + offline queue) lives at
|
|
211
|
+
[`examples/expo`](../../examples/expo) in this repo.
|
|
212
|
+
|
|
213
|
+
## License
|
|
44
214
|
|
|
45
|
-
|
|
215
|
+
FSL-1.1-Apache-2.0 — see [LICENSE.md](./LICENSE.md).
|