jazz-react-native 0.8.15 → 0.8.16
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/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
@@ -3,217 +3,198 @@ import { Account, AuthMethod, AuthResult, ID } from "jazz-tools";
|
|
3
3
|
import { KvStore, KvStoreContext } from "../storage/kv-store-context.js";
|
4
4
|
|
5
5
|
type StorageData = {
|
6
|
-
|
7
|
-
|
6
|
+
accountID: ID<Account>;
|
7
|
+
accountSecret: AgentSecret;
|
8
8
|
};
|
9
9
|
|
10
10
|
/** @category Auth Providers */
|
11
11
|
// eslint-disable-next-line @typescript-eslint/no-namespace
|
12
12
|
export namespace RNDemoAuth {
|
13
|
-
|
14
|
-
|
15
|
-
|
16
|
-
|
17
|
-
|
18
|
-
|
19
|
-
|
20
|
-
|
21
|
-
|
13
|
+
export interface Driver {
|
14
|
+
onReady: (next: {
|
15
|
+
signUp: (username: string) => Promise<void>;
|
16
|
+
getExistingUsers: () => Promise<string[]>;
|
17
|
+
logInAs: (existingUser: string) => Promise<void>;
|
18
|
+
}) => void;
|
19
|
+
onSignedIn: (next: { logOut: () => void }) => void;
|
20
|
+
onError: (error: string | Error) => void;
|
21
|
+
}
|
22
22
|
}
|
23
23
|
|
24
24
|
const localStorageKey = "demo-auth-logged-in-secret";
|
25
25
|
|
26
26
|
export class RNDemoAuth implements AuthMethod {
|
27
|
-
|
28
|
-
|
29
|
-
|
30
|
-
|
27
|
+
private constructor(
|
28
|
+
private driver: RNDemoAuth.Driver,
|
29
|
+
private kvStore: KvStore,
|
30
|
+
) {}
|
31
31
|
|
32
|
-
|
33
|
-
|
34
|
-
|
35
|
-
|
36
|
-
|
37
|
-
|
38
|
-
|
39
|
-
|
40
|
-
|
41
|
-
|
42
|
-
|
43
|
-
|
44
|
-
|
45
|
-
|
46
|
-
|
47
|
-
|
48
|
-
|
49
|
-
|
50
|
-
|
51
|
-
|
52
|
-
|
53
|
-
|
54
|
-
|
55
|
-
|
56
|
-
|
57
|
-
|
58
|
-
|
59
|
-
existingUsers + "," + name,
|
60
|
-
);
|
61
|
-
} else {
|
62
|
-
await kvStore.set("demo-auth-existing-users", name);
|
63
|
-
}
|
64
|
-
}
|
65
|
-
await kvStore.set("demo-auth-existing-users-" + name, storageData);
|
32
|
+
public static async init(
|
33
|
+
driver: RNDemoAuth.Driver,
|
34
|
+
seedAccounts?: {
|
35
|
+
[name: string]: {
|
36
|
+
accountID: ID<Account>;
|
37
|
+
accountSecret: AgentSecret;
|
38
|
+
};
|
39
|
+
},
|
40
|
+
) {
|
41
|
+
const kvStore = KvStoreContext.getInstance().getStorage();
|
42
|
+
for (const [name, credentials] of Object.entries(seedAccounts || {})) {
|
43
|
+
const storageData = JSON.stringify(credentials satisfies StorageData);
|
44
|
+
if (
|
45
|
+
!(
|
46
|
+
(await kvStore.get("demo-auth-existing-users"))?.split(",") as
|
47
|
+
| string[]
|
48
|
+
| undefined
|
49
|
+
)?.includes(name)
|
50
|
+
) {
|
51
|
+
const existingUsers = await kvStore.get("demo-auth-existing-users");
|
52
|
+
if (existingUsers) {
|
53
|
+
await kvStore.set(
|
54
|
+
"demo-auth-existing-users",
|
55
|
+
existingUsers + "," + name,
|
56
|
+
);
|
57
|
+
} else {
|
58
|
+
await kvStore.set("demo-auth-existing-users", name);
|
66
59
|
}
|
67
|
-
|
60
|
+
}
|
61
|
+
await kvStore.set("demo-auth-existing-users-" + name, storageData);
|
68
62
|
}
|
63
|
+
return new RNDemoAuth(driver, kvStore);
|
64
|
+
}
|
69
65
|
|
70
|
-
|
71
|
-
|
72
|
-
|
73
|
-
|
74
|
-
|
75
|
-
|
66
|
+
async start() {
|
67
|
+
try {
|
68
|
+
if (await this.kvStore.get(localStorageKey)) {
|
69
|
+
const localStorageData = JSON.parse(
|
70
|
+
(await this.kvStore.get(localStorageKey)) ?? "{}",
|
71
|
+
) as StorageData;
|
76
72
|
|
77
|
-
|
78
|
-
|
73
|
+
const accountID = localStorageData.accountID as ID<Account>;
|
74
|
+
const secret = localStorageData.accountSecret;
|
79
75
|
|
80
|
-
|
81
|
-
|
82
|
-
|
83
|
-
|
84
|
-
|
85
|
-
|
86
|
-
|
87
|
-
|
88
|
-
|
89
|
-
|
90
|
-
|
91
|
-
|
92
|
-
|
93
|
-
|
94
|
-
|
95
|
-
|
96
|
-
|
97
|
-
|
98
|
-
|
99
|
-
|
100
|
-
|
101
|
-
|
102
|
-
|
103
|
-
|
104
|
-
|
105
|
-
|
106
|
-
|
107
|
-
|
108
|
-
|
76
|
+
return {
|
77
|
+
type: "existing",
|
78
|
+
credentials: { accountID, secret },
|
79
|
+
onSuccess: () => {
|
80
|
+
this.driver.onSignedIn({ logOut });
|
81
|
+
},
|
82
|
+
onError: (error: string | Error) => {
|
83
|
+
this.driver.onError(error);
|
84
|
+
},
|
85
|
+
logOut: async () => {
|
86
|
+
void (await this.kvStore.delete(localStorageKey));
|
87
|
+
},
|
88
|
+
} satisfies AuthResult;
|
89
|
+
} else {
|
90
|
+
return new Promise<AuthResult>((resolve) => {
|
91
|
+
this.driver.onReady({
|
92
|
+
// @ts-expect-error asd
|
93
|
+
signUp: (username: string) => {
|
94
|
+
resolve({
|
95
|
+
type: "new",
|
96
|
+
creationProps: { name: username },
|
97
|
+
saveCredentials: async (credentials: {
|
98
|
+
accountID: ID<Account>;
|
99
|
+
secret: AgentSecret;
|
100
|
+
}) => {
|
101
|
+
const storageData = JSON.stringify({
|
102
|
+
accountID: credentials.accountID,
|
103
|
+
accountSecret: credentials.secret,
|
104
|
+
} satisfies StorageData);
|
109
105
|
|
110
|
-
|
111
|
-
|
112
|
-
|
113
|
-
|
114
|
-
|
115
|
-
|
116
|
-
|
117
|
-
: [];
|
106
|
+
// Retrieve the list of existing users
|
107
|
+
const existingUsers = await this.kvStore.get(
|
108
|
+
"demo-auth-existing-users",
|
109
|
+
);
|
110
|
+
const existingUsernames = existingUsers
|
111
|
+
? existingUsers.split(",")
|
112
|
+
: [];
|
118
113
|
|
119
|
-
|
120
|
-
|
121
|
-
|
122
|
-
|
123
|
-
|
124
|
-
|
125
|
-
|
126
|
-
) {
|
127
|
-
counter++;
|
128
|
-
uniqueUsername = `${username}-${counter}`;
|
129
|
-
}
|
114
|
+
// Determine if the username already exists and generate a unique username
|
115
|
+
let uniqueUsername = username;
|
116
|
+
let counter = 1;
|
117
|
+
while (existingUsernames.includes(uniqueUsername)) {
|
118
|
+
counter++;
|
119
|
+
uniqueUsername = `${username}-${counter}`;
|
120
|
+
}
|
130
121
|
|
131
|
-
|
132
|
-
|
133
|
-
|
134
|
-
|
135
|
-
|
136
|
-
|
137
|
-
"demo-auth-existing-users-" +
|
138
|
-
uniqueUsername,
|
139
|
-
storageData,
|
140
|
-
);
|
122
|
+
// Save credentials using the unique username
|
123
|
+
await this.kvStore.set(localStorageKey, storageData);
|
124
|
+
await this.kvStore.set(
|
125
|
+
"demo-auth-existing-users-" + uniqueUsername,
|
126
|
+
storageData,
|
127
|
+
);
|
141
128
|
|
142
|
-
|
143
|
-
|
144
|
-
|
145
|
-
|
146
|
-
|
147
|
-
|
148
|
-
|
149
|
-
|
150
|
-
|
151
|
-
|
152
|
-
|
153
|
-
|
154
|
-
|
155
|
-
|
156
|
-
|
157
|
-
|
158
|
-
|
159
|
-
|
160
|
-
|
161
|
-
|
162
|
-
|
163
|
-
|
164
|
-
|
165
|
-
|
166
|
-
|
167
|
-
|
168
|
-
|
169
|
-
|
170
|
-
|
171
|
-
|
172
|
-
|
173
|
-
|
174
|
-
|
175
|
-
|
176
|
-
|
177
|
-
(await this.kvStore.get(
|
178
|
-
"demo-auth-existing-users-" + existingUser,
|
179
|
-
)) ?? "{}",
|
180
|
-
) as StorageData;
|
129
|
+
// Update the list of existing users
|
130
|
+
const updatedUsers = existingUsers
|
131
|
+
? `${existingUsers},${uniqueUsername}`
|
132
|
+
: uniqueUsername;
|
133
|
+
await this.kvStore.set(
|
134
|
+
"demo-auth-existing-users",
|
135
|
+
updatedUsers,
|
136
|
+
);
|
137
|
+
},
|
138
|
+
onSuccess: () => {
|
139
|
+
this.driver.onSignedIn({ logOut });
|
140
|
+
},
|
141
|
+
onError: (error: string | Error) => {
|
142
|
+
// @ts-expect-error asd
|
143
|
+
console.error("onError", error.cause);
|
144
|
+
this.driver.onError(error);
|
145
|
+
},
|
146
|
+
logOut: async () => {
|
147
|
+
void (await this.kvStore.delete(localStorageKey));
|
148
|
+
},
|
149
|
+
});
|
150
|
+
},
|
151
|
+
getExistingUsers: async () => {
|
152
|
+
return (
|
153
|
+
(await this.kvStore.get("demo-auth-existing-users"))?.split(
|
154
|
+
",",
|
155
|
+
) ?? []
|
156
|
+
);
|
157
|
+
},
|
158
|
+
logInAs: async (existingUser) => {
|
159
|
+
const storageData = JSON.parse(
|
160
|
+
(await this.kvStore.get(
|
161
|
+
"demo-auth-existing-users-" + existingUser,
|
162
|
+
)) ?? "{}",
|
163
|
+
) as StorageData;
|
181
164
|
|
182
|
-
|
183
|
-
|
184
|
-
|
185
|
-
|
165
|
+
await this.kvStore.set(
|
166
|
+
localStorageKey,
|
167
|
+
JSON.stringify(storageData),
|
168
|
+
);
|
186
169
|
|
187
|
-
|
188
|
-
|
189
|
-
|
190
|
-
|
191
|
-
|
192
|
-
|
193
|
-
|
194
|
-
|
195
|
-
|
196
|
-
|
197
|
-
|
198
|
-
|
199
|
-
|
200
|
-
|
201
|
-
|
202
|
-
|
203
|
-
|
204
|
-
|
205
|
-
|
206
|
-
|
207
|
-
|
208
|
-
|
209
|
-
|
210
|
-
console.error("error", error);
|
211
|
-
throw error;
|
212
|
-
}
|
170
|
+
resolve({
|
171
|
+
type: "existing",
|
172
|
+
credentials: {
|
173
|
+
accountID: storageData.accountID,
|
174
|
+
secret: storageData.accountSecret,
|
175
|
+
},
|
176
|
+
onSuccess: () => {
|
177
|
+
this.driver.onSignedIn({ logOut });
|
178
|
+
},
|
179
|
+
onError: (error: string | Error) => {
|
180
|
+
this.driver.onError(error);
|
181
|
+
},
|
182
|
+
logOut: async () => {
|
183
|
+
void (await this.kvStore.delete(localStorageKey));
|
184
|
+
},
|
185
|
+
});
|
186
|
+
},
|
187
|
+
});
|
188
|
+
});
|
189
|
+
}
|
190
|
+
} catch (error) {
|
191
|
+
console.error("error", error);
|
192
|
+
throw error;
|
213
193
|
}
|
194
|
+
}
|
214
195
|
}
|
215
196
|
|
216
197
|
async function logOut() {
|
217
|
-
|
218
|
-
|
198
|
+
const kvStore = KvStoreContext.getInstance().getStorage();
|
199
|
+
void (await kvStore.delete(localStorageKey));
|
219
200
|
}
|