jazz-react-native 0.8.3
Sign up to get free protection for your applications and to get access to all the features.
- package/.eslintrc.cjs +24 -0
- package/.prettierrc.js +9 -0
- package/CHANGELOG.md +917 -0
- package/LICENSE.txt +19 -0
- package/README.md +10 -0
- package/dist/auth/DemoAuthMethod.d.ts +28 -0
- package/dist/auth/DemoAuthMethod.js +132 -0
- package/dist/auth/DemoAuthMethod.js.map +1 -0
- package/dist/auth/DemoAuthUI.d.ts +33 -0
- package/dist/auth/DemoAuthUI.js +203 -0
- package/dist/auth/DemoAuthUI.js.map +1 -0
- package/dist/auth/auth.d.ts +1 -0
- package/dist/auth/auth.js +2 -0
- package/dist/auth/auth.js.map +1 -0
- package/dist/index.d.ts +50 -0
- package/dist/index.js +153 -0
- package/dist/index.js.map +1 -0
- package/dist/media.d.ts +21 -0
- package/dist/media.js +44 -0
- package/dist/media.js.map +1 -0
- package/dist/native-storage.d.ts +15 -0
- package/dist/native-storage.js +24 -0
- package/dist/native-storage.js.map +1 -0
- package/dist/provider.d.ts +46 -0
- package/dist/provider.js +130 -0
- package/dist/provider.js.map +1 -0
- package/package.json +47 -0
- package/src/auth/DemoAuthMethod.ts +219 -0
- package/src/auth/DemoAuthUI.tsx +289 -0
- package/src/auth/auth.ts +1 -0
- package/src/index.ts +275 -0
- package/src/media.tsx +68 -0
- package/src/native-storage.ts +35 -0
- package/src/provider.tsx +306 -0
- package/tsconfig.json +21 -0
package/LICENSE.txt
ADDED
@@ -0,0 +1,19 @@
|
|
1
|
+
Copyright 2024, Garden Computing, Inc.
|
2
|
+
|
3
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
4
|
+
of this software and associated documentation files (the "Software"), to deal
|
5
|
+
in the Software without restriction, including without limitation the rights
|
6
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
7
|
+
copies of the Software, and to permit persons to whom the Software is
|
8
|
+
furnished to do so, subject to the following conditions:
|
9
|
+
|
10
|
+
The above copyright notice and this permission notice shall be included in all
|
11
|
+
copies or substantial portions of the Software.
|
12
|
+
|
13
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
14
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
15
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
16
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
17
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
18
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
19
|
+
SOFTWARE.
|
package/README.md
ADDED
@@ -0,0 +1,10 @@
|
|
1
|
+
# `jazz-browser`
|
2
|
+
|
3
|
+
These are browser bindings for Jazz (see [jazz.tools](https://jazz.tools)), a framework for distributed state.
|
4
|
+
|
5
|
+
Use this only if you want to write Jazz apps using plain JavaScript,
|
6
|
+
or to build your framework bindings for Jazz.
|
7
|
+
|
8
|
+
## Higher-level framework bindings:
|
9
|
+
|
10
|
+
- `jazz-react` - React bindings for Jazz
|
@@ -0,0 +1,28 @@
|
|
1
|
+
import { AgentSecret } from "cojson";
|
2
|
+
import { Account, AuthMethod, AuthResult, ID } from "jazz-tools";
|
3
|
+
/** @category Auth Providers */
|
4
|
+
export declare namespace RNDemoAuth {
|
5
|
+
interface Driver {
|
6
|
+
onReady: (next: {
|
7
|
+
signUp: (username: string) => Promise<void>;
|
8
|
+
getExistingUsers: () => Promise<string[]>;
|
9
|
+
logInAs: (existingUser: string) => Promise<void>;
|
10
|
+
}) => void;
|
11
|
+
onSignedIn: (next: {
|
12
|
+
logOut: () => void;
|
13
|
+
}) => void;
|
14
|
+
onError: (error: string | Error) => void;
|
15
|
+
}
|
16
|
+
}
|
17
|
+
export declare class RNDemoAuth implements AuthMethod {
|
18
|
+
private driver;
|
19
|
+
private storage;
|
20
|
+
private constructor();
|
21
|
+
static init(driver: RNDemoAuth.Driver, seedAccounts?: {
|
22
|
+
[name: string]: {
|
23
|
+
accountID: ID<Account>;
|
24
|
+
accountSecret: AgentSecret;
|
25
|
+
};
|
26
|
+
}): Promise<RNDemoAuth>;
|
27
|
+
start(): Promise<AuthResult>;
|
28
|
+
}
|
@@ -0,0 +1,132 @@
|
|
1
|
+
import NativeStorageContext from "../native-storage.js";
|
2
|
+
const localStorageKey = "demo-auth-logged-in-secret";
|
3
|
+
export class RNDemoAuth {
|
4
|
+
driver;
|
5
|
+
storage;
|
6
|
+
constructor(driver, storage) {
|
7
|
+
this.driver = driver;
|
8
|
+
this.storage = storage;
|
9
|
+
}
|
10
|
+
static async init(driver, seedAccounts) {
|
11
|
+
const storage = NativeStorageContext.getInstance().getStorage();
|
12
|
+
for (const [name, credentials] of Object.entries(seedAccounts || {})) {
|
13
|
+
const storageData = JSON.stringify(credentials);
|
14
|
+
if (!(await storage.get("demo-auth-existing-users"))?.split(",")?.includes(name)) {
|
15
|
+
const existingUsers = await storage.get("demo-auth-existing-users");
|
16
|
+
if (existingUsers) {
|
17
|
+
await storage.set("demo-auth-existing-users", existingUsers + "," + name);
|
18
|
+
}
|
19
|
+
else {
|
20
|
+
await storage.set("demo-auth-existing-users", name);
|
21
|
+
}
|
22
|
+
}
|
23
|
+
await storage.set("demo-auth-existing-users-" + name, storageData);
|
24
|
+
}
|
25
|
+
return new RNDemoAuth(driver, storage);
|
26
|
+
}
|
27
|
+
async start() {
|
28
|
+
try {
|
29
|
+
if (await this.storage.get(localStorageKey)) {
|
30
|
+
const localStorageData = JSON.parse((await this.storage.get(localStorageKey)) ?? "{}");
|
31
|
+
const accountID = localStorageData.accountID;
|
32
|
+
const secret = localStorageData.accountSecret;
|
33
|
+
return {
|
34
|
+
type: "existing",
|
35
|
+
credentials: { accountID, secret },
|
36
|
+
onSuccess: () => {
|
37
|
+
this.driver.onSignedIn({ logOut });
|
38
|
+
},
|
39
|
+
onError: (error) => {
|
40
|
+
this.driver.onError(error);
|
41
|
+
},
|
42
|
+
logOut: async () => {
|
43
|
+
void (await this.storage.delete(localStorageKey));
|
44
|
+
},
|
45
|
+
};
|
46
|
+
}
|
47
|
+
else {
|
48
|
+
return new Promise((resolve) => {
|
49
|
+
this.driver.onReady({
|
50
|
+
// @ts-expect-error asd
|
51
|
+
signUp: (username) => {
|
52
|
+
resolve({
|
53
|
+
type: "new",
|
54
|
+
creationProps: { name: username },
|
55
|
+
saveCredentials: async (credentials) => {
|
56
|
+
const storageData = JSON.stringify({
|
57
|
+
accountID: credentials.accountID,
|
58
|
+
accountSecret: credentials.secret,
|
59
|
+
});
|
60
|
+
// Retrieve the list of existing users
|
61
|
+
const existingUsers = await this.storage.get("demo-auth-existing-users");
|
62
|
+
const existingUsernames = existingUsers
|
63
|
+
? existingUsers.split(",")
|
64
|
+
: [];
|
65
|
+
// Determine if the username already exists and generate a unique username
|
66
|
+
let uniqueUsername = username;
|
67
|
+
let counter = 1;
|
68
|
+
while (existingUsernames.includes(uniqueUsername)) {
|
69
|
+
counter++;
|
70
|
+
uniqueUsername = `${username}-${counter}`;
|
71
|
+
}
|
72
|
+
// Save credentials using the unique username
|
73
|
+
await this.storage.set(localStorageKey, storageData);
|
74
|
+
await this.storage.set("demo-auth-existing-users-" +
|
75
|
+
uniqueUsername, storageData);
|
76
|
+
// Update the list of existing users
|
77
|
+
const updatedUsers = existingUsers
|
78
|
+
? `${existingUsers},${uniqueUsername}`
|
79
|
+
: uniqueUsername;
|
80
|
+
await this.storage.set("demo-auth-existing-users", updatedUsers);
|
81
|
+
},
|
82
|
+
onSuccess: () => {
|
83
|
+
this.driver.onSignedIn({ logOut });
|
84
|
+
},
|
85
|
+
onError: (error) => {
|
86
|
+
// @ts-expect-error asd
|
87
|
+
console.error("onError", error.cause);
|
88
|
+
this.driver.onError(error);
|
89
|
+
},
|
90
|
+
logOut: async () => {
|
91
|
+
void (await this.storage.delete(localStorageKey));
|
92
|
+
},
|
93
|
+
});
|
94
|
+
},
|
95
|
+
getExistingUsers: async () => {
|
96
|
+
return ((await this.storage.get("demo-auth-existing-users"))?.split(",") ?? []);
|
97
|
+
},
|
98
|
+
logInAs: async (existingUser) => {
|
99
|
+
const storageData = JSON.parse((await this.storage.get("demo-auth-existing-users-" + existingUser)) ?? "{}");
|
100
|
+
await this.storage.set(localStorageKey, JSON.stringify(storageData));
|
101
|
+
resolve({
|
102
|
+
type: "existing",
|
103
|
+
credentials: {
|
104
|
+
accountID: storageData.accountID,
|
105
|
+
secret: storageData.accountSecret,
|
106
|
+
},
|
107
|
+
onSuccess: () => {
|
108
|
+
this.driver.onSignedIn({ logOut });
|
109
|
+
},
|
110
|
+
onError: (error) => {
|
111
|
+
this.driver.onError(error);
|
112
|
+
},
|
113
|
+
logOut: async () => {
|
114
|
+
void (await this.storage.delete(localStorageKey));
|
115
|
+
},
|
116
|
+
});
|
117
|
+
},
|
118
|
+
});
|
119
|
+
});
|
120
|
+
}
|
121
|
+
}
|
122
|
+
catch (error) {
|
123
|
+
console.error("error", error);
|
124
|
+
throw error;
|
125
|
+
}
|
126
|
+
}
|
127
|
+
}
|
128
|
+
async function logOut() {
|
129
|
+
const storage = NativeStorageContext.getInstance().getStorage();
|
130
|
+
void (await storage.delete(localStorageKey));
|
131
|
+
}
|
132
|
+
//# sourceMappingURL=DemoAuthMethod.js.map
|
@@ -0,0 +1 @@
|
|
1
|
+
{"version":3,"file":"DemoAuthMethod.js","sourceRoot":"","sources":["../../src/auth/DemoAuthMethod.ts"],"names":[],"mappings":"AAEA,OAAO,oBAAuC,MAAM,sBAAsB,CAAC;AAqB3E,MAAM,eAAe,GAAG,4BAA4B,CAAC;AAErD,MAAM,OAAO,UAAU;IAEP;IACA;IAFZ,YACY,MAAyB,EACzB,OAAsB;QADtB,WAAM,GAAN,MAAM,CAAmB;QACzB,YAAO,GAAP,OAAO,CAAe;IAC/B,CAAC;IAEG,MAAM,CAAC,KAAK,CAAC,IAAI,C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CAAC,UAAU,EAAE,CAAC;IAChE,KAAK,CAAC,MAAM,OAAO,CAAC,MAAM,CAAC,eAAe,CAAC,CAAC,CAAC;AACjD,CAAC"}
|
@@ -0,0 +1,33 @@
|
|
1
|
+
import React from "react";
|
2
|
+
import { AgentSecret } from "cojson";
|
3
|
+
import { Account, ID } from "jazz-tools";
|
4
|
+
import { RNDemoAuth } from "./DemoAuthMethod.js";
|
5
|
+
type DemoAuthState = ({
|
6
|
+
state: "uninitialized";
|
7
|
+
} | {
|
8
|
+
state: "loading";
|
9
|
+
} | {
|
10
|
+
state: "ready";
|
11
|
+
existingUsers: string[];
|
12
|
+
signUp: (username: string) => void;
|
13
|
+
logInAs: (existingUser: string) => void;
|
14
|
+
} | {
|
15
|
+
state: "signedIn";
|
16
|
+
logOut: () => void;
|
17
|
+
}) & {
|
18
|
+
errors: string[];
|
19
|
+
};
|
20
|
+
/** @category Auth Providers */
|
21
|
+
export declare function useDemoAuth({ seedAccounts, }?: {
|
22
|
+
seedAccounts?: {
|
23
|
+
[name: string]: {
|
24
|
+
accountID: ID<Account>;
|
25
|
+
accountSecret: AgentSecret;
|
26
|
+
};
|
27
|
+
};
|
28
|
+
}): readonly [RNDemoAuth | null, DemoAuthState];
|
29
|
+
export declare const DemoAuthBasicUI: ({ appName, state, }: {
|
30
|
+
appName: string;
|
31
|
+
state: DemoAuthState;
|
32
|
+
}) => React.JSX.Element;
|
33
|
+
export {};
|
@@ -0,0 +1,203 @@
|
|
1
|
+
import React, { useMemo, useState, useEffect } from "react";
|
2
|
+
import { View, Text, TouchableOpacity, TextInput, StyleSheet, } from "react-native";
|
3
|
+
import { RNDemoAuth } from "./DemoAuthMethod.js";
|
4
|
+
/** @category Auth Providers */
|
5
|
+
export function useDemoAuth({ seedAccounts, } = {}) {
|
6
|
+
const [state, setState] = useState({
|
7
|
+
state: "loading",
|
8
|
+
errors: [],
|
9
|
+
});
|
10
|
+
const [authMethod, setAuthMethod] = useState(null);
|
11
|
+
const authMethodPromise = useMemo(() => {
|
12
|
+
return RNDemoAuth.init({
|
13
|
+
onReady: async ({ signUp, getExistingUsers, logInAs }) => {
|
14
|
+
const existingUsers = await getExistingUsers();
|
15
|
+
setState({
|
16
|
+
state: "ready",
|
17
|
+
signUp,
|
18
|
+
existingUsers,
|
19
|
+
logInAs,
|
20
|
+
errors: [],
|
21
|
+
});
|
22
|
+
},
|
23
|
+
onSignedIn: ({ logOut }) => {
|
24
|
+
setState({ state: "signedIn", logOut, errors: [] });
|
25
|
+
},
|
26
|
+
onError: (error) => {
|
27
|
+
setState((current) => ({
|
28
|
+
...current,
|
29
|
+
errors: [...current.errors, error.toString()],
|
30
|
+
}));
|
31
|
+
},
|
32
|
+
}, seedAccounts);
|
33
|
+
}, [seedAccounts]);
|
34
|
+
useEffect(() => {
|
35
|
+
async function init() {
|
36
|
+
const auth = await authMethodPromise;
|
37
|
+
setAuthMethod(auth);
|
38
|
+
}
|
39
|
+
if (authMethod)
|
40
|
+
return;
|
41
|
+
void init();
|
42
|
+
}, [seedAccounts]);
|
43
|
+
return [authMethod, state];
|
44
|
+
}
|
45
|
+
export const DemoAuthBasicUI = ({ appName, state, }) => {
|
46
|
+
const darkMode = false;
|
47
|
+
const [username, setUsername] = useState("");
|
48
|
+
const [errorMessage, setErrorMessage] = useState(null);
|
49
|
+
const handleSignUp = () => {
|
50
|
+
if (state.state !== "ready")
|
51
|
+
return;
|
52
|
+
if (username.trim() === "") {
|
53
|
+
setErrorMessage("Display name is required");
|
54
|
+
}
|
55
|
+
else {
|
56
|
+
setErrorMessage(null);
|
57
|
+
state.signUp(username);
|
58
|
+
}
|
59
|
+
};
|
60
|
+
return (<View style={[
|
61
|
+
styles.container,
|
62
|
+
darkMode ? styles.darkBackground : styles.lightBackground,
|
63
|
+
]}>
|
64
|
+
{state.state === "loading" ? (<Text style={styles.loadingText}>Loading...</Text>) : state.state === "ready" ? (<View style={styles.formContainer}>
|
65
|
+
<Text style={[
|
66
|
+
styles.headerText,
|
67
|
+
darkMode ? styles.darkText : styles.lightText,
|
68
|
+
]}>
|
69
|
+
{appName}
|
70
|
+
</Text>
|
71
|
+
|
72
|
+
{state.errors.map((error) => (<Text key={error} style={styles.errorText}>
|
73
|
+
{error}
|
74
|
+
</Text>))}
|
75
|
+
|
76
|
+
{errorMessage && (<Text style={styles.errorText}>{errorMessage}</Text>)}
|
77
|
+
|
78
|
+
<TextInput placeholder="Display name" value={username} onChangeText={setUsername} placeholderTextColor={darkMode ? "#fff" : "#000"} style={[
|
79
|
+
styles.textInput,
|
80
|
+
darkMode ? styles.darkInput : styles.lightInput,
|
81
|
+
]}/>
|
82
|
+
|
83
|
+
<TouchableOpacity onPress={handleSignUp} style={[
|
84
|
+
styles.button,
|
85
|
+
darkMode ? styles.darkButton : styles.lightButton,
|
86
|
+
]}>
|
87
|
+
<Text style={darkMode
|
88
|
+
? styles.darkButtonText
|
89
|
+
: styles.lightButtonText}>
|
90
|
+
Sign Up as new account
|
91
|
+
</Text>
|
92
|
+
</TouchableOpacity>
|
93
|
+
|
94
|
+
<View style={styles.existingUsersContainer}>
|
95
|
+
{state.existingUsers.map((user) => (<TouchableOpacity key={user} onPress={() => state.logInAs(user)} style={[
|
96
|
+
styles.existingUserButton,
|
97
|
+
darkMode
|
98
|
+
? styles.darkUserButton
|
99
|
+
: styles.lightUserButton,
|
100
|
+
]}>
|
101
|
+
<Text style={darkMode
|
102
|
+
? styles.darkText
|
103
|
+
: styles.lightText}>
|
104
|
+
Log In as "{user}"
|
105
|
+
</Text>
|
106
|
+
</TouchableOpacity>))}
|
107
|
+
</View>
|
108
|
+
</View>) : null}
|
109
|
+
</View>);
|
110
|
+
};
|
111
|
+
const styles = StyleSheet.create({
|
112
|
+
container: {
|
113
|
+
flex: 1,
|
114
|
+
justifyContent: "center",
|
115
|
+
alignItems: "center",
|
116
|
+
padding: 20,
|
117
|
+
},
|
118
|
+
formContainer: {
|
119
|
+
width: "80%",
|
120
|
+
alignItems: "center",
|
121
|
+
justifyContent: "center",
|
122
|
+
},
|
123
|
+
headerText: {
|
124
|
+
fontSize: 24,
|
125
|
+
marginBottom: 20,
|
126
|
+
},
|
127
|
+
errorText: {
|
128
|
+
color: "red",
|
129
|
+
marginVertical: 5,
|
130
|
+
textAlign: "center",
|
131
|
+
},
|
132
|
+
textInput: {
|
133
|
+
borderWidth: 1,
|
134
|
+
padding: 10,
|
135
|
+
marginVertical: 10,
|
136
|
+
width: "100%",
|
137
|
+
borderRadius: 6,
|
138
|
+
},
|
139
|
+
darkInput: {
|
140
|
+
borderColor: "#444",
|
141
|
+
backgroundColor: "#000",
|
142
|
+
color: "#fff",
|
143
|
+
},
|
144
|
+
lightInput: {
|
145
|
+
borderColor: "#ddd",
|
146
|
+
backgroundColor: "#fff",
|
147
|
+
color: "#000",
|
148
|
+
},
|
149
|
+
button: {
|
150
|
+
paddingVertical: 15,
|
151
|
+
paddingHorizontal: 10,
|
152
|
+
borderRadius: 6,
|
153
|
+
width: "100%",
|
154
|
+
marginVertical: 10,
|
155
|
+
},
|
156
|
+
darkButton: {
|
157
|
+
backgroundColor: "#444",
|
158
|
+
},
|
159
|
+
lightButton: {
|
160
|
+
backgroundColor: "#ddd",
|
161
|
+
},
|
162
|
+
darkButtonText: {
|
163
|
+
color: "#fff",
|
164
|
+
textAlign: "center",
|
165
|
+
},
|
166
|
+
lightButtonText: {
|
167
|
+
color: "#000",
|
168
|
+
textAlign: "center",
|
169
|
+
},
|
170
|
+
existingUsersContainer: {
|
171
|
+
width: "100%",
|
172
|
+
marginTop: 20,
|
173
|
+
},
|
174
|
+
existingUserButton: {
|
175
|
+
paddingVertical: 15,
|
176
|
+
paddingHorizontal: 10,
|
177
|
+
borderRadius: 6,
|
178
|
+
marginVertical: 5,
|
179
|
+
},
|
180
|
+
darkUserButton: {
|
181
|
+
backgroundColor: "#222",
|
182
|
+
},
|
183
|
+
lightUserButton: {
|
184
|
+
backgroundColor: "#eee",
|
185
|
+
},
|
186
|
+
loadingText: {
|
187
|
+
fontSize: 18,
|
188
|
+
color: "#888",
|
189
|
+
},
|
190
|
+
darkText: {
|
191
|
+
color: "#fff",
|
192
|
+
},
|
193
|
+
lightText: {
|
194
|
+
color: "#000",
|
195
|
+
},
|
196
|
+
darkBackground: {
|
197
|
+
backgroundColor: "#000",
|
198
|
+
},
|
199
|
+
lightBackground: {
|
200
|
+
backgroundColor: "#fff",
|
201
|
+
},
|
202
|
+
});
|
203
|
+
//# sourceMappingURL=DemoAuthUI.js.map
|
@@ -0,0 +1 @@
|
|
1
|
+
{"version":3,"file":"DemoAuthUI.js","sourceRoot":"","sources":["../../src/auth/DemoAuthUI.tsx"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,EAAE,OAAO,EAAE,QAAQ,EAAE,SAAS,EAAE,MAAM,OAAO,CAAC;AAC5D,OAAO,EACH,IAAI,EACJ,IAAI,EACJ,gBAAgB,EAChB,SAAS,EACT,UAAU,GACb,MAAM,cAAc,CAAC;AAGtB,OAAO,EAAE,UAAU,EAAE,MAAM,qBAAqB,CAAC;AAuBjD,+BAA+B;AAC/B,MAAM,UAAU,WAAW,CAAC,EACxB,YAAY,MAKZ,EAAE;IACF,MAAM,CAAC,KAAK,EAAE,QAAQ,CAAC,GAAG,QAAQ,CAAgB;QAC9C,KAAK,EAAE,SAAS;QAChB,MAAM,EAAE,EAAE;KACb,CAAC,CAAC;IAEH,MAAM,CAAC,UAAU,EAAE,aAAa,CAAC,GAAG,QAAQ,CAAoB,IAAI,CAAC,CAAC;IAEtE,MAAM,iBAAiB,GAAG,OAAO,CAAC,GAAG,EAAE;QACnC,OAAO,UAAU,CAAC,IAAI,CAClB;YACI,OAAO,EAAE,KAAK,EAAE,EAAE,MAAM,EAAE,gBAAgB,EAAE,OAAO,EAAE,EAAE,EAAE;gBACrD,MAAM,aAAa,GAAG,MAAM,gBAAgB,EAAE,CAAC;gBAC/C,QAAQ,CAAC;oBACL,KAAK,EAAE,OAAO;oBACd,MAAM;oBACN,aAAa;oBACb,OAAO;oBACP,MAAM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iBAAiB,EAAE,EAAE;QACrB,YAAY,EAAE,CAAC;QACf,KAAK,EAAE,MAAM;QACb,cAAc,EAAE,EAAE;KACrB;IACD,UAAU,EAAE;QACR,eAAe,EAAE,MAAM;KAC1B;IACD,WAAW,EAAE;QACT,eAAe,EAAE,MAAM;KAC1B;IACD,cAAc,EAAE;QACZ,KAAK,EAAE,MAAM;QACb,SAAS,EAAE,QAAQ;KACtB;IACD,eAAe,EAAE;QACb,KAAK,EAAE,MAAM;QACb,SAAS,EAAE,QAAQ;KACtB;IACD,sBAAsB,EAAE;QACpB,KAAK,EAAE,MAAM;QACb,SAAS,EAAE,EAAE;KAChB;IACD,kBAAkB,EAAE;QAChB,eAAe,EAAE,EAAE;QACnB,iBAAiB,EAAE,EAAE;QACrB,YAAY,EAAE,CAAC;QACf,cAAc,EAAE,CAAC;KACpB;IACD,cAAc,EAAE;QACZ,eAAe,EAAE,MAAM;KAC1B;IACD,eAAe,EAAE;QACb,eAAe,EAAE,MAAM;KAC1B;IACD,WAAW,EAAE;QACT,QAAQ,EAAE,EAAE;QACZ,KAAK,EAAE,MAAM;KAChB;IACD,QAAQ,EAAE;QACN,KAAK,EAAE,MAAM;KAChB;IACD,SAAS,EAAE;QACP,KAAK,EAAE,MAAM;KAChB;IACD,cAAc,EAAE;QACZ,eAAe,EAAE,MAAM;KAC1B;IACD,eAAe,EAAE;QACb,eAAe,EAAE,MAAM;KAC1B;CACJ,CAAC,CAAC"}
|
@@ -0,0 +1 @@
|
|
1
|
+
export * from "./DemoAuthUI.js";
|
@@ -0,0 +1 @@
|
|
1
|
+
{"version":3,"file":"auth.js","sourceRoot":"","sources":["../../src/auth/auth.ts"],"names":[],"mappings":"AAAA,cAAc,iBAAiB,CAAC"}
|
package/dist/index.d.ts
ADDED
@@ -0,0 +1,50 @@
|
|
1
|
+
import { CoValue, ID, AgentID, SessionID, InviteSecret, Account, CoValueClass, CryptoProvider, AuthMethod, AnonymousJazzAgent } from "jazz-tools";
|
2
|
+
import { RawAccountID } from "cojson";
|
3
|
+
export { RNDemoAuth } from "./auth/DemoAuthMethod.js";
|
4
|
+
/** @category Context Creation */
|
5
|
+
export type BrowserContext<Acc extends Account> = {
|
6
|
+
me: Acc;
|
7
|
+
logOut: () => void;
|
8
|
+
done: () => void;
|
9
|
+
};
|
10
|
+
export type BrowserGuestContext = {
|
11
|
+
guest: AnonymousJazzAgent;
|
12
|
+
logOut: () => void;
|
13
|
+
done: () => void;
|
14
|
+
};
|
15
|
+
export type BrowserContextOptions<Acc extends Account> = {
|
16
|
+
auth: AuthMethod;
|
17
|
+
AccountSchema: CoValueClass<Acc> & {
|
18
|
+
fromNode: (typeof Account)["fromNode"];
|
19
|
+
};
|
20
|
+
} & BaseBrowserContextOptions;
|
21
|
+
export type BaseBrowserContextOptions = {
|
22
|
+
peer: `wss://${string}` | `ws://${string}`;
|
23
|
+
reconnectionTimeout?: number;
|
24
|
+
storage?: "indexedDB" | "singleTabOPFS";
|
25
|
+
crypto?: CryptoProvider;
|
26
|
+
};
|
27
|
+
/** @category Context Creation */
|
28
|
+
export declare function createJazzRNContext<Acc extends Account>(options: BrowserContextOptions<Acc>): Promise<BrowserContext<Acc>>;
|
29
|
+
export declare function createJazzRNContext(options: BaseBrowserContextOptions): Promise<BrowserGuestContext>;
|
30
|
+
export declare function createJazzRNContext<Acc extends Account>(options: BrowserContextOptions<Acc> | BaseBrowserContextOptions): Promise<BrowserContext<Acc> | BrowserGuestContext>;
|
31
|
+
/** @category Auth Providers */
|
32
|
+
export type SessionProvider = (accountID: ID<Account> | AgentID) => Promise<SessionID>;
|
33
|
+
export declare function provideLockSession(accountID: ID<Account> | AgentID, crypto: CryptoProvider): Promise<{
|
34
|
+
sessionID: `${RawAccountID}_session_z${string}` | `sealer_z${string}/signer_z${string}_session_z${string}`;
|
35
|
+
sessionDone: () => void;
|
36
|
+
}>;
|
37
|
+
/** @category Invite Links */
|
38
|
+
export declare function createInviteLink<C extends CoValue>(value: C, role: "reader" | "writer" | "admin", { baseURL, valueHint }?: {
|
39
|
+
baseURL?: string;
|
40
|
+
valueHint?: string;
|
41
|
+
}): string;
|
42
|
+
/** @category Invite Links */
|
43
|
+
export declare function parseInviteLink<C extends CoValue>(inviteURL: string): {
|
44
|
+
valueID: ID<C>;
|
45
|
+
valueHint?: string;
|
46
|
+
inviteSecret: InviteSecret;
|
47
|
+
} | undefined;
|
48
|
+
export * from "./provider.js";
|
49
|
+
export * from "./auth/auth.js";
|
50
|
+
export * from "./native-storage.js";
|
package/dist/index.js
ADDED
@@ -0,0 +1,153 @@
|
|
1
|
+
/* eslint-disable @typescript-eslint/no-explicit-any */
|
2
|
+
/* eslint-disable @typescript-eslint/no-unused-vars */
|
3
|
+
import { cojsonInternals, createJazzContext, } from "jazz-tools";
|
4
|
+
import { PureJSCrypto } from "jazz-tools/native";
|
5
|
+
import { createWebSocketPeer } from "cojson-transport-ws";
|
6
|
+
import NetInfo from "@react-native-community/netinfo";
|
7
|
+
import * as Linking from "expo-linking";
|
8
|
+
export { RNDemoAuth } from "./auth/DemoAuthMethod.js";
|
9
|
+
import { NativeStorageContext } from "./native-storage.js";
|
10
|
+
export async function createJazzRNContext(options) {
|
11
|
+
const firstWsPeer = createWebSocketPeer({
|
12
|
+
websocket: new WebSocket(options.peer),
|
13
|
+
id: options.peer + "@" + new Date().toISOString(),
|
14
|
+
role: "server",
|
15
|
+
expectPings: true,
|
16
|
+
});
|
17
|
+
let shouldTryToReconnect = true;
|
18
|
+
let currentReconnectionTimeout = options.reconnectionTimeout || 500;
|
19
|
+
const unsubscribeNetworkChange = NetInfo.addEventListener((state) => {
|
20
|
+
if (state.isConnected) {
|
21
|
+
currentReconnectionTimeout = options.reconnectionTimeout || 500;
|
22
|
+
}
|
23
|
+
});
|
24
|
+
const context = "auth" in options
|
25
|
+
? await createJazzContext({
|
26
|
+
AccountSchema: options.AccountSchema,
|
27
|
+
auth: options.auth,
|
28
|
+
crypto: await PureJSCrypto.create(),
|
29
|
+
peersToLoadFrom: [firstWsPeer],
|
30
|
+
sessionProvider: provideLockSession,
|
31
|
+
})
|
32
|
+
: await createJazzContext({
|
33
|
+
crypto: await PureJSCrypto.create(),
|
34
|
+
peersToLoadFrom: [firstWsPeer],
|
35
|
+
});
|
36
|
+
const node = "account" in context
|
37
|
+
? context.account._raw.core.node
|
38
|
+
: context.agent.node;
|
39
|
+
async function websocketReconnectLoop() {
|
40
|
+
while (shouldTryToReconnect) {
|
41
|
+
if (Object.keys(node.syncManager.peers).some((peerId) => peerId.includes(options.peer))) {
|
42
|
+
// TODO: this might drain battery, use listeners instead
|
43
|
+
await new Promise((resolve) => setTimeout(resolve, 100));
|
44
|
+
}
|
45
|
+
else {
|
46
|
+
console.log("Websocket disconnected, trying to reconnect in " +
|
47
|
+
currentReconnectionTimeout +
|
48
|
+
"ms");
|
49
|
+
currentReconnectionTimeout = Math.min(currentReconnectionTimeout * 2, 30000);
|
50
|
+
await new Promise((resolve) => {
|
51
|
+
setTimeout(resolve, currentReconnectionTimeout);
|
52
|
+
const _unsubscribeNetworkChange = NetInfo.addEventListener((state) => {
|
53
|
+
if (state.isConnected) {
|
54
|
+
resolve();
|
55
|
+
_unsubscribeNetworkChange();
|
56
|
+
}
|
57
|
+
});
|
58
|
+
});
|
59
|
+
node.syncManager.addPeer(createWebSocketPeer({
|
60
|
+
websocket: new WebSocket(options.peer),
|
61
|
+
id: options.peer + "@" + new Date().toISOString(),
|
62
|
+
role: "server",
|
63
|
+
}));
|
64
|
+
}
|
65
|
+
}
|
66
|
+
}
|
67
|
+
void websocketReconnectLoop();
|
68
|
+
return "account" in context
|
69
|
+
? {
|
70
|
+
me: context.account,
|
71
|
+
done: () => {
|
72
|
+
shouldTryToReconnect = false;
|
73
|
+
unsubscribeNetworkChange?.();
|
74
|
+
context.done();
|
75
|
+
},
|
76
|
+
logOut: () => {
|
77
|
+
context.logOut();
|
78
|
+
},
|
79
|
+
}
|
80
|
+
: {
|
81
|
+
guest: context.agent,
|
82
|
+
done: () => {
|
83
|
+
shouldTryToReconnect = false;
|
84
|
+
unsubscribeNetworkChange?.();
|
85
|
+
context.done();
|
86
|
+
},
|
87
|
+
logOut: () => {
|
88
|
+
context.logOut();
|
89
|
+
},
|
90
|
+
};
|
91
|
+
}
|
92
|
+
export async function provideLockSession(accountID, crypto) {
|
93
|
+
const sessionDone = () => { };
|
94
|
+
const storage = NativeStorageContext.getInstance().getStorage();
|
95
|
+
const sessionID = (await storage.get(accountID)) ||
|
96
|
+
crypto.newRandomSessionID(accountID);
|
97
|
+
await storage.set(accountID, sessionID);
|
98
|
+
return Promise.resolve({
|
99
|
+
sessionID,
|
100
|
+
sessionDone,
|
101
|
+
});
|
102
|
+
}
|
103
|
+
const window = {
|
104
|
+
location: {
|
105
|
+
href: "#",
|
106
|
+
},
|
107
|
+
history: {
|
108
|
+
replaceState: (a, b, c) => { },
|
109
|
+
},
|
110
|
+
};
|
111
|
+
/** @category Invite Links */
|
112
|
+
export function createInviteLink(value, role, { baseURL, valueHint } = {}) {
|
113
|
+
const coValueCore = value._raw.core;
|
114
|
+
let currentCoValue = coValueCore;
|
115
|
+
while (currentCoValue.header.ruleset.type === "ownedByGroup") {
|
116
|
+
currentCoValue = currentCoValue.getGroup().core;
|
117
|
+
}
|
118
|
+
if (currentCoValue.header.ruleset.type !== "group") {
|
119
|
+
throw new Error("Can't create invite link for object without group");
|
120
|
+
}
|
121
|
+
const group = cojsonInternals.expectGroup(currentCoValue.getCurrentContent());
|
122
|
+
const inviteSecret = group.createInvite(role);
|
123
|
+
return `${baseURL}/invite/${valueHint ? valueHint + "/" : ""}${value.id}/${inviteSecret}`;
|
124
|
+
}
|
125
|
+
/** @category Invite Links */
|
126
|
+
export function parseInviteLink(inviteURL) {
|
127
|
+
const url = Linking.parse(inviteURL);
|
128
|
+
const parts = url.path?.split("/");
|
129
|
+
if (!parts || parts[0] !== "invite") {
|
130
|
+
return undefined;
|
131
|
+
}
|
132
|
+
let valueHint;
|
133
|
+
let valueID;
|
134
|
+
let inviteSecret;
|
135
|
+
if (parts.length === 4) {
|
136
|
+
valueHint = parts[1];
|
137
|
+
valueID = parts[2];
|
138
|
+
inviteSecret = parts[3];
|
139
|
+
}
|
140
|
+
else if (parts.length === 3) {
|
141
|
+
valueID = parts[1];
|
142
|
+
inviteSecret = parts[2];
|
143
|
+
}
|
144
|
+
if (!valueID || !inviteSecret) {
|
145
|
+
return undefined;
|
146
|
+
}
|
147
|
+
return { valueID, inviteSecret, valueHint };
|
148
|
+
}
|
149
|
+
/////////
|
150
|
+
export * from "./provider.js";
|
151
|
+
export * from "./auth/auth.js";
|
152
|
+
export * from "./native-storage.js";
|
153
|
+
//# sourceMappingURL=index.js.map
|