#[Solved] trouble deleting users
371 messages · Page 1 of 1 (latest)
What was the results of the Users.delete function?
It says "user role: guests" "account required"
Can you share the code of the function?
I'm calling the function wrong I think. I'm using a http POST
Sure
That's the function I'm using dart for it
And here is how I'm sending the uid to it
What did you put inside the deleteUserFunctionId variable?
The payload I was trying to send
I'm probably doing this all wrong and there's a simple way of passing the uid to the function
This code is inside the App in your flutter project?
The first one is a function folder I deployed and the second one inside my flutter app lib folder both are in my flutter project
Okay, good
Awesome
Not sure if the second one is right but I think the first one is since I followed the documentation for it
I just tried sending the uid string using http as a payload and I think that's the problem
Not sure how other appwriters send uids and other stuff to functions
So you'll need to change a few things.
First you can if you want to use Appwrite flutter package (https://appwrite.io/docs/getting-started-for-flutter)
API Key
The API key let's you access Appwrite resource on a server level https://appwrite.io/docs/sdks#server
That's means that unless you have a very good reason for that you only use this key in server side code as your function.
Flutter
So, In your flutter code (App) when execution a function you should remove the X-Appwrite-key header.
Function
In your function I think that you'll need to to change the line 34 in which you're using the deleteUserFunctionId constant to delete_user_API one
Is this make sense?
Yup makes sense didn't know the header part and I think you're right about changing the deleteUserFunctionId to delete_user_API
Going to try them out now
It can be confusing at start
Just curious but if you know any other way of sending strings to a function and calling a function let me know
Sure
You can add the flutter SDK in your pubspec
dependencies:
appwrite: ^9.0.0
If you're using the cloud then add this version
dependencies:
appwrite: ^8.1.0
Then you can use this code to execute the function
Future result = functions.createExecution(
functionId: '[FUNCTION_ID]',
);
result
.then((response) {
print(response);
}).catchError((error) {
print(error.response);
});
You can see here more about it
https://appwrite.io/docs/client/functions?sdk=flutter-default#functionsCreateExecution
Awesome I saw that in the documentation but I didn't know how to get the data from it in the function
You'll get in the results of the promise in the response variable
So if I execute the function as Future result = functions.createExecution(
functionId: '[FUNCTION_ID]', data: '[USER_ID]'
);
I'd get the data from res
In the function
Yes
But this way is in sync mode in you get your results in the then function
Okay gotcha. Do I have to do res.data or res.payload
You can run it also async (which is my preferred way) like so
try{
final answer = await functions.createExecution(
functionId: '[FUNCTION_ID]', data: '[USER_ID]'
);
} catch (e){
// handle error
}
I didn't know
No it will be inside the data
Where?
I think I've lost a bit
No problemI'm probably confusing you. I know I can send data to the function but I don't know how to use that data for the Users.delete('[USER_ID]') when I use Future result = functions.createExecution(
functionId: '[FUNCTION_ID]', data: '[USER_ID]'
); you said it will be in the response and in the function I see req and res
I thought maybe it would be res.data or maybe it's the same code I'm using now
you mean in the function code?
No
It should work the same
Ohh okay awesome
functions.createExecution is a convinced wrapper around Appwrite REST API
Ahhh gotcha
Switching to createExecution () is helping me narrow down the issue this is what it tells me
Where you did you got this error?
In the function or in the App?
In the app
Maybe if I make the function execute access for all/user/guests it will work?
In your function remove the Guest option leave only the any and try again
Okay
Damn it's still not working
What now?
Same thing but when I do any for roles that should excute I don't get errors displayed but the execution fails and I can't delete
Maybe I shouldn't try making the function in dart
You can do it in any of the supporting languages Appwrite have
All those tabs are empty?
Yup all empty
That means it crashes before
Only thing I see is under errors and says operation timed out
Add
res.json in your catch
Internal curl error
Ohh
That means the address is maybe worng
Ohhh
You've set all your variables like so?
Yes, you need to set them in the functions settings
Go ahead and set them, then access each one of them using the req.variables['variable_name']
Okay
Got it
Wow it worked and I finally got a completed instead of failed
But the user is still in the Auth tab
Cool
Do you any output?
Nope all output is successful
Can you share the function code as text?
I can do pastebin
One sec
Here you go
Pastebin.com is the number one paste tool since 2002. Pastebin is a website where you can store text online for a set period of time.
Oh crap I found the error
I didn't specify the host
In the uri
Yes.
You need to replace those lines
.setEndpoint(req.variables['APPWRITE_FUNCTION_PROJECT_ID'])
.setProject(req.variables['APPWRITE_FUNCTION_ENDPOINT'])
Wow
Yup
Atleast now I know a 100 ways not to make an appwrite cloud function
Thank you for being patient
Weird thing is now it's failing
I'm trying to do the function without async because I remember you telling me "Yes
But this way is in sync mode in you get your results in the then function"
Nope nvm needs to stay async
So what's falling now?
Is this your code?
import 'package:dart_appwrite/dart_appwrite.dart';
Future<void> start(final req, final res) async {
final client = Client();
client
.setEndpoint(req.variables['APPWRITE_FUNCTION_ENDPOINT'])
.setProject(req.variables['APPWRITE_FUNCTION_PROJECT_ID'])
.setKey(req.variables['APPWRITE_FUNCTION_API_KEY']);
final users = Users(client);
try {
final userId = req.payload;
await users.delete(userId: userId);
res.json({'message': 'User deleted'});
} catch (e) {
res.send('Error deleting user: $e', status: 500);
}
}
Eventually they will both work the same
Just imo async is more clear and easy to read.
Yes this is my code. It went back to saying an internal curl error Error Msg operation timed out
Try this
import 'package:dart_appwrite/dart_appwrite.dart';
Future<void> start(final req, final res) async {
final client = Client();
client
.setEndpoint(req.variables['APPWRITE_FUNCTION_ENDPOINT'])
.setProject(req.variables['APPWRITE_FUNCTION_PROJECT_ID'])
.setKey(req.variables['APPWRITE_FUNCTION_API_KEY']);
res.json(req.variables);
/*final users = Users(client);
try {
final userId = req.payload;
await users.delete(userId: userId);
res.json({'message': 'User deleted'});
} catch (e) {
res.send('Error deleting user: $e', status: 500);
}*/
}
So you can verify that all the variables sets
Okay
That was an awesome idea and I think I found the final problem 😀
For the endpoint this is what it looks like
Https://example.com/v1
Everything thing else looks fine
The brackets for the endpoint lol weird
Look
They do / instead of //
///
Let me take a picture
Discord messes it up
Not sure why the backslashes look like that
I don't think the endpoint is the problem. Not sure what I'm doing wrong
Is this your endpoint
Appwrite.test.org or you're using hosts file?
If so add setSelfSigned() to your client
client
.setEndpoint(req.variables['APPWRITE_FUNCTION_ENDPOINT'])
.setProject(req.variables['APPWRITE_FUNCTION_PROJECT_ID'])
.setKey(req.variables['APPWRITE_FUNCTION_API_KEY'])
.setSelfSigned();
Then try again
what's the error?
I'm
Did you put your Appwrite URL in the APPWRITE_FUNCTION_ENDPOINT variable?
The setting it setSelfSigned(status: true) didn't fix it
I put the exact endpoint I get on the appwrite console
Should it be just my url?
So here you've adjusted the value to show appwrite.test.org?
Nope my value is set as appwrite.test.org/v1
So you've set this address locally in your computer?
Nope it's a domain
Yup
i thought you're on Appwrite Cloud 🧐
You own the domain test.org?
what's your code and how long does the execution run for before the error occurs?
So you'll need to put your working Appwrite URL + v1 putting appwrite.test.org make the function point to this url which return nothing and which makes the curl timeout
Pastebin.com is the number one paste tool since 2002. Pastebin is a website where you can store text online for a set period of time.
I'll count how long it takes before throwing the curl error and failing
it should show in the console.
would you please remove the status: 500 in the res.send()?
Is your Appwrite is local or on a remote host?
Okay
So let's assume your Appwrite is https://aw.domain.com
Then set this https://aw.domain.com/v1 to APPWRITE_FUNCTION_ENDPOINT
And try again
Yup its currently set at https://aw.domain.com/v1 sorry for the confusion
You still get timeout?
And you can verify that all the APPWRITE_FUNCTION_ENDPOINT contains your server domain?
Yup i can verify because you helped me res.json(req.variables)
Removed it still had the error
try increasing the timeout in the function settings to 2 minutes
Okay will do and let you guys know in 2 hours
you mean 2 minutes?
It still fails with the same error when I increase the timeout in the function to 2 minutes. When I do res.json(res.variables) everything prints correctly on the appwrite console logs. Not sure what's wrong. Maybe I should try the function in nodejs and not dart?
Let's try from the beginning.
Can you share your Appwrite url, either here or in DM?
so what's the execution time now?
One was 3s and the other was 119ms
What was the result of the 119ms one?
Same error
Internal curl error
I don't think anyone else is having these issues maybe I need to docker-compose up -d -force -recreate
?
Yup probably something super simple I'm messing up
an exception might be ocurring...what's your code now?
I can't share the exact appwrite url. I can show how I have it formatted. Sorry for not being able to share that
Sure thing don't be sorry
Same code
Pastebin.com is the number one paste tool since 2002. Pastebin is a website where you can store text online for a set period of time.
did you remove the status: 500?
Yup that's the only part I changed
I removed status:500
and you have all your variables set?
And you sure that this variable APPWRITE_FUNCTION_ENDPOINT contains the same URL you have here? in your project settings?
Yes I made sure it's the same as url in the project settings
It does something weird tho that I've never seen a url do let me show you
See the weird backspaces
It's okay It try to escape it
But why you have the value test.org in there?
Ohh, so you have replace it in inspect element?
Yes
Clever 👍
isn't this a previous execution?
And you can verify that everything in order when you click the eye button in the function settings page?
Yes that's okay
That's project of mine
Oh okay
could you try putting the client initialization code into the try too?
move the try { line up before final client = ...
Just to know what version of Appwrite you're running on?
You can find it either in the bottom of your console or by going to your appwrite domain and adding versions at the end as such appwrite.domain.com/versions
how long did it run for and what was the execution logs?
I ran this exact code and it was successful and gave me a response:
import 'package:dart_appwrite/dart_appwrite.dart';
Future<void> start(final req, final res) async {
try {
final client = Client();
client
.setEndpoint(req.variables['APPWRITE_FUNCTION_ENDPOINT'])
.setProject(req.variables['APPWRITE_FUNCTION_PROJECT_ID'])
.setKey(req.variables['APPWRITE_FUNCTION_API_KEY'])
.setSelfSigned();
//res.json(req.variables);
final users = Users(client);
final userId =
req.payload; // Assuming the userId is passed as the request payload
await users.delete(userId: userId);
res.json({'message': 'User deleted'});
} catch (e) {
res.send('Error deleting user: $e');
}
}
so if you have this...it shouldn't fail...
can you share a screenshot of your deployments, executions, and settings (variables and timeout) pages
Awesome glad it's just me. Here are all the screenshots
Didn't send one for the response logs because it's the same as the logs tab
I do have a real-time subscription running on the page that I'm trying to run the delete
Not sure if that could be it but at this point I'm pretty sure it's something simple I missed
Sorry for all the trouble guys
Quite the opposite when finding the solution it can help in the future
And how many real-time subscriptions is too much. Right now I'm using 2
Can you share also the settings screen.
so if you comment out the await.users.delete(), it'll execute successfully?
that's wierd those status circles are blue 👀
I think I found the issue guys
I added dart to the function runtimes but not to the function envs
It's okay
Could that be it?
The last one is deprecated
Dammit
Yeah, sorry
The problem is only between the function and the server it tries to connect.
Damn
Can you try what Steven suggested to run it without the user line
like so
import 'package:dart_appwrite/dart_appwrite.dart';
Future<void> start(final req, final res) async {
try {
final client = Client();
client
.setEndpoint(req.variables['APPWRITE_FUNCTION_ENDPOINT'])
.setProject(req.variables['APPWRITE_FUNCTION_PROJECT_ID'])
.setKey(req.variables['APPWRITE_FUNCTION_API_KEY'])
.setSelfSigned();
//res.json(req.variables);
final users = Users(client);
final userId = req.payload; // Assuming the userId is passed as the request payload
// await users.delete(userId: userId);
res.json({'message': 'User deleted'});
} catch (e) {
res.send('Error deleting user: $e');
}
}
Okay
Yup the code Steven sent works for me too Byawitz
Issue is when I try to call the Users.delete()
Will try to call it
it still makes no sense...if an exception is thrown due to that, the catch should catch the exception and then we should see response logs 😵💫
are you executing the function from the console?
I think we can add timeout to the await function and then dart should throw an error, maybe this will help debug the problem
Try this:
...
await users.delete(userId: userId).timeout(const Duration(seconds: 1));
...
theoretically, a 2 minute timeout on the function should give it plenty of time to timeout. I used an invalid appwrite endpoint and it timed out after 1m resulting in:
Nice, got it.
I think there is a way, some way to fix it.
Can you share a picture of the output of the command
docker ps -a
I moved the delete button to a different page that doest use real-time to make sure that wasnt what was causing it
I'll try the timeout you told me Byawitz
Yeah Steven you're right by I think the Users.delete() is not working for some reason otherwise event works
Eveything
I'll try to run that command Byawitz on my server
one other thing you can try is disabling the firewall on your server
That was my way of thinking on it.
you can try byawitz's suggestion first
I'm guessing most people have a way easier time deleting a user than this
Deployed his solution now and gonna try it
It completes
But future didn't
Completed in the duration time 1s
And Steven I'm not deploying it on the console but when I do I get the same error
im also curious to try some other commands in the runtime container...would you try running this on the server?
docker exec -it [YOUR PROJECT ID]-[FUNCTION DEPLOYMENT ID] curl -k [YOUR ENDPOINT]/health/version
Okay
Running that now
It looks paused not sure if that's normal
Had to cancel it
Doesn't show anything
what's paused?
It's not paused anymore I finally got an output. It says " curl: (28) Failed to connect to appwrite.myurl.org port 443: Connection timed out
I guess curl is where my problems are?
I think you should check the Firewall as Steven suggested
In what company is your server in?
or what operating system is your appwrite instance running on?
see i expected this kind of error from the function execution..i still have no idea why we weren't seeing any response
Damn
Atleast I now know I didn't do anything wrong
I was feeling kinda down for not making it work
Maybe unrelated but when I deploy my function it doesn't give me the correct time it created the deployment
I usually deploy multiple times for it to show the right time
what do you mean?
On the appwrite console when I do a new deployment the time created says I did it 20 mins ago instead of right now do I usually redeploy and the the time created is the current time
do you mean in the inactive table?
when you deploy, you should see something show up in the inactive table as it's building:
Then, the updated at timestamp in the active section should update. and the build logs should show you the logs for the active deployment:
Ahhh gotcha
I was always reading the inactive
But for the server test you told me to run I used the right deployment id
so what operating system is your appwrite instance running on?
It's on Ubuntu 20.04 x64
try:
sudo ufw status
good. this confirms you have a firewall enabled. just for testing, you can disable it to see if it makes a difference. disable with:
sudo ufw disable
Okay will do right now
then you can try executing your function or that docker exec curl command
I can later add an exception if this fixes it?
Cool
WOW it worked
Unbelievable
I would've never fixed that alone
You guys are awesome
How can I add firewall exceptions and what exceptions
I have no idea where to start
Thank you guys @undone lantern and @lunar moon
now turn firewall back on and allow your appwrite ports (I'm assuming it's 80 and 443):
sudo ufw enable
sudo ufw allow proto tcp from any to any port 80,443
this would have been 100000x easier if that function returned the error...which it really should have. i've run into this case before but the function always returned an error so we knew the request was being blocked
Wow I guess my firewall was maybe even blocking the error from being return
maybe we needed a longer timeout 🤷🏼♂️
does the function still work?
Let me make a new user and try
And I have a couple questions to ask that aren't related but I just wanna get to know appwrite more
best to use separate #1072905050399191082 posts
Okay will do
They aren't bugs
Don't worry
But I'll make separate support posts
The function failed with the exceptions I added
huh?
port should be 443
Worked when I disabled the firewall again
Oops
How do I delete a port
I think I found out
Sudo udw delete
Followed by the number
Added the right port number
Yup works
Amd thank you guys
And
thanks for your patience