Skip to content

Commit 83d11ad

Browse files
fix: handle existing users without key (#473)
2 parents 6a44b4b + f667c57 commit 83d11ad

File tree

1 file changed

+19
-8
lines changed

1 file changed

+19
-8
lines changed

src/support/appwrite-utils.js

Lines changed: 19 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,9 @@ export const getKeyObject = async () => {
1818
if (error.code === 401 && error.type === 'general_unauthorized_scope') {
1919
console.log('No existing Account')
2020
return await creatNewAnonUser(account, client)
21+
} else if (error.code === 404 && error.type === 'document_not_found') {
22+
console.log('No anon key found')
23+
return await createAnonKey(client)
2124
} else {
2225
throw error
2326
}
@@ -59,6 +62,21 @@ function executeLinkAnonFunction(functions) {
5962
)
6063
}
6164

65+
/**
66+
* Runs anon key creation function with retry
67+
* @param {Client} client
68+
*/
69+
async function createAnonKey(client) {
70+
const functions = new Functions(client)
71+
72+
let res = await executeLinkAnonFunction(functions)
73+
74+
if (res.status === 'failed' && res.responseStatusCode === 500) {
75+
res = await executeLinkAnonFunction(functions)
76+
}
77+
return JSON.parse(res.responseBody)
78+
}
79+
6280
/**
6381
* Creates a new anonymous user & session.
6482
* Passes domain & configured policy to specific appwrite function
@@ -69,14 +87,7 @@ function executeLinkAnonFunction(functions) {
6987
const creatNewAnonUser = async (account, client) => {
7088
try {
7189
await account.createAnonymousSession()
72-
const functions = new Functions(client)
73-
74-
let res = await executeLinkAnonFunction(functions)
75-
76-
if (res.status === 'failed' && res.responseStatusCode === 500) {
77-
res = await executeLinkAnonFunction(functions)
78-
}
79-
return JSON.parse(res.responseBody)
90+
return await createAnonKey(client)
8091
} catch (error) {
8192
console.error('Error creating anonymous user: ', error)
8293
throw error

0 commit comments

Comments
 (0)