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.
@@ -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
- accountID: ID<Account>;
7
- accountSecret: AgentSecret;
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
- 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
- }
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
- private constructor(
28
- private driver: RNDemoAuth.Driver,
29
- private kvStore: KvStore,
30
- ) {}
27
+ private constructor(
28
+ private driver: RNDemoAuth.Driver,
29
+ private kvStore: KvStore,
30
+ ) {}
31
31
 
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(
44
- credentials satisfies StorageData,
45
- );
46
- if (
47
- !(
48
- (await kvStore.get("demo-auth-existing-users"))?.split(
49
- ",",
50
- ) as string[] | undefined
51
- )?.includes(name)
52
- ) {
53
- const existingUsers = await kvStore.get(
54
- "demo-auth-existing-users",
55
- );
56
- if (existingUsers) {
57
- await kvStore.set(
58
- "demo-auth-existing-users",
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
- return new RNDemoAuth(driver, kvStore);
60
+ }
61
+ await kvStore.set("demo-auth-existing-users-" + name, storageData);
68
62
  }
63
+ return new RNDemoAuth(driver, kvStore);
64
+ }
69
65
 
70
- async start() {
71
- try {
72
- if (await this.kvStore.get(localStorageKey)) {
73
- const localStorageData = JSON.parse(
74
- (await this.kvStore.get(localStorageKey)) ?? "{}",
75
- ) as StorageData;
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
- const accountID = localStorageData.accountID as ID<Account>;
78
- const secret = localStorageData.accountSecret;
73
+ const accountID = localStorageData.accountID as ID<Account>;
74
+ const secret = localStorageData.accountSecret;
79
75
 
80
- return {
81
- type: "existing",
82
- credentials: { accountID, secret },
83
- onSuccess: () => {
84
- this.driver.onSignedIn({ logOut });
85
- },
86
- onError: (error: string | Error) => {
87
- this.driver.onError(error);
88
- },
89
- logOut: async () => {
90
- void (await this.kvStore.delete(localStorageKey));
91
- },
92
- } satisfies AuthResult;
93
- } else {
94
- return new Promise<AuthResult>((resolve) => {
95
- this.driver.onReady({
96
- // @ts-expect-error asd
97
- signUp: (username: string) => {
98
- resolve({
99
- type: "new",
100
- creationProps: { name: username },
101
- saveCredentials: async (credentials: {
102
- accountID: ID<Account>;
103
- secret: AgentSecret;
104
- }) => {
105
- const storageData = JSON.stringify({
106
- accountID: credentials.accountID,
107
- accountSecret: credentials.secret,
108
- } satisfies StorageData);
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
- // Retrieve the list of existing users
111
- const existingUsers =
112
- await this.kvStore.get(
113
- "demo-auth-existing-users",
114
- );
115
- const existingUsernames = existingUsers
116
- ? existingUsers.split(",")
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
- // Determine if the username already exists and generate a unique username
120
- let uniqueUsername = username;
121
- let counter = 1;
122
- while (
123
- existingUsernames.includes(
124
- uniqueUsername,
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
- // Save credentials using the unique username
132
- await this.kvStore.set(
133
- localStorageKey,
134
- storageData,
135
- );
136
- await this.kvStore.set(
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
- // Update the list of existing users
143
- const updatedUsers = existingUsers
144
- ? `${existingUsers},${uniqueUsername}`
145
- : uniqueUsername;
146
- await this.kvStore.set(
147
- "demo-auth-existing-users",
148
- updatedUsers,
149
- );
150
- },
151
- onSuccess: () => {
152
- this.driver.onSignedIn({ logOut });
153
- },
154
- onError: (error: string | Error) => {
155
- // @ts-expect-error asd
156
- console.error("onError", error.cause);
157
- this.driver.onError(error);
158
- },
159
- logOut: async () => {
160
- void (await this.kvStore.delete(
161
- localStorageKey,
162
- ));
163
- },
164
- });
165
- },
166
- getExistingUsers: async () => {
167
- return (
168
- (
169
- await this.kvStore.get(
170
- "demo-auth-existing-users",
171
- )
172
- )?.split(",") ?? []
173
- );
174
- },
175
- logInAs: async (existingUser) => {
176
- const storageData = JSON.parse(
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
- await this.kvStore.set(
183
- localStorageKey,
184
- JSON.stringify(storageData),
185
- );
165
+ await this.kvStore.set(
166
+ localStorageKey,
167
+ JSON.stringify(storageData),
168
+ );
186
169
 
187
- resolve({
188
- type: "existing",
189
- credentials: {
190
- accountID: storageData.accountID,
191
- secret: storageData.accountSecret,
192
- },
193
- onSuccess: () => {
194
- this.driver.onSignedIn({ logOut });
195
- },
196
- onError: (error: string | Error) => {
197
- this.driver.onError(error);
198
- },
199
- logOut: async () => {
200
- void (await this.kvStore.delete(
201
- localStorageKey,
202
- ));
203
- },
204
- });
205
- },
206
- });
207
- });
208
- }
209
- } catch (error) {
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
- const kvStore = KvStoreContext.getInstance().getStorage();
218
- void (await kvStore.delete(localStorageKey));
198
+ const kvStore = KvStoreContext.getInstance().getStorage();
199
+ void (await kvStore.delete(localStorageKey));
219
200
  }