jazz-react-native 0.8.15 → 0.8.16
Sign up to get free protection for your applications and to get access to all the features.
- package/CHANGELOG.md +466 -457
- package/dist/auth/DemoAuthMethod.js +1 -2
- package/dist/auth/DemoAuthMethod.js.map +1 -1
- package/dist/auth/DemoAuthUI.d.ts +1 -1
- package/dist/auth/DemoAuthUI.js +25 -31
- package/dist/auth/DemoAuthUI.js.map +1 -1
- package/dist/index.d.ts +2 -2
- package/dist/index.js +3 -5
- package/dist/index.js.map +1 -1
- package/dist/media.d.ts +1 -1
- package/dist/media.js.map +1 -1
- package/dist/provider.js +2 -4
- package/dist/provider.js.map +1 -1
- package/dist/storage/expo-secure-store-adapter.js.map +1 -1
- package/dist/storage/kv-store-context.js.map +1 -1
- package/package.json +7 -11
- package/src/auth/DemoAuthMethod.ts +168 -187
- package/src/auth/DemoAuthUI.tsx +239 -253
- package/src/index.ts +198 -202
- package/src/media.tsx +50 -51
- package/src/provider.tsx +265 -274
- package/src/storage/expo-secure-store-adapter.ts +21 -21
- package/src/storage/kv-store-context.ts +21 -21
- package/tsconfig.json +4 -8
- package/.eslintrc.cjs +0 -24
- package/.prettierrc.js +0 -9
package/src/auth/DemoAuthUI.tsx
CHANGED
@@ -1,289 +1,275 @@
|
|
1
|
+
import { AgentSecret } from "cojson";
|
2
|
+
import { Account, ID } from "jazz-tools";
|
1
3
|
import React, { useMemo, useState, useEffect } from "react";
|
2
4
|
import {
|
3
|
-
|
4
|
-
|
5
|
-
|
6
|
-
|
7
|
-
|
5
|
+
StyleSheet,
|
6
|
+
Text,
|
7
|
+
TextInput,
|
8
|
+
TouchableOpacity,
|
9
|
+
View,
|
8
10
|
} from "react-native";
|
9
|
-
import { AgentSecret } from "cojson";
|
10
|
-
import { Account, ID } from "jazz-tools";
|
11
11
|
import { RNDemoAuth } from "./DemoAuthMethod.js";
|
12
12
|
|
13
13
|
type DemoAuthState = (
|
14
|
-
|
15
|
-
|
16
|
-
|
17
|
-
|
18
|
-
|
19
|
-
|
20
|
-
|
21
|
-
|
22
|
-
|
23
|
-
|
24
|
-
|
25
|
-
|
26
|
-
|
27
|
-
|
28
|
-
|
29
|
-
|
14
|
+
| {
|
15
|
+
state: "uninitialized";
|
16
|
+
}
|
17
|
+
| {
|
18
|
+
state: "loading";
|
19
|
+
}
|
20
|
+
| {
|
21
|
+
state: "ready";
|
22
|
+
existingUsers: string[];
|
23
|
+
signUp: (username: string) => void;
|
24
|
+
logInAs: (existingUser: string) => void;
|
25
|
+
}
|
26
|
+
| {
|
27
|
+
state: "signedIn";
|
28
|
+
logOut: () => void;
|
29
|
+
}
|
30
30
|
) & {
|
31
|
-
|
31
|
+
errors: string[];
|
32
32
|
};
|
33
33
|
|
34
34
|
/** @category Auth Providers */
|
35
35
|
export function useDemoAuth({
|
36
|
-
|
36
|
+
seedAccounts,
|
37
37
|
}: {
|
38
|
-
|
39
|
-
|
40
|
-
|
38
|
+
seedAccounts?: {
|
39
|
+
[name: string]: { accountID: ID<Account>; accountSecret: AgentSecret };
|
40
|
+
};
|
41
41
|
} = {}) {
|
42
|
-
|
43
|
-
|
44
|
-
|
45
|
-
|
42
|
+
const [state, setState] = useState<DemoAuthState>({
|
43
|
+
state: "loading",
|
44
|
+
errors: [],
|
45
|
+
});
|
46
46
|
|
47
|
-
|
47
|
+
const [authMethod, setAuthMethod] = useState<RNDemoAuth | null>(null);
|
48
48
|
|
49
|
-
|
50
|
-
|
51
|
-
|
52
|
-
|
53
|
-
|
54
|
-
|
55
|
-
|
56
|
-
|
57
|
-
|
58
|
-
|
59
|
-
|
60
|
-
|
61
|
-
|
62
|
-
|
63
|
-
|
64
|
-
|
65
|
-
|
66
|
-
|
67
|
-
|
68
|
-
|
69
|
-
|
70
|
-
|
71
|
-
|
72
|
-
|
73
|
-
|
74
|
-
|
49
|
+
const authMethodPromise = useMemo(() => {
|
50
|
+
return RNDemoAuth.init(
|
51
|
+
{
|
52
|
+
onReady: async ({ signUp, getExistingUsers, logInAs }) => {
|
53
|
+
const existingUsers = await getExistingUsers();
|
54
|
+
setState({
|
55
|
+
state: "ready",
|
56
|
+
signUp,
|
57
|
+
existingUsers,
|
58
|
+
logInAs,
|
59
|
+
errors: [],
|
60
|
+
});
|
61
|
+
},
|
62
|
+
onSignedIn: ({ logOut }) => {
|
63
|
+
setState({ state: "signedIn", logOut, errors: [] });
|
64
|
+
},
|
65
|
+
onError: (error) => {
|
66
|
+
setState((current) => ({
|
67
|
+
...current,
|
68
|
+
errors: [...current.errors, error.toString()],
|
69
|
+
}));
|
70
|
+
},
|
71
|
+
},
|
72
|
+
seedAccounts,
|
73
|
+
);
|
74
|
+
}, [seedAccounts]);
|
75
75
|
|
76
|
-
|
77
|
-
|
78
|
-
|
79
|
-
|
80
|
-
|
81
|
-
|
82
|
-
|
83
|
-
|
76
|
+
useEffect(() => {
|
77
|
+
async function init() {
|
78
|
+
const auth = await authMethodPromise;
|
79
|
+
setAuthMethod(auth);
|
80
|
+
}
|
81
|
+
if (authMethod) return;
|
82
|
+
void init();
|
83
|
+
}, [seedAccounts]);
|
84
84
|
|
85
|
-
|
85
|
+
return [authMethod, state] as const;
|
86
86
|
}
|
87
87
|
|
88
88
|
export const DemoAuthBasicUI = ({
|
89
|
-
|
90
|
-
|
89
|
+
appName,
|
90
|
+
state,
|
91
91
|
}: {
|
92
|
-
|
93
|
-
|
92
|
+
appName: string;
|
93
|
+
state: DemoAuthState;
|
94
94
|
}) => {
|
95
|
-
|
96
|
-
|
97
|
-
|
95
|
+
const darkMode = false;
|
96
|
+
const [username, setUsername] = useState<string>("");
|
97
|
+
const [errorMessage, setErrorMessage] = useState<string | null>(null);
|
98
98
|
|
99
|
-
|
100
|
-
|
101
|
-
|
102
|
-
|
103
|
-
|
104
|
-
|
105
|
-
|
106
|
-
|
107
|
-
|
99
|
+
const handleSignUp = () => {
|
100
|
+
if (state.state !== "ready") return;
|
101
|
+
if (username.trim() === "") {
|
102
|
+
setErrorMessage("Display name is required");
|
103
|
+
} else {
|
104
|
+
setErrorMessage(null);
|
105
|
+
state.signUp(username);
|
106
|
+
}
|
107
|
+
};
|
108
108
|
|
109
|
-
|
110
|
-
|
109
|
+
return (
|
110
|
+
<View
|
111
|
+
style={[
|
112
|
+
styles.container,
|
113
|
+
darkMode ? styles.darkBackground : styles.lightBackground,
|
114
|
+
]}
|
115
|
+
>
|
116
|
+
{state.state === "loading" ? (
|
117
|
+
<Text style={styles.loadingText}>Loading...</Text>
|
118
|
+
) : state.state === "ready" ? (
|
119
|
+
<View style={styles.formContainer}>
|
120
|
+
<Text
|
111
121
|
style={[
|
112
|
-
|
113
|
-
|
122
|
+
styles.headerText,
|
123
|
+
darkMode ? styles.darkText : styles.lightText,
|
114
124
|
]}
|
115
|
-
|
116
|
-
{
|
117
|
-
|
118
|
-
) : state.state === "ready" ? (
|
119
|
-
<View style={styles.formContainer}>
|
120
|
-
<Text
|
121
|
-
style={[
|
122
|
-
styles.headerText,
|
123
|
-
darkMode ? styles.darkText : styles.lightText,
|
124
|
-
]}
|
125
|
-
>
|
126
|
-
{appName}
|
127
|
-
</Text>
|
125
|
+
>
|
126
|
+
{appName}
|
127
|
+
</Text>
|
128
128
|
|
129
|
-
|
130
|
-
|
131
|
-
|
132
|
-
|
133
|
-
|
129
|
+
{state.errors.map((error) => (
|
130
|
+
<Text key={error} style={styles.errorText}>
|
131
|
+
{error}
|
132
|
+
</Text>
|
133
|
+
))}
|
134
134
|
|
135
|
-
|
136
|
-
<Text style={styles.errorText}>{errorMessage}</Text>
|
137
|
-
)}
|
135
|
+
{errorMessage && <Text style={styles.errorText}>{errorMessage}</Text>}
|
138
136
|
|
139
|
-
|
140
|
-
|
141
|
-
|
142
|
-
|
143
|
-
|
144
|
-
|
145
|
-
|
146
|
-
|
147
|
-
|
148
|
-
|
137
|
+
<TextInput
|
138
|
+
placeholder="Display name"
|
139
|
+
value={username}
|
140
|
+
onChangeText={setUsername}
|
141
|
+
placeholderTextColor={darkMode ? "#fff" : "#000"}
|
142
|
+
style={[
|
143
|
+
styles.textInput,
|
144
|
+
darkMode ? styles.darkInput : styles.lightInput,
|
145
|
+
]}
|
146
|
+
/>
|
149
147
|
|
150
|
-
|
151
|
-
|
152
|
-
|
153
|
-
|
154
|
-
|
155
|
-
|
156
|
-
|
157
|
-
|
158
|
-
|
159
|
-
|
160
|
-
|
161
|
-
|
162
|
-
|
163
|
-
>
|
164
|
-
Sign Up as new account
|
165
|
-
</Text>
|
166
|
-
</TouchableOpacity>
|
148
|
+
<TouchableOpacity
|
149
|
+
onPress={handleSignUp}
|
150
|
+
style={[
|
151
|
+
styles.button,
|
152
|
+
darkMode ? styles.darkButton : styles.lightButton,
|
153
|
+
]}
|
154
|
+
>
|
155
|
+
<Text
|
156
|
+
style={darkMode ? styles.darkButtonText : styles.lightButtonText}
|
157
|
+
>
|
158
|
+
Sign Up as new account
|
159
|
+
</Text>
|
160
|
+
</TouchableOpacity>
|
167
161
|
|
168
|
-
|
169
|
-
|
170
|
-
|
171
|
-
|
172
|
-
|
173
|
-
|
174
|
-
|
175
|
-
|
176
|
-
|
177
|
-
|
178
|
-
|
179
|
-
|
180
|
-
|
181
|
-
|
182
|
-
|
183
|
-
|
184
|
-
: styles.lightText
|
185
|
-
}
|
186
|
-
>
|
187
|
-
Log In as "{user}"
|
188
|
-
</Text>
|
189
|
-
</TouchableOpacity>
|
190
|
-
))}
|
191
|
-
</View>
|
192
|
-
</View>
|
193
|
-
) : null}
|
162
|
+
<View style={styles.existingUsersContainer}>
|
163
|
+
{state.existingUsers.map((user) => (
|
164
|
+
<TouchableOpacity
|
165
|
+
key={user}
|
166
|
+
onPress={() => state.logInAs(user)}
|
167
|
+
style={[
|
168
|
+
styles.existingUserButton,
|
169
|
+
darkMode ? styles.darkUserButton : styles.lightUserButton,
|
170
|
+
]}
|
171
|
+
>
|
172
|
+
<Text style={darkMode ? styles.darkText : styles.lightText}>
|
173
|
+
Log In as "{user}"
|
174
|
+
</Text>
|
175
|
+
</TouchableOpacity>
|
176
|
+
))}
|
177
|
+
</View>
|
194
178
|
</View>
|
195
|
-
|
179
|
+
) : null}
|
180
|
+
</View>
|
181
|
+
);
|
196
182
|
};
|
197
183
|
|
198
184
|
const styles = StyleSheet.create({
|
199
|
-
|
200
|
-
|
201
|
-
|
202
|
-
|
203
|
-
|
204
|
-
|
205
|
-
|
206
|
-
|
207
|
-
|
208
|
-
|
209
|
-
|
210
|
-
|
211
|
-
|
212
|
-
|
213
|
-
|
214
|
-
|
215
|
-
|
216
|
-
|
217
|
-
|
218
|
-
|
219
|
-
|
220
|
-
|
221
|
-
|
222
|
-
|
223
|
-
|
224
|
-
|
225
|
-
|
226
|
-
|
227
|
-
|
228
|
-
|
229
|
-
|
230
|
-
|
231
|
-
|
232
|
-
|
233
|
-
|
234
|
-
|
235
|
-
|
236
|
-
|
237
|
-
|
238
|
-
|
239
|
-
|
240
|
-
|
241
|
-
|
242
|
-
|
243
|
-
|
244
|
-
|
245
|
-
|
246
|
-
|
247
|
-
|
248
|
-
|
249
|
-
|
250
|
-
|
251
|
-
|
252
|
-
|
253
|
-
|
254
|
-
|
255
|
-
|
256
|
-
|
257
|
-
|
258
|
-
|
259
|
-
|
260
|
-
|
261
|
-
|
262
|
-
|
263
|
-
|
264
|
-
|
265
|
-
|
266
|
-
|
267
|
-
|
268
|
-
|
269
|
-
|
270
|
-
|
271
|
-
|
272
|
-
|
273
|
-
|
274
|
-
|
275
|
-
|
276
|
-
|
277
|
-
|
278
|
-
|
279
|
-
|
280
|
-
|
281
|
-
|
282
|
-
|
283
|
-
|
284
|
-
|
285
|
-
|
286
|
-
|
287
|
-
|
288
|
-
|
185
|
+
container: {
|
186
|
+
flex: 1,
|
187
|
+
justifyContent: "center",
|
188
|
+
alignItems: "center",
|
189
|
+
padding: 20,
|
190
|
+
},
|
191
|
+
formContainer: {
|
192
|
+
width: "80%",
|
193
|
+
alignItems: "center",
|
194
|
+
justifyContent: "center",
|
195
|
+
},
|
196
|
+
headerText: {
|
197
|
+
fontSize: 24,
|
198
|
+
marginBottom: 20,
|
199
|
+
},
|
200
|
+
errorText: {
|
201
|
+
color: "red",
|
202
|
+
marginVertical: 5,
|
203
|
+
textAlign: "center",
|
204
|
+
},
|
205
|
+
textInput: {
|
206
|
+
borderWidth: 1,
|
207
|
+
padding: 10,
|
208
|
+
marginVertical: 10,
|
209
|
+
width: "100%",
|
210
|
+
borderRadius: 6,
|
211
|
+
},
|
212
|
+
darkInput: {
|
213
|
+
borderColor: "#444",
|
214
|
+
backgroundColor: "#000",
|
215
|
+
color: "#fff",
|
216
|
+
},
|
217
|
+
lightInput: {
|
218
|
+
borderColor: "#ddd",
|
219
|
+
backgroundColor: "#fff",
|
220
|
+
color: "#000",
|
221
|
+
},
|
222
|
+
button: {
|
223
|
+
paddingVertical: 15,
|
224
|
+
paddingHorizontal: 10,
|
225
|
+
borderRadius: 6,
|
226
|
+
width: "100%",
|
227
|
+
marginVertical: 10,
|
228
|
+
},
|
229
|
+
darkButton: {
|
230
|
+
backgroundColor: "#444",
|
231
|
+
},
|
232
|
+
lightButton: {
|
233
|
+
backgroundColor: "#ddd",
|
234
|
+
},
|
235
|
+
darkButtonText: {
|
236
|
+
color: "#fff",
|
237
|
+
textAlign: "center",
|
238
|
+
},
|
239
|
+
lightButtonText: {
|
240
|
+
color: "#000",
|
241
|
+
textAlign: "center",
|
242
|
+
},
|
243
|
+
existingUsersContainer: {
|
244
|
+
width: "100%",
|
245
|
+
marginTop: 20,
|
246
|
+
},
|
247
|
+
existingUserButton: {
|
248
|
+
paddingVertical: 15,
|
249
|
+
paddingHorizontal: 10,
|
250
|
+
borderRadius: 6,
|
251
|
+
marginVertical: 5,
|
252
|
+
},
|
253
|
+
darkUserButton: {
|
254
|
+
backgroundColor: "#222",
|
255
|
+
},
|
256
|
+
lightUserButton: {
|
257
|
+
backgroundColor: "#eee",
|
258
|
+
},
|
259
|
+
loadingText: {
|
260
|
+
fontSize: 18,
|
261
|
+
color: "#888",
|
262
|
+
},
|
263
|
+
darkText: {
|
264
|
+
color: "#fff",
|
265
|
+
},
|
266
|
+
lightText: {
|
267
|
+
color: "#000",
|
268
|
+
},
|
269
|
+
darkBackground: {
|
270
|
+
backgroundColor: "#000",
|
271
|
+
},
|
272
|
+
lightBackground: {
|
273
|
+
backgroundColor: "#fff",
|
274
|
+
},
|
289
275
|
});
|