@@ -18,6 +18,9 @@ export const getKeyObject = async () => {
18
18
if ( error . code === 401 && error . type === 'general_unauthorized_scope' ) {
19
19
console . log ( 'No existing Account' )
20
20
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 )
21
24
} else {
22
25
throw error
23
26
}
@@ -59,6 +62,21 @@ function executeLinkAnonFunction(functions) {
59
62
)
60
63
}
61
64
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
+
62
80
/**
63
81
* Creates a new anonymous user & session.
64
82
* Passes domain & configured policy to specific appwrite function
@@ -69,14 +87,7 @@ function executeLinkAnonFunction(functions) {
69
87
const creatNewAnonUser = async ( account , client ) => {
70
88
try {
71
89
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 )
80
91
} catch ( error ) {
81
92
console . error ( 'Error creating anonymous user: ' , error )
82
93
throw error
0 commit comments